Skip to content

Commit 10a5255

Browse files
committed
Render the double-diff more like git with a background on the sign
1 parent 6341619 commit 10a5255

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
@@ -179,7 +179,7 @@ pub async fn gh_range_diff(
179179
</head>
180180
<body>
181181
<h3>range-diff of {oldbase}...{oldhead} {newbase}...{newhead}</h3>
182-
<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>
182+
<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>
183183
"#
184184
)?;
185185

@@ -274,24 +274,21 @@ pub async fn gh_range_diff(
274274
Ok((StatusCode::OK, headers, html))
275275
}
276276

277+
const REMOVED_BLOCK_SIGN: &str = r#"<span style="background-color:red;color:white;">-</span>"#;
278+
const ADDED_BLOCK_SIGN: &str = r#"<span style="background-color:green;color:white;">+</span>"#;
279+
277280
struct HtmlDiffPrinter<'a>(pub &'a Interner<&'a str>);
278281

279282
impl HtmlDiffPrinter<'_> {
280-
fn handle_hunk_token(
281-
&self,
282-
mut f: impl fmt::Write,
283-
span_open: &str,
284-
token: &str,
285-
) -> fmt::Result {
286-
write!(f, "{span_open}")?;
283+
fn handle_hunk_token(&self, mut f: impl fmt::Write, color: &str, token: &str) -> fmt::Result {
287284
// Highlight the whole the line only if it has changes it-self, otherwise
288285
// only highlight the `+`, `-` to avoid distracting users with context
289286
// changes.
290287
if token.starts_with('+') || token.starts_with('-') {
288+
write!(f, r#"<span style="color:{color}">"#)?;
291289
pulldown_cmark_escape::escape_html(FmtWriter(&mut f), token)?;
292290
write!(f, "</span>")?;
293291
} else {
294-
write!(f, "</span>")?;
295292
pulldown_cmark_escape::escape_html(FmtWriter(&mut f), token)?;
296293
}
297294
Ok(())
@@ -330,7 +327,8 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
330327
if let Some(&last) = before.last() {
331328
for &token in before {
332329
let token = self.0[token];
333-
self.handle_hunk_token(&mut f, r#"<span style="color:red;">-"#, token)?;
330+
write!(f, "{REMOVED_BLOCK_SIGN}")?;
331+
self.handle_hunk_token(&mut f, "red", token)?;
334332
}
335333
if !self.0[last].ends_with('\n') {
336334
writeln!(f)?;
@@ -340,7 +338,8 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
340338
if let Some(&last) = after.last() {
341339
for &token in after {
342340
let token = self.0[token];
343-
self.handle_hunk_token(&mut f, r#"<span style="color:green;">+"#, token)?;
341+
write!(f, "{ADDED_BLOCK_SIGN}")?;
342+
self.handle_hunk_token(&mut f, "green", token)?;
344343
}
345344
if !self.0[last].ends_with('\n') {
346345
writeln!(f)?;

0 commit comments

Comments
 (0)