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

Redo of background colors to improve diff readability #690

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 8 additions & 8 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ radix-heap = "0.4.2"
# ignore 0.4.19 requires scoped_threads, which was added in rust 1.63.
ignore = ">= 0.4, < 0.4.19"
const_format = "0.2.22"
owo-colors = "3.5.0"
wu-diff = "0.1.2"
rayon = "1.7.0"

Expand All @@ -80,6 +79,7 @@ humansize = "2.1.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
line-numbers = "0.3.0"
yansi = "1.0.1"

[dev-dependencies]
# assert_cmd 2.0.6 requires rust 1.60
Expand Down
26 changes: 20 additions & 6 deletions src/display/inline.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Inline, or "unified" diff display.

use yansi::Paint;

use crate::{
constants::Side,
display::context::{calculate_after_context, calculate_before_context, opposite_positions},
Expand Down Expand Up @@ -27,17 +29,15 @@ pub(crate) fn print(
apply_colors(
lhs_src,
Side::Left,
display_options.syntax_highlight,
display_options,
file_format,
display_options.background_color,
lhs_positions,
),
apply_colors(
rhs_src,
Side::Right,
display_options.syntax_highlight,
display_options,
file_format,
display_options.background_color,
rhs_positions,
),
)
Expand Down Expand Up @@ -112,7 +112,14 @@ pub(crate) fn print(
Side::Left,
display_options,
),
lhs_colored_lines[lhs_line.as_usize()]
// wrap bg color here
// TODO: might need to extend lines so background color shows up on right side
Paint::bg(
&lhs_colored_lines[lhs_line.as_usize()],
display_options.theme.novel_bg_left
)
.wrap()
.to_string()
);
}
}
Expand All @@ -126,7 +133,14 @@ pub(crate) fn print(
Side::Right,
display_options,
),
rhs_colored_lines[rhs_line.as_usize()]
// wrap bg color here
// TODO: might need to extend lines so background color shows up on right side
Paint::bg(
&rhs_colored_lines[rhs_line.as_usize()],
display_options.theme.novel_bg_right
)
.wrap()
.to_string()
);
}
}
Expand Down
111 changes: 61 additions & 50 deletions src/display/side_by_side.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ use std::{

use line_numbers::LineNumber;
use line_numbers::SingleLineSpan;
use owo_colors::{OwoColorize, Style};
use yansi::{Paint, Style};

use crate::{
constants::Side,
display::context::all_matched_lines_filled,
display::hunks::{matched_lines_indexes_for_hunk, Hunk},
display::style::{
self, apply_colors, apply_line_number_color, color_positions, novel_style, replace_tabs,
split_and_apply, BackgroundColor,
self, apply_colors, apply_line_number_color, color_positions, replace_tabs, split_and_apply,
},
hash::DftHashMap,
lines::format_line_num,
Expand Down Expand Up @@ -52,7 +51,7 @@ fn format_missing_line_num(

let mut style = Style::new();
if use_color {
style = style.dimmed();
style = style.dim();
}

let num_digits = prev_num.display().len();
Expand All @@ -61,7 +60,7 @@ fn format_missing_line_num(
(if after_end { " " } else { "." }).repeat(num_digits),
width = column_width - 1
)
.style(style)
.paint(style)
.to_string()
}

Expand Down Expand Up @@ -92,14 +91,14 @@ fn display_single_column(

let mut style = Style::new();
if display_options.use_color {
style = novel_style(Style::new(), side, display_options.background_color);
style = *display_options.theme.lineno_style(false, side);
}

for (i, line) in src_lines.iter().enumerate() {
let mut formatted_line = String::with_capacity(line.len());
formatted_line.push_str(
&format_line_num_padded((i as u32).into(), column_width)
.style(style)
.paint(style)
.to_string(),
);
formatted_line.push_str(line);
Expand Down Expand Up @@ -237,22 +236,15 @@ pub(crate) fn lines_with_novel(
/// Calculate positions of highlights on both sides. This includes
/// both syntax highlighting and added/removed content highlighting.
fn highlight_positions(
background: BackgroundColor,
syntax_highlight: bool,
display_options: &DisplayOptions,
file_format: &FileFormat,
lhs_mps: &[MatchedPos],
rhs_mps: &[MatchedPos],
) -> (
DftHashMap<LineNumber, Vec<(SingleLineSpan, Style)>>,
DftHashMap<LineNumber, Vec<(SingleLineSpan, Style)>>,
) {
let lhs_positions = color_positions(
Side::Left,
background,
syntax_highlight,
file_format,
lhs_mps,
);
let lhs_positions = color_positions(Side::Left, display_options, file_format, lhs_mps);
// Preallocate the hashmap assuming the average line will have 2 items on it.
let mut lhs_styles: DftHashMap<LineNumber, Vec<(SingleLineSpan, Style)>> =
DftHashMap::default();
Expand All @@ -261,13 +253,7 @@ fn highlight_positions(
styles.push((span, style));
}

let rhs_positions = color_positions(
Side::Right,
background,
syntax_highlight,
file_format,
rhs_mps,
);
let rhs_positions = color_positions(Side::Right, display_options, file_format, rhs_mps);
let mut rhs_styles: DftHashMap<LineNumber, Vec<(SingleLineSpan, Style)>> =
DftHashMap::default();
for (span, style) in rhs_positions {
Expand Down Expand Up @@ -315,22 +301,8 @@ pub(crate) fn print(
) {
let (lhs_colored_lines, rhs_colored_lines) = if display_options.use_color {
(
apply_colors(
lhs_src,
Side::Left,
display_options.syntax_highlight,
file_format,
display_options.background_color,
lhs_mps,
),
apply_colors(
rhs_src,
Side::Right,
display_options.syntax_highlight,
file_format,
display_options.background_color,
rhs_mps,
),
apply_colors(lhs_src, Side::Left, display_options, file_format, lhs_mps),
apply_colors(rhs_src, Side::Right, display_options, file_format, rhs_mps),
)
} else {
(
Expand Down Expand Up @@ -381,13 +353,7 @@ pub(crate) fn print(

// TODO: this is largely duplicating the `apply_colors` logic.
let (lhs_highlights, rhs_highlights) = if display_options.use_color {
highlight_positions(
display_options.background_color,
display_options.syntax_highlight,
file_format,
lhs_mps,
rhs_mps,
)
highlight_positions(display_options, file_format, lhs_mps, rhs_mps)
} else {
(DftHashMap::default(), DftHashMap::default())
};
Expand Down Expand Up @@ -466,6 +432,17 @@ pub(crate) fn print(
match rhs_line_num {
Some(rhs_line_num) => {
let rhs_line = &rhs_colored_lines[rhs_line_num.as_usize()];

let rhs_line = if rhs_lines_with_novel.contains(rhs_line_num) {
// TODO: replace the argument to on_fixed with the color from the theme
Paint::bg(&rhs_line, display_options.theme.novel_bg_right)
.wrap()
.to_string()
} else {
rhs_line.to_string()
};

// TODO: add line bg color here
if same_lines {
print!("{}{}", display_rhs_line_num, rhs_line);
} else {
Expand All @@ -486,6 +463,17 @@ pub(crate) fn print(
match lhs_line_num {
Some(lhs_line_num) => {
let lhs_line = &lhs_colored_lines[lhs_line_num.as_usize()];

let lhs_line = if lhs_lines_with_novel.contains(lhs_line_num) {
// TODO: replace the argument to on_fixed with the color from the theme
Paint::bg(&lhs_line, display_options.theme.novel_bg_left)
.wrap()
.to_string()
} else {
lhs_line.to_string()
};

// TODO: add line bg color here
if same_lines {
print!("{}{}", display_lhs_line_num, lhs_line);
} else {
Expand Down Expand Up @@ -518,6 +506,7 @@ pub(crate) fn print(
rhs_highlights.get(rhs_line_num).unwrap_or(&vec![]),
Side::Right,
),
// NOTE: do not pad the below string, it will cause excess blank lines
None => vec!["".into()],
};

Expand All @@ -527,7 +516,8 @@ pub(crate) fn print(
{
let lhs_line =
lhs_line.unwrap_or_else(|| " ".repeat(source_dims.content_width));
let rhs_line = rhs_line.unwrap_or_else(|| "".into());
let rhs_line =
rhs_line.unwrap_or_else(|| " ".repeat(source_dims.content_width - 8));
let lhs_num: String = if i == 0 {
display_lhs_line_num.clone()
} else {
Expand Down Expand Up @@ -569,7 +559,28 @@ pub(crate) fn print(
s
};

println!("{}{}{}{}{}", lhs_num, lhs_line, SPACER, rhs_num, rhs_line);
println!(
"{}{}{}{}{}",
lhs_num,
match lhs_line_num {
Some(line_num) if lhs_lines_with_novel.contains(line_num) =>
// TODO: replace the argument to on_fixed with the color from the theme
Paint::bg(&lhs_line, display_options.theme.novel_bg_left)
.wrap()
.to_string(),
_ => lhs_line,
},
SPACER,
rhs_num,
match rhs_line_num {
Some(line_num) if rhs_lines_with_novel.contains(line_num) =>
// TODO: replace the argument to on_fixed with the color from the theme
Paint::bg(&rhs_line, display_options.theme.novel_bg_right)
.wrap()
.to_string(),
_ => rhs_line,
}
);
}
}

Expand Down Expand Up @@ -615,7 +626,7 @@ mod tests {

assert_eq!(
format_missing_line_num(0.into(), &source_dims, Side::Left, true),
". ".dimmed().to_string()
". ".dim().to_string()
);
assert_eq!(
format_missing_line_num(0.into(), &source_dims, Side::Left, false),
Expand All @@ -635,7 +646,7 @@ mod tests {

assert_eq!(
format_missing_line_num(1.into(), &source_dims, Side::Left, true),
" ".dimmed().to_string()
" ".dim().to_string()
);
assert_eq!(
format_missing_line_num(1.into(), &source_dims, Side::Left, false),
Expand Down
Loading