Skip to content

Commit d029fea

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

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

src/gh_range_diff.rs

Lines changed: 39 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,18 @@ 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 {
287313
// Highlight the whole the line only if it has changes it-self, otherwise
288314
// only highlight the `+`, `-` to avoid distracting users with context
289315
// changes.
290316
if token.starts_with('+') || token.starts_with('-') {
291-
write!(f, r#"<span style="color:{color}">"#)?;
317+
write!(f, r#" <span class="{class}">"#)?;
292318
pulldown_cmark_escape::escape_html(FmtWriter(&mut f), token)?;
293319
write!(f, "</span>")?;
294320
} else {
@@ -313,7 +339,7 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
313339

314340
fn display_context_token(&self, mut f: impl fmt::Write, token: Token) -> fmt::Result {
315341
let token = self.0[token];
316-
write!(f, " ")?;
342+
write!(f, " ")?;
317343
pulldown_cmark_escape::escape_html(FmtWriter(&mut f), token)?;
318344
if !token.ends_with('\n') {
319345
writeln!(f)?;
@@ -331,7 +357,7 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
331357
for &token in before {
332358
let token = self.0[token];
333359
write!(f, "{REMOVED_BLOCK_SIGN}")?;
334-
self.handle_hunk_token(&mut f, "red", token)?;
360+
self.handle_hunk_token(&mut f, "removed-line", token)?;
335361
}
336362
if !self.0[last].ends_with('\n') {
337363
writeln!(f)?;
@@ -342,7 +368,7 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
342368
for &token in after {
343369
let token = self.0[token];
344370
write!(f, "{ADDED_BLOCK_SIGN}")?;
345-
self.handle_hunk_token(&mut f, "green", token)?;
371+
self.handle_hunk_token(&mut f, "added-line", token)?;
346372
}
347373
if !self.0[last].ends_with('\n') {
348374
writeln!(f)?;

0 commit comments

Comments
 (0)