From c2c279b5fb8b79d5ce2991682ca6a0c5958f87c3 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Tue, 16 Jun 2020 21:15:56 -0400 Subject: Highlight added/removed empty lines if they would be invisible Thanks @phillipwood --- src/cli.rs | 11 ++++ src/config.rs | 48 +++++++++++++++- src/delta.rs | 2 + src/paint.rs | 24 +++++++- src/set_options.rs | 2 + src/style.rs | 8 +++ src/tests/ansi_test_utils.rs | 1 + src/tests/test_example_diffs.rs | 118 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 211 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/cli.rs b/src/cli.rs index 52cbe0e7..e91b7056 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -404,6 +404,17 @@ pub struct Opt { #[structopt(parse(from_os_str))] pub plus_file: Option, + /// Style for removed empty line marker (used only if --minus-style has no background color) + #[structopt( + long = "--minus-empty-line-marker-style", + default_value = "normal auto" + )] + pub minus_empty_line_marker_style: String, + + /// Style for added empty line marker (used only if --plus-style has no background color) + #[structopt(long = "--plus-empty-line-marker-style", default_value = "normal auto")] + pub plus_empty_line_marker_style: String, + #[structopt(long = "minus-color")] /// Deprecated: use --minus-style='normal my_background_color'. pub deprecated_minus_background_color: Option, diff --git a/src/config.rs b/src/config.rs index ee2b4c16..794acc7d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -44,6 +44,7 @@ pub struct Config { pub max_line_distance: f64, pub max_line_distance_for_naively_paired_lines: f64, pub minus_emph_style: Style, + pub minus_empty_line_marker_style: Style, pub minus_file: Option, pub minus_non_emph_style: Style, pub minus_style: Style, @@ -58,6 +59,7 @@ pub struct Config { pub number_plus_style: Style, pub paging_mode: PagingMode, pub plus_emph_style: Style, + pub plus_empty_line_marker_style: Style, pub plus_file: Option, pub plus_non_emph_style: Style, pub plus_style: Style, @@ -207,10 +209,12 @@ impl From for Config { minus_style, minus_emph_style, minus_non_emph_style, + minus_empty_line_marker_style, zero_style, plus_style, plus_emph_style, plus_non_emph_style, + plus_empty_line_marker_style, ) = make_hunk_styles(&opt, is_light_mode, true_color); let (commit_style, file_style, hunk_header_style) = @@ -268,6 +272,7 @@ impl From for Config { max_line_distance: opt.max_line_distance, max_line_distance_for_naively_paired_lines, minus_emph_style, + minus_empty_line_marker_style, minus_file: opt.minus_file.map(|s| s.clone()), minus_non_emph_style, minus_style, @@ -282,6 +287,7 @@ impl From for Config { number_plus_style, paging_mode, plus_emph_style, + plus_empty_line_marker_style, plus_file: opt.plus_file.map(|s| s.clone()), plus_non_emph_style, plus_style, @@ -303,7 +309,17 @@ fn make_hunk_styles<'a>( opt: &'a cli::Opt, is_light_mode: bool, true_color: bool, -) -> (Style, Style, Style, Style, Style, Style, Style) { +) -> ( + Style, + Style, + Style, + Style, + Style, + Style, + Style, + Style, + Style, +) { let minus_style = Style::from_str( &opt.minus_style, None, @@ -337,6 +353,20 @@ fn make_hunk_styles<'a>( false, ); + // The style used to highlight a removed empty line when otherwise it would be invisible due to + // lack of background color in minus-style. + let minus_empty_line_marker_style = Style::from_str( + &opt.minus_empty_line_marker_style, + None, + Some(color::get_minus_background_color_default( + is_light_mode, + true_color, + )), + None, + true_color, + false, + ); + let zero_style = Style::from_str(&opt.zero_style, None, None, None, true_color, false); let plus_style = Style::from_str( @@ -372,14 +402,30 @@ fn make_hunk_styles<'a>( false, ); + // The style used to highlight an added empty line when otherwise it would be invisible due to + // lack of background color in plus-style. + let plus_empty_line_marker_style = Style::from_str( + &opt.plus_empty_line_marker_style, + None, + Some(color::get_plus_background_color_default( + is_light_mode, + true_color, + )), + None, + true_color, + false, + ); + ( minus_style, minus_emph_style, minus_non_emph_style, + minus_empty_line_marker_style, zero_style, plus_style, plus_emph_style, plus_non_emph_style, + plus_empty_line_marker_style, ) } diff --git a/src/delta.rs b/src/delta.rs index 36ea6a56..366520ec 100644 --- a/src/delta.rs +++ b/src/delta.rs @@ -405,6 +405,7 @@ fn handle_hunk_header_line( "", config.null_style, config.null_style, + None, Some(false), ); painter.output_buffer.pop(); // trim newline @@ -495,6 +496,7 @@ fn handle_hunk_line( config.zero_style, config.zero_style, None, + None, ); painter.minus_line_number += 1; painter.plus_line_number += 1; diff --git a/src/paint.rs b/src/paint.rs index 62a7f64f..d9dddd62 100644 --- a/src/paint.rs +++ b/src/paint.rs @@ -14,6 +14,8 @@ use crate::paint::superimpose_style_sections::superimpose_style_sections; use crate::style::Style; pub const ANSI_CSI_CLEAR_TO_EOL: &str = "\x1b[0K"; +pub const ANSI_CSI_CLEAR_TO_BOL: &str = "\x1b[1K"; +pub const ANSI_CSI_CURSOR_BACK_1: &str = "\x1b[1D"; pub const ANSI_SGR_RESET: &str = "\x1b[0m"; pub struct Painter<'a> { @@ -103,6 +105,7 @@ impl<'a> Painter<'a> { }, self.config.minus_style, self.config.minus_non_emph_style, + Some(self.config.minus_empty_line_marker_style), None, ); } @@ -120,6 +123,7 @@ impl<'a> Painter<'a> { }, self.config.plus_style, self.config.plus_non_emph_style, + Some(self.config.plus_empty_line_marker_style), None, ); } @@ -138,6 +142,7 @@ impl<'a> Painter<'a> { prefix: &str, style: Style, // style for right fill if line contains no emph sections non_emph_style: Style, // style for right fill if line contains emph sections + empty_line_style: Option