diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-06-27 13:54:49 -0400 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2020-06-27 14:02:57 -0400 |
commit | 315cc954ab66571b0c576d2ce0e01378535c2c25 (patch) | |
tree | 3b140acdfb057fe803bab7c020d0464e9fd69fa9 | |
parent | ea7266594a7a470f98a309bb4c0da7359b2037e2 (diff) |
Refactor: pass foreground & background defaults as a single Style
-rw-r--r-- | src/color.rs | 16 | ||||
-rw-r--r-- | src/config.rs | 131 | ||||
-rw-r--r-- | src/parse_style.rs | 90 | ||||
-rw-r--r-- | src/style.rs | 14 | ||||
-rw-r--r-- | src/tests/ansi_test_utils.rs | 2 | ||||
-rw-r--r-- | src/tests/test_example_diffs.rs | 5 |
6 files changed, 105 insertions, 153 deletions
diff --git a/src/color.rs b/src/color.rs index 463bfc22..da05c26c 100644 --- a/src/color.rs +++ b/src/color.rs @@ -24,18 +24,10 @@ pub fn color_from_rgb_or_ansi_code(s: &str, true_color: bool) -> Color { to_ansi_color(syntect_color, true_color) } -pub fn color_from_rgb_or_ansi_code_with_default( - arg: &str, - default: Option<Color>, - true_color: bool, -) -> Option<Color> { - let arg = arg.to_lowercase(); - if arg == "normal" { - None - } else if arg == "auto" { - default - } else { - Some(color_from_rgb_or_ansi_code(&arg, true_color)) +pub fn color_from_rgb_or_ansi_code_with_default(arg: &str, true_color: bool) -> Option<Color> { + match arg { + "normal" => None, + s => Some(color_from_rgb_or_ansi_code(s, true_color)), } } diff --git a/src/config.rs b/src/config.rs index abe746cb..672a9507 100644 --- a/src/config.rs +++ b/src/config.rs @@ -258,10 +258,12 @@ fn make_hunk_styles<'a>( ) { let minus_style = Style::from_str( &opt.minus_style, - None, - Some(color::get_minus_background_color_default( - is_light_mode, - true_color, + Some(Style::from_colors( + None, + Some(color::get_minus_background_color_default( + is_light_mode, + true_color, + )), )), None, true_color, @@ -270,10 +272,12 @@ fn make_hunk_styles<'a>( let minus_emph_style = Style::from_str( &opt.minus_emph_style, - None, - Some(color::get_minus_emph_background_color_default( - is_light_mode, - true_color, + Some(Style::from_colors( + None, + Some(color::get_minus_emph_background_color_default( + is_light_mode, + true_color, + )), )), None, true_color, @@ -282,8 +286,7 @@ fn make_hunk_styles<'a>( let minus_non_emph_style = Style::from_str( &opt.minus_non_emph_style, - minus_style.ansi_term_style.foreground, - minus_style.ansi_term_style.background, + Some(minus_style), None, true_color, false, @@ -293,24 +296,28 @@ fn make_hunk_styles<'a>( // 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, + Some(Style::from_colors( + 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 zero_style = Style::from_str(&opt.zero_style, None, None, true_color, false); let plus_style = Style::from_str( &opt.plus_style, - None, - Some(color::get_plus_background_color_default( - is_light_mode, - true_color, + Some(Style::from_colors( + None, + Some(color::get_plus_background_color_default( + is_light_mode, + true_color, + )), )), None, true_color, @@ -319,10 +326,12 @@ fn make_hunk_styles<'a>( let plus_emph_style = Style::from_str( &opt.plus_emph_style, - None, - Some(color::get_plus_emph_background_color_default( - is_light_mode, - true_color, + Some(Style::from_colors( + None, + Some(color::get_plus_emph_background_color_default( + is_light_mode, + true_color, + )), )), None, true_color, @@ -331,8 +340,7 @@ fn make_hunk_styles<'a>( let plus_non_emph_style = Style::from_str( &opt.plus_non_emph_style, - plus_style.ansi_term_style.foreground, - plus_style.ansi_term_style.background, + Some(plus_style), None, true_color, false, @@ -342,24 +350,20 @@ fn make_hunk_styles<'a>( // 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, + Some(Style::from_colors( + None, + Some(color::get_plus_background_color_default( + is_light_mode, + true_color, + )), )), None, true_color, false, ); - let whitespace_error_style = Style::from_str( - &opt.whitespace_error_style, - None, - None, - None, - true_color, - false, - ); + let whitespace_error_style = + Style::from_str(&opt.whitespace_error_style, None, None, true_color, false); ( minus_style, @@ -379,50 +383,20 @@ fn make_line_number_styles<'a>( opt: &'a cli::Opt, true_color: bool, ) -> (Style, Style, Style, Style, Style) { - let line_numbers_left_style = Style::from_str( - &opt.line_numbers_left_style, - None, - None, - None, - true_color, - false, - ); + let line_numbers_left_style = + Style::from_str(&opt.line_numbers_left_style, None, None, true_color, false); - let line_numbers_minus_style = Style::from_str( - &opt.line_numbers_minus_style, - None, - None, - None, - true_color, - false, - ); + let line_numbers_minus_style = + Style::from_str(&opt.line_numbers_minus_style, None, None, true_color, false); - let line_numbers_zero_style = Style::from_str( - &opt.line_numbers_zero_style, - None, - None, - None, - true_color, - false, - ); + let line_numbers_zero_style = + Style::from_str(&opt.line_numbers_zero_style, None, None, true_color, false); - let line_numbers_plus_style = Style::from_str( - &opt.line_numbers_plus_style, - None, - None, - None, - true_color, - false, - ); + let line_numbers_plus_style = + Style::from_str(&opt.line_numbers_plus_style, None, None, true_color, false); - let line_numbers_right_style = Style::from_str( - &opt.line_numbers_right_style, - None, - None, - None, - true_color, - false, - ); + let line_numbers_right_style = + Style::from_str(&opt.line_numbers_right_style, None, None, true_color, false); ( line_numbers_minus_style, @@ -438,7 +412,6 @@ fn make_commit_file_hunk_header_styles(opt: &cli::Opt, true_color: bool) -> (Sty Style::from_str_with_handling_of_special_decoration_attributes_and_respecting_deprecated_foreground_color_arg( &opt.commit_style, None, - None, Some(&opt.commit_decoration_style), opt.deprecated_commit_color.as_deref(), true_color, @@ -447,7 +420,6 @@ fn make_commit_file_hunk_header_styles(opt: &cli::Opt, true_color: bool) -> (Sty Style::from_str_with_handling_of_special_decoration_attributes_and_respecting_deprecated_foreground_color_arg( &opt.file_style, None, - None, Some(&opt.file_decoration_style), opt.deprecated_file_color.as_deref(), true_color, @@ -456,7 +428,6 @@ fn make_commit_file_hunk_header_styles(opt: &cli::Opt, true_color: bool) -> (Sty Style::from_str_with_handling_of_special_decoration_attributes_and_respecting_deprecated_foreground_color_arg( &opt.hunk_header_style, None, - None, Some(&opt.hunk_header_decoration_style), opt.deprecated_hunk_color.as_deref(), true_color, diff --git a/src/parse_style.rs b/src/parse_style.rs index ffe514d5..cd018812 100644 --- a/src/parse_style.rs +++ b/src/parse_style.rs @@ -13,18 +13,13 @@ impl Style { /// --help` for more precise spec. pub fn from_str( style_string: &str, - foreground_default: Option<ansi_term::Color>, - background_default: Option<ansi_term::Color>, + default: Option<Self>, decoration_style_string: Option<&str>, true_color: bool, is_emph: bool, ) -> Self { - let (ansi_term_style, is_omitted, is_raw, is_syntax_highlighted) = parse_ansi_term_style( - &style_string, - foreground_default, - background_default, - true_color, - ); + let (ansi_term_style, is_omitted, is_raw, is_syntax_highlighted) = + parse_ansi_term_style(&style_string, default, true_color); let decoration_style = DecorationStyle::from_str(decoration_style_string.unwrap_or(""), true_color); Self { @@ -40,8 +35,7 @@ impl Style { /// Construct Style but interpreting 'ul', 'box', etc as applying to the decoration style. fn from_str_with_handling_of_special_decoration_attributes( style_string: &str, - foreground_default: Option<ansi_term::Color>, - background_default: Option<ansi_term::Color>, + default: Option<Self>, decoration_style_string: Option<&str>, true_color: bool, is_emph: bool, @@ -50,8 +44,7 @@ impl Style { extract_special_decoration_attributes_from_non_decoration_style_string(style_string); let mut style = Style::from_str( &style_string, - foreground_default, - background_default, + default, decoration_style_string.as_deref(), true_color, is_emph, @@ -68,8 +61,7 @@ impl Style { /// foreground color which has precedence when present. pub fn from_str_with_handling_of_special_decoration_attributes_and_respecting_deprecated_foreground_color_arg( style_string: &str, - foreground_default: Option<ansi_term::Color>, - background_default: Option<ansi_term::Color>, + default: Option<Self>, decoration_style_string: Option<&str>, deprecated_foreground_color_arg: Option<&str>, true_color: bool, @@ -77,8 +69,7 @@ impl Style { ) -> Self { let mut style = Self::from_str_with_handling_of_special_decoration_attributes( style_string, - foreground_default, - background_default, + default, decoration_style_string, true_color, is_emph, @@ -86,9 +77,8 @@ impl Style { if let Some(s) = deprecated_foreground_color_arg { // The deprecated --{commit,file,hunk}-color args functioned to set the decoration // foreground color. In the case of file, it set the text foreground color also. - let foreground_from_deprecated_arg = parse_ansi_term_style(s, None, None, true_color) - .0 - .foreground; + let foreground_from_deprecated_arg = + parse_ansi_term_style(s, None, true_color).0.foreground; style.ansi_term_style.foreground = foreground_from_deprecated_arg; style.decoration_style = match style.decoration_style { DecorationStyle::Box(mut ansi_term_style) => { @@ -140,7 +130,7 @@ impl DecorationStyle { let (special_attributes, style_string) = extract_special_decoration_attributes(&style_string); let (style, is_omitted, is_raw, is_syntax_highlighted) = - parse_ansi_term_style(&style_string, None, None, true_color); + parse_ansi_term_style(&style_string, None, true_color); if is_raw { eprintln!("'raw' may not be used in a decoration style."); process::exit(1); @@ -207,8 +197,7 @@ impl DecorationStyle { fn parse_ansi_term_style( s: &str, - foreground_default: Option<ansi_term::Color>, - background_default: Option<ansi_term::Color>, + default: Option<Style>, true_color: bool, ) -> (ansi_term::Style, bool, bool, bool) { let mut style = ansi_term::Style::new(); @@ -245,12 +234,11 @@ fn parse_ansi_term_style( } else if !seen_foreground { if word == "syntax" { is_syntax_highlighted = true; + } else if word == "auto" { + style.foreground = default.and_then(|s| s.ansi_term_style.foreground); } else { - style.foreground = color::color_from_rgb_or_ansi_code_with_default( - word, - foreground_default, - true_color, - ); + style.foreground = + color::color_from_rgb_or_ansi_code_with_default(word, true_color); } seen_foreground = true; } else if !seen_background { @@ -261,12 +249,11 @@ fn parse_ansi_term_style( color (first color in a style string)." ); process::exit(1); + } else if word == "auto" { + style.background = default.and_then(|s| s.ansi_term_style.background); } else { - style.background = color::color_from_rgb_or_ansi_code_with_default( - word, - background_default, - true_color, - ); + style.background = + color::color_from_rgb_or_ansi_code_with_default(word, true_color); } seen_background = true; } else { @@ -331,11 +318,11 @@ mod tests { #[test] fn test_parse_ansi_term_style() { assert_eq!( - parse_ansi_term_style("", None, None, false), + parse_ansi_term_style("", None, false), (ansi_term::Style::new(), false, false, false) ); assert_eq!( - parse_ansi_term_style("red", None, None, false), + parse_ansi_term_style("red", None, false), ( ansi_term::Style { foreground: Some(ansi_term::Color::Fixed( @@ -349,7 +336,7 @@ mod tests { ) ); assert_eq!( - parse_ansi_term_style("red green", None, None, false), + parse_ansi_term_style("red green", None, false), ( ansi_term::Style { foreground: Some(ansi_term::Color::Fixed( @@ -366,7 +353,7 @@ mod tests { ) ); assert_eq!( - parse_ansi_term_style("bold red underline green blink", None, None, false), + parse_ansi_term_style("bold red underline green blink", None, false), ( ansi_term::Style { foreground: Some(ansi_term::Color::Fixed( @@ -390,11 +377,11 @@ mod tests { #[test] fn test_parse_ansi_term_style_with_special_syntax_color() { assert_eq!( - parse_ansi_term_style("syntax", None, None, false), + parse_ansi_term_style("syntax", None, false), (ansi_term::Style::new(), false, false, true) ); assert_eq!( - parse_ansi_term_style("syntax italic white hidden", None, None, false), + parse_ansi_term_style("syntax italic white hidden", None, false), ( ansi_term::Style { background: Some(ansi_term::Color::Fixed( @@ -410,7 +397,7 @@ mod tests { ) ); assert_eq!( - parse_ansi_term_style("bold syntax italic white hidden", None, None, false), + parse_ansi_term_style("bold syntax italic white hidden", None, false), ( ansi_term::Style { background: Some(ansi_term::Color::Fixed( @@ -431,12 +418,12 @@ mod tests { #[test] fn test_parse_ansi_term_style_with_special_omit_attribute() { assert_eq!( - parse_ansi_term_style("omit", None, None, false), + parse_ansi_term_style("omit", None, false), (ansi_term::Style::new(), true, false, false) ); // It doesn't make sense for omit to be combined with anything else, but it is not an error. assert_eq!( - parse_ansi_term_style("omit syntax italic white hidden", None, None, false), + parse_ansi_term_style("omit syntax italic white hidden", None, false), ( ansi_term::Style { background: Some(ansi_term::Color::Fixed( @@ -456,12 +443,12 @@ mod tests { #[test] fn test_parse_ansi_term_style_with_special_raw_attribute() { assert_eq!( - parse_ansi_term_style("raw", None, None, false), + parse_ansi_term_style("raw", None, false), (ansi_term::Style::new(), false, true, false) ); // It doesn't make sense for raw to be combined with anything else, but it is not an error. assert_eq!( - parse_ansi_term_style("raw syntax italic white hidden", None, None, false), + parse_ansi_term_style("raw syntax italic white hidden", None, false), ( ansi_term::Style { background: Some(ansi_term::Color::Fixed( @@ -547,7 +534,6 @@ mod tests { let actual_style = Style::from_str( "red green bold", None, - None, Some("ol red box bold green ul"), true, false, @@ -570,7 +556,7 @@ mod tests { #[test] fn test_style_from_str_raw_with_box() { - let actual_style = Style::from_str("raw", None, None, Some("box"), true, false); + let actual_style = Style::from_str("raw", None, Some("box"), true, false); let empty_ansi_term_style = ansi_term::Style::new(); assert_eq!( actual_style, @@ -585,14 +571,7 @@ mod tests { #[test] fn test_style_from_str_decoration_style_only() { - let actual_style = Style::from_str( - "", - None, - None, - Some("ol red box bold green ul"), - true, - false, - ); + let actual_style = Style::from_str("", None, Some("ol red box bold green ul"), true, false); let red_green_bold = ansi_term::Style { foreground: Some(ansi_term::Color::Fixed(1)), background: Some(ansi_term::Color::Fixed(2)), @@ -613,7 +592,6 @@ mod tests { let actual_style = Style::from_str_with_handling_of_special_decoration_attributes( "", None, - None, Some("ol red box bold green ul"), true, false, @@ -638,7 +616,6 @@ mod tests { let actual_style = Style::from_str_with_handling_of_special_decoration_attributes( "raw", None, - None, Some("box"), true, false, @@ -665,7 +642,7 @@ mod tests { ..ansi_term::Style::new() }); let actual_style = Style::from_str_with_handling_of_special_decoration_attributes_and_respecting_deprecated_foreground_color_arg( - "", None, None, Some("ol red box bold green ul"), None, true, false + "", None, Some("ol red box bold green ul"), None, true, false ); assert_eq!( actual_style, @@ -682,7 +659,6 @@ mod tests { let actual_style = Style::from_str_with_handling_of_special_decoration_attributes_and_respecting_deprecated_foreground_color_arg( "raw", None, - None, Some("box"), None, true, diff --git a/src/style.rs b/src/style.rs index dbac0863..734d3b9f 100644 --- a/src/style.rs +++ b/src/style.rs @@ -39,6 +39,20 @@ impl Style { } } + pub fn from_colors( + foreground: Option<ansi_term::Color>, + background: Option<ansi_term::Color>, + ) -> Self { + Self { + ansi_term_style: ansi_term::Style { + foreground, + background, + ..ansi_term::Style::new() + }, + ..Self::new() + } + } + pub fn paint<'a, I, S: 'a + ToOwned + ?Sized>( self, input: I, diff --git a/src/tests/ansi_test_utils.rs b/src/tests/ansi_test_utils.rs index d3304635..e7980e35 100644 --- a/src/tests/ansi_test_utils.rs +++ b/src/tests/ansi_test_utils.rs @@ -147,7 +147,7 @@ pub mod ansi_test_utils { ) -> bool { let line = output.lines().nth(line_number).unwrap(); assert!(strip_ansi_codes(line).starts_with(expected_prefix)); - let mut style = Style::from_str(expected_style, None, None, None, config.true_color, false); + let mut style = Style::from_str(expected_style, None, None, config.true_color, false); if _4_bit_color { style.ansi_term_style.foreground = style .ansi_term_style diff --git a/src/tests/test_example_diffs.rs b/src/tests/test_example_diffs.rs index e3cf21de..305e51d8 100644 --- a/src/tests/test_example_diffs.rs +++ b/src/tests/test_example_diffs.rs @@ -1080,7 +1080,7 @@ impl<'a> Alignment<'a> { │ let output = integration_test_utils::run_delta(example_diff, &config); let line = output.lines().nth(6).unwrap(); if base_style_has_background_color { - let style = style::Style::from_str(base_style, None, None, None, true, false); + let style = style::Style::from_str(base_style, None, None, true, false); assert_eq!( line, &style @@ -1089,8 +1089,7 @@ impl<'a> Alignment<'a> { │ .to_string() ); } else { - let style = - style::Style::from_str(empty_line_marker_style, None, None, None, true, false); + let style = style::Style::from_str(empty_line_marker_style, None, None, true, false); assert_eq!( line, &style |