Skip to content

Commit 06510dd

Browse files
committed
Render the double-diff more like git with a background on the sign
1 parent fae22c1 commit 06510dd

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/gh_range_diff.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ pub async fn gh_range_diff(
182182
</head>
183183
<body>
184184
<h3>range-diff of {oldbase}...{oldhead} {newbase}...{newhead}</h3>
185-
<p>Bookmarklet: <a href="{bookmarklet}" title="Drag-and-drop me on the bookmarks bar, and use me on GitHub compare page.">range-diff</a> <span title="This javascript bookmark can be used to access this page with the right URL. To use it drag-on-drop the range-diff link to your bookmarks bar and click on it when you are on GitHub's compare page to use range-diff compare.">&#128712;</span></p>
185+
<p>Bookmarklet: <a href="{bookmarklet}" title="Drag-and-drop me on the bookmarks bar, and use me on GitHub compare page.">range-diff</a> <span title="This javascript bookmark can be used to access this page with the right URL. To use it drag-on-drop the range-diff link to your bookmarks bar and click on it when you are on GitHub's compare page to use range-diff compare.">&#128712;</span> | {ADDED_BLOCK_SIGN} added {REMOVED_BLOCK_SIGN} removed</p>
186186
"#
187187
)?;
188188

@@ -277,24 +277,21 @@ pub async fn gh_range_diff(
277277
Ok((StatusCode::OK, headers, html))
278278
}
279279

280+
const REMOVED_BLOCK_SIGN: &str = r#"<span style="background-color:red;color:white;">-</span>"#;
281+
const ADDED_BLOCK_SIGN: &str = r#"<span style="background-color:green;color:white;">+</span>"#;
282+
280283
struct HtmlDiffPrinter<'a>(pub &'a Interner<&'a str>);
281284

282285
impl HtmlDiffPrinter<'_> {
283-
fn handle_hunk_token(
284-
&self,
285-
mut f: impl fmt::Write,
286-
span_open: &str,
287-
token: &str,
288-
) -> fmt::Result {
289-
write!(f, "{span_open}")?;
286+
fn handle_hunk_token(&self, mut f: impl fmt::Write, color: &str, token: &str) -> fmt::Result {
290287
// Highlight the whole the line only if it has changes it-self, otherwise
291288
// only highlight the `+`, `-` to avoid distracting users with context
292289
// changes.
293290
if token.starts_with('+') || token.starts_with('-') {
291+
write!(f, r#"<span style="color:{color}">"#)?;
294292
pulldown_cmark_escape::escape_html(FmtWriter(&mut f), token)?;
295293
write!(f, "</span>")?;
296294
} else {
297-
write!(f, "</span>")?;
298295
pulldown_cmark_escape::escape_html(FmtWriter(&mut f), token)?;
299296
}
300297
Ok(())
@@ -333,7 +330,8 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
333330
if let Some(&last) = before.last() {
334331
for &token in before {
335332
let token = self.0[token];
336-
self.handle_hunk_token(&mut f, r#"<span style="color:red;">-"#, token)?;
333+
write!(f, "{REMOVED_BLOCK_SIGN}")?;
334+
self.handle_hunk_token(&mut f, "red", token)?;
337335
}
338336
if !self.0[last].ends_with('\n') {
339337
writeln!(f)?;
@@ -343,7 +341,8 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
343341
if let Some(&last) = after.last() {
344342
for &token in after {
345343
let token = self.0[token];
346-
self.handle_hunk_token(&mut f, r#"<span style="color:green;">+"#, token)?;
344+
write!(f, "{ADDED_BLOCK_SIGN}")?;
345+
self.handle_hunk_token(&mut f, "green", token)?;
347346
}
348347
if !self.0[last].ends_with('\n') {
349348
writeln!(f)?;

0 commit comments

Comments
 (0)