Skip to content

Commit cfdd238

Browse files
committed
Make gh_range_diff coloring same as git range-diff
Since we make the coloring the same, remove the explicit explanation, since now I think it becomes easy to see what the range-diff shows.
1 parent 08781c3 commit cfdd238

File tree

1 file changed

+45
-29
lines changed

1 file changed

+45
-29
lines changed

src/gh_range_diff.rs

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -242,18 +242,24 @@ fn process_old_new(
242242
overflow-x: auto;
243243
}}
244244
.removed-block {{
245-
background-color: rgb(255, 206, 203);
245+
background-color: rgba(255, 150, 150, 1);
246246
white-space: pre;
247247
}}
248248
.added-block {{
249-
background-color: rgb(172, 238, 187);
249+
background-color: rgba(150, 255, 150, 1);
250250
white-space: pre;
251251
}}
252-
.removed-line {{
253-
color: #DE0000;
252+
.removed-line-after {{
253+
color: rgba(255, 0, 0, 1);
254254
}}
255-
.added-line {{
256-
color: #2F6500;
255+
.added-line-after {{
256+
color: rgba(0, 255, 0, 1);
257+
}}
258+
.removed-line-before {{
259+
color: rgba(100, 0, 0, 1);
260+
}}
261+
.added-line-before {{
262+
color: rgba(0, 100, 0, 1);
257263
}}
258264
@media (prefers-color-scheme: dark) {{
259265
body {{
@@ -264,23 +270,19 @@ fn process_old_new(
264270
color: #41a6ff;
265271
}}
266272
.removed-block {{
267-
background-color: rgba(248, 81, 73, 0.1);
273+
background-color: rgba(80, 45, 45, 1);
274+
white-space: pre;
268275
}}
269276
.added-block {{
270-
background-color: rgba(46, 160, 67, 0.15);
271-
}}
272-
.removed-line {{
273-
color: #F34848;
274-
}}
275-
.added-line {{
276-
color: #86D03C;
277+
background-color: rgba(70, 120, 70, 1);
278+
white-space: pre;
277279
}}
278280
}}
279281
</style>
280282
</head>
281283
<body>
282284
<h3>range-diff of {oldbase}<wbr>...{oldhead} {newbase}<wbr>...{newhead}</h3>
283-
<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;<span class="added-line">+</span> adds a line | {ADDED_BLOCK_SIGN}&nbsp;<span class="removed-line">-</span> removes a line | {REMOVED_BLOCK_SIGN}&nbsp;<span class="removed-line">+</span> removes the added line | {REMOVED_BLOCK_SIGN}&nbsp;- cancel the removal</p>
285+
<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>
284286
"#
285287
)?;
286288

@@ -391,13 +393,19 @@ enum HunkTokenStatus {
391393
Removed,
392394
}
393395

396+
enum BeforeOrAfter {
397+
Before,
398+
After,
399+
}
400+
394401
struct HtmlDiffPrinter<'a>(pub &'a Interner<&'a str>);
395402

396403
impl HtmlDiffPrinter<'_> {
397404
fn handle_hunk_token(
398405
&self,
399406
mut f: impl fmt::Write,
400407
hunk_token_status: HunkTokenStatus,
408+
before_or_after: BeforeOrAfter,
401409
token: &str,
402410
) -> fmt::Result {
403411
// Show the hunk status
@@ -409,19 +417,17 @@ impl HtmlDiffPrinter<'_> {
409417
let is_add = token.starts_with('+');
410418
let is_remove = token.starts_with('-');
411419

412-
// Highlight the whole the line only if it has changes it-self, otherwise
413-
// only highlight the `+`, `-` to avoid distracting users with context
414-
// changes.
420+
// Highlight in the same was as `git range-diff`, which shows the diff of two diffs.
421+
// In all cases added lines are green and removed lines are red.
422+
// The most vivid colors are for the top-level diff.
423+
// Dark colors are for the "before" diff.
424+
// Light colors are for the "after" diff.
415425
if is_add || is_remove {
416-
let class = match (hunk_token_status, is_add) {
417-
// adds a line
418-
(HunkTokenStatus::Added, true) => "added-line",
419-
// removes a line
420-
(HunkTokenStatus::Added, false) => "removed-line",
421-
// removes the added line
422-
(HunkTokenStatus::Removed, true) => "removed-line",
423-
// removes the removed line, so nothing changed
424-
(HunkTokenStatus::Removed, false) => "",
426+
let class = match (before_or_after, is_add) {
427+
(BeforeOrAfter::Before, true) => "added-line-before",
428+
(BeforeOrAfter::Before, false) => "removed-line-before",
429+
(BeforeOrAfter::After, true) => "added-line-after",
430+
(BeforeOrAfter::After, false) => "removed-line-after",
425431
};
426432

427433
write!(f, r#"<span class="{class}">"#)?;
@@ -466,7 +472,12 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
466472
if let Some(&last) = before.last() {
467473
for &token in before {
468474
let token = self.0[token];
469-
self.handle_hunk_token(&mut f, HunkTokenStatus::Removed, token)?;
475+
self.handle_hunk_token(
476+
&mut f,
477+
HunkTokenStatus::Removed,
478+
BeforeOrAfter::Before,
479+
token,
480+
)?;
470481
}
471482
if !self.0[last].ends_with('\n') {
472483
writeln!(f)?;
@@ -476,7 +487,12 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
476487
if let Some(&last) = after.last() {
477488
for &token in after {
478489
let token = self.0[token];
479-
self.handle_hunk_token(&mut f, HunkTokenStatus::Added, token)?;
490+
self.handle_hunk_token(
491+
&mut f,
492+
HunkTokenStatus::Added,
493+
BeforeOrAfter::After,
494+
token,
495+
)?;
480496
}
481497
if !self.0[last].ends_with('\n') {
482498
writeln!(f)?;

0 commit comments

Comments
 (0)