Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use background colors to improve diff readability #286

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ license = "MIT"
version = "0.29.0"
authors = ["Wilfred Hughes <[email protected]>"]
keywords = ["diff", "syntax"]
categories = ["development-tools", "command-line-utilities", "parser-implementations"]
categories = [
"development-tools",
"command-line-utilities",
"parser-implementations",
]
edition = "2018"
rust-version = "1.56.0"
include = [
Expand Down Expand Up @@ -42,6 +46,8 @@ owo-colors = "3.3.0"
rpds = "0.10.0"
wu-diff = "0.1.2"
rayon = "1.5.2"
yansi = "0.5.1"
cansi = "2.1.1"
tree_magic_mini = "3.0.3"
bumpalo = "3.9.1"

Expand Down
99 changes: 86 additions & 13 deletions src/display/side_by_side.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Side-by-side (two column) display of diffs.

use cansi::{self, categorise_text};
use owo_colors::{OwoColorize, Style};
use rustc_hash::FxHashMap;
use std::{cmp::max, collections::HashSet};
use yansi::{Color, Paint};

use crate::{
constants::Side,
Expand Down Expand Up @@ -438,14 +440,39 @@ pub fn print(
match rhs_line_num {
Some(rhs_line_num) => {
let rhs_line = &rhs_colored_lines[rhs_line_num.0];
if same_lines {
println!("{}{}", display_rhs_line_num, rhs_line);
let line_to_print = if same_lines {
format!("{}{}", display_rhs_line_num, rhs_line)
} else {
println!(
format!(
"{}{}{}",
display_lhs_line_num, display_rhs_line_num, rhs_line
);
}
)
};
let (line_bg, padding_len) = if rhs_lines_with_novel.contains(&rhs_line_num)
{
(
Color::Fixed(194),
display_options.display_width
// we are using cansi::categorize_text to remove ANSI escapes
// if we don't do this, we can't properly pad the line length
// tried several other ANSI stripping libs, this one actually works
- categorise_text(&line_to_print)
.iter()
.map(|s| (s.end - s.start) as usize)
.sum::<usize>(),
)
} else {
(Color::Default, 0)
};
println!(
"{}",
Paint::wrapping(format!(
"{}{}",
line_to_print,
" ".repeat(padding_len)
))
.bg(line_bg)
);
}
None => {
// We didn't have any changed RHS lines in the
Expand All @@ -458,17 +485,42 @@ pub fn print(
match lhs_line_num {
Some(lhs_line_num) => {
let lhs_line = &lhs_colored_lines[lhs_line_num.0];
if same_lines {
println!("{}{}", display_lhs_line_num, lhs_line);
let line_to_print = if same_lines {
format!("{}{}", display_lhs_line_num, lhs_line)
} else {
println!(
format!(
"{}{}{}",
display_lhs_line_num, display_rhs_line_num, lhs_line
);
}
display_lhs_line_num, display_lhs_line_num, lhs_line
)
};
let (line_bg, padding_len) = if lhs_lines_with_novel.contains(&lhs_line_num)
{
(
Color::Fixed(224),
display_options.display_width
// we are using cansi::categorize_text to remove ANSI escapes
// if we don't do this, we can't properly pad the line length
// tried several other ANSI stripping libs, this one actually works
- categorise_text(&line_to_print)
.iter()
.map(|s| (s.end - s.start) as usize)
.sum::<usize>(),
)
} else {
(Color::Default, 0)
};
println!(
"{}",
Paint::wrapping(format!(
"{}{}",
line_to_print,
" ".repeat(padding_len)
))
.bg(line_bg)
);
}
None => {
println!("{}{}", display_lhs_line_num, display_rhs_line_num);
println!("{}{}", display_lhs_line_num, display_lhs_line_num);
}
}
} else {
Expand Down Expand Up @@ -543,7 +595,28 @@ pub fn print(
s
};

println!("{}{}{}{}{}", lhs_num, lhs_line, SPACER, rhs_num, rhs_line);
println!(
"{}{}{}",
Paint::wrapping(format!("{}{}", lhs_num, lhs_line)).bg(
if lhs_line_num.is_some()
&& lhs_lines_with_novel.contains(&lhs_line_num.unwrap())
{
Color::Fixed(224)
} else {
Color::Default
}
),
SPACER,
Paint::wrapping(format!("{}{}", rhs_num, rhs_line)).bg(
if rhs_line_num.is_some()
&& rhs_lines_with_novel.contains(&rhs_line_num.unwrap())
{
Color::Fixed(194)
} else {
Color::Default
}
),
);
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/display/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn split_and_apply(
) -> Vec<String> {
if styles.is_empty() && !line.trim().is_empty() {
// Missing styles is a bug, so highlight in purple to make this obvious.
return split_string_by_codepoint(line, max_len, matches!(side, Side::Left))
return split_string_by_codepoint(line, max_len, true)
.into_iter()
.map(|part| {
if use_color {
Expand All @@ -105,7 +105,7 @@ pub fn split_and_apply(
let mut styled_parts = vec![];
let mut part_start = 0;

for part in split_string_by_codepoint(line, max_len, matches!(side, Side::Left)) {
for part in split_string_by_codepoint(line, max_len, true) {
let mut res = String::with_capacity(part.len());
let mut prev_style_end = 0;
for (span, style) in styles {
Expand Down Expand Up @@ -225,14 +225,14 @@ fn apply(s: &str, styles: &[(SingleLineSpan, Style)]) -> String {
pub fn novel_style(style: Style, is_lhs: bool, background: BackgroundColor) -> Style {
if background.is_dark() {
if is_lhs {
style.bright_red()
style.on_red()
} else {
style.bright_green()
style.on_green()
}
} else if is_lhs {
style.red()
style.on_bright_red()
} else {
style.green()
style.on_bright_green()
}
}

Expand Down