Skip to content

Commit e4a8305

Browse files
committed
Improve range-diff colors and contrast
1 parent b21542f commit e4a8305

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

src/gh_range_diff.rs

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,26 @@ 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+
}}
172+
.removed-block {{
173+
background-color: rgb(255, 206, 203);
174+
white-space: pre;
175+
}}
176+
.added-block {{
177+
background-color: rgb(172, 238, 187);
178+
white-space: pre;
179+
}}
180+
.removed-line {{
181+
color: #DE0000;
182+
}}
183+
.added-line {{
184+
color: #2F6500;
185+
}}
166186
@media (prefers-color-scheme: dark) {{
167187
body {{
168188
background: #0C0C0C;
@@ -171,12 +191,18 @@ pub async fn gh_range_diff(
171191
a {{
172192
color: #41a6ff;
173193
}}
174-
}}
175-
details {{
176-
white-space: pre;
177-
}}
178-
summary {{
179-
font-weight: 800;
194+
.removed-block {{
195+
background-color: rgba(248, 81, 73, 0.1);
196+
}}
197+
.added-block {{
198+
background-color: rgba(46, 160, 67, 0.15);
199+
}}
200+
.removed-line {{
201+
color: #F34848;
202+
}}
203+
.added-line {{
204+
color: #86D03C;
205+
}}
180206
}}
181207
</style>
182208
</head>
@@ -277,18 +303,19 @@ pub async fn gh_range_diff(
277303
Ok((StatusCode::OK, headers, html))
278304
}
279305

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

283309
struct HtmlDiffPrinter<'a>(pub &'a Interner<&'a str>);
284310

285311
impl HtmlDiffPrinter<'_> {
286-
fn handle_hunk_token(&self, mut f: impl fmt::Write, color: &str, token: &str) -> fmt::Result {
312+
fn handle_hunk_token(&self, mut f: impl fmt::Write, class: &str, token: &str) -> fmt::Result {
313+
write!(f, " ")?;
287314
// Highlight the whole the line only if it has changes it-self, otherwise
288315
// only highlight the `+`, `-` to avoid distracting users with context
289316
// changes.
290317
if token.starts_with('+') || token.starts_with('-') {
291-
write!(f, r#"<span style="color:{color}">"#)?;
318+
write!(f, r#"<span class="{class}">"#)?;
292319
pulldown_cmark_escape::escape_html(FmtWriter(&mut f), token)?;
293320
write!(f, "</span>")?;
294321
} else {
@@ -313,7 +340,7 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
313340

314341
fn display_context_token(&self, mut f: impl fmt::Write, token: Token) -> fmt::Result {
315342
let token = self.0[token];
316-
write!(f, " ")?;
343+
write!(f, " ")?;
317344
pulldown_cmark_escape::escape_html(FmtWriter(&mut f), token)?;
318345
if !token.ends_with('\n') {
319346
writeln!(f)?;
@@ -331,7 +358,7 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
331358
for &token in before {
332359
let token = self.0[token];
333360
write!(f, "{REMOVED_BLOCK_SIGN}")?;
334-
self.handle_hunk_token(&mut f, "red", token)?;
361+
self.handle_hunk_token(&mut f, "removed-line", token)?;
335362
}
336363
if !self.0[last].ends_with('\n') {
337364
writeln!(f)?;
@@ -342,7 +369,7 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
342369
for &token in after {
343370
let token = self.0[token];
344371
write!(f, "{ADDED_BLOCK_SIGN}")?;
345-
self.handle_hunk_token(&mut f, "green", token)?;
372+
self.handle_hunk_token(&mut f, "added-line", token)?;
346373
}
347374
if !self.0[last].ends_with('\n') {
348375
writeln!(f)?;

0 commit comments

Comments
 (0)