Skip to content

Commit d489199

Browse files
authored
Merge pull request #2153 from Urgau/range_diff-2
Improve range-diff colors, contrast and mobile layout
2 parents 2d0eff3 + 3395026 commit d489199

File tree

1 file changed

+48
-16
lines changed

1 file changed

+48
-16
lines changed

src/gh_range_diff.rs

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,31 @@ pub async fn gh_range_diff(
163163
body {{
164164
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
165165
}}
166+
details {{
167+
white-space: pre;
168+
}}
169+
summary {{
170+
font-weight: 800;
171+
overflow-wrap: break-word;
172+
white-space: normal;
173+
}}
174+
.diff-content {{
175+
overflow-x: auto;
176+
}}
177+
.removed-block {{
178+
background-color: rgb(255, 206, 203);
179+
white-space: pre;
180+
}}
181+
.added-block {{
182+
background-color: rgb(172, 238, 187);
183+
white-space: pre;
184+
}}
185+
.removed-line {{
186+
color: #DE0000;
187+
}}
188+
.added-line {{
189+
color: #2F6500;
190+
}}
166191
@media (prefers-color-scheme: dark) {{
167192
body {{
168193
background: #0C0C0C;
@@ -171,18 +196,24 @@ pub async fn gh_range_diff(
171196
a {{
172197
color: #41a6ff;
173198
}}
174-
}}
175-
details {{
176-
white-space: pre;
177-
}}
178-
summary {{
179-
font-weight: 800;
199+
.removed-block {{
200+
background-color: rgba(248, 81, 73, 0.1);
201+
}}
202+
.added-block {{
203+
background-color: rgba(46, 160, 67, 0.15);
204+
}}
205+
.removed-line {{
206+
color: #F34848;
207+
}}
208+
.added-line {{
209+
color: #86D03C;
210+
}}
180211
}}
181212
</style>
182213
</head>
183214
<body>
184-
<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> | {ADDED_BLOCK_SIGN} added {REMOVED_BLOCK_SIGN} removed</p>
215+
<h3>range-diff of {oldbase}<wbr>...{oldhead} {newbase}<wbr>...{newhead}</h3>
216+
<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}&nbsp;added {REMOVED_BLOCK_SIGN}&nbsp;removed</p>
186217
"#
187218
)?;
188219

@@ -215,7 +246,7 @@ pub async fn gh_range_diff(
215246

216247
writeln!(
217248
html,
218-
r#"<details open=""><summary>{filename} <a href="{before_href}">before</a> <a href="{after_href}">after</a></summary><pre>{diff}</pre></details>"#
249+
r#"<details open=""><summary>{filename} <a href="{before_href}">before</a> <a href="{after_href}">after</a></summary><pre class="diff-content">{diff}</pre></details>"#
219250
)?;
220251
}
221252
Ok(())
@@ -277,18 +308,19 @@ pub async fn gh_range_diff(
277308
Ok((StatusCode::OK, headers, html))
278309
}
279310

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>"#;
311+
const REMOVED_BLOCK_SIGN: &str = r#"<span class="removed-block"> - </span>"#;
312+
const ADDED_BLOCK_SIGN: &str = r#"<span class="added-block"> + </span>"#;
282313

283314
struct HtmlDiffPrinter<'a>(pub &'a Interner<&'a str>);
284315

285316
impl HtmlDiffPrinter<'_> {
286-
fn handle_hunk_token(&self, mut f: impl fmt::Write, color: &str, token: &str) -> fmt::Result {
317+
fn handle_hunk_token(&self, mut f: impl fmt::Write, class: &str, token: &str) -> fmt::Result {
318+
write!(f, " ")?;
287319
// Highlight the whole the line only if it has changes it-self, otherwise
288320
// only highlight the `+`, `-` to avoid distracting users with context
289321
// changes.
290322
if token.starts_with('+') || token.starts_with('-') {
291-
write!(f, r#"<span style="color:{color}">"#)?;
323+
write!(f, r#"<span class="{class}">"#)?;
292324
pulldown_cmark_escape::escape_html(FmtWriter(&mut f), token)?;
293325
write!(f, "</span>")?;
294326
} else {
@@ -313,7 +345,7 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
313345

314346
fn display_context_token(&self, mut f: impl fmt::Write, token: Token) -> fmt::Result {
315347
let token = self.0[token];
316-
write!(f, " ")?;
348+
write!(f, " ")?;
317349
pulldown_cmark_escape::escape_html(FmtWriter(&mut f), token)?;
318350
if !token.ends_with('\n') {
319351
writeln!(f)?;
@@ -331,7 +363,7 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
331363
for &token in before {
332364
let token = self.0[token];
333365
write!(f, "{REMOVED_BLOCK_SIGN}")?;
334-
self.handle_hunk_token(&mut f, "red", token)?;
366+
self.handle_hunk_token(&mut f, "removed-line", token)?;
335367
}
336368
if !self.0[last].ends_with('\n') {
337369
writeln!(f)?;
@@ -342,7 +374,7 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
342374
for &token in after {
343375
let token = self.0[token];
344376
write!(f, "{ADDED_BLOCK_SIGN}")?;
345-
self.handle_hunk_token(&mut f, "green", token)?;
377+
self.handle_hunk_token(&mut f, "added-line", token)?;
346378
}
347379
if !self.0[last].ends_with('\n') {
348380
writeln!(f)?;

0 commit comments

Comments
 (0)