From 57d3f65e2e08c3abc5147fd0a0d115f8fab1b47d Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Thu, 31 Dec 2020 10:20:22 -0500 Subject: Add styles for file path and line number in hunk header Fixes #481 --- src/cli.rs | 12 ++++++++++++ src/config.rs | 29 ++++++++++++++++++++++++++--- src/hunk_header.rs | 12 ++++++------ src/options/set.rs | 2 ++ src/parse_style.rs | 2 +- src/style.rs | 13 ------------- src/tests/test_example_diffs.rs | 6 ++++-- 7 files changed, 51 insertions(+), 25 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index cd1555df..ee3baace 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -372,6 +372,18 @@ pub struct Opt { /// output. pub hunk_header_style: String, + #[structopt(long = "hunk-header-file-style", default_value = "blue")] + /// Style (foreground, background, attributes) for the file path part of the hunk-header. See + /// STYLES section. The file path will only be displayed if hunk-header-style contains the + /// 'file' special attribute. + pub hunk_header_file_style: String, + + #[structopt(long = "hunk-header-line-number-style", default_value = "blue")] + /// Style (foreground, background, attributes) for the line number part of the hunk-header. See + /// STYLES section. The line number will only be displayed if hunk-header-style contains the + /// 'line-number' special attribute. + pub hunk_header_line_number_style: String, + #[structopt(long = "hunk-header-decoration-style", default_value = "blue box")] /// Style (foreground, background, attributes) for the hunk-header decoration. See STYLES /// section. The style string should contain one of the special attributes 'box', 'ul' diff --git a/src/config.rs b/src/config.rs index 7331a12c..d7828952 100644 --- a/src/config.rs +++ b/src/config.rs @@ -31,6 +31,8 @@ pub struct Config { pub file_renamed_label: String, pub file_style: Style, pub git_config_entries: HashMap, + pub hunk_header_file_style: Style, + pub hunk_header_line_number_style: Style, pub hunk_header_style: Style, pub hunk_header_style_include_file_path: bool, pub hunk_header_style_include_line_number: bool, @@ -107,8 +109,13 @@ impl From for Config { whitespace_error_style, ) = make_hunk_styles(&opt); - let (commit_style, file_style, hunk_header_style) = - make_commit_file_hunk_header_styles(&opt); + let ( + commit_style, + file_style, + hunk_header_style, + hunk_header_file_style, + hunk_header_line_number_style, + ) = make_commit_file_hunk_header_styles(&opt); let ( line_numbers_minus_style, @@ -163,6 +170,8 @@ impl From for Config { file_renamed_label: opt.file_renamed_label, file_style, git_config_entries: opt.git_config_entries, + hunk_header_file_style, + hunk_header_line_number_style, hunk_header_style, hunk_header_style_include_file_path: opt .hunk_header_style @@ -384,7 +393,7 @@ fn make_line_number_styles(opt: &cli::Opt) -> (Style, Style, Style, Style, Style ) } -fn make_commit_file_hunk_header_styles(opt: &cli::Opt) -> (Style, Style, Style) { +fn make_commit_file_hunk_header_styles(opt: &cli::Opt) -> (Style, Style, Style, Style, Style) { let true_color = opt.computed.true_color; ( Style::from_str_with_handling_of_special_decoration_attributes_and_respecting_deprecated_foreground_color_arg( @@ -411,6 +420,20 @@ fn make_commit_file_hunk_header_styles(opt: &cli::Opt) -> (Style, Style, Style) true_color, false, ), + Style::from_str_with_handling_of_special_decoration_attributes( + &opt.hunk_header_file_style, + None, + None, + true_color, + false, + ), + Style::from_str_with_handling_of_special_decoration_attributes( + &opt.hunk_header_line_number_style, + None, + None, + true_color, + false, + ), ) } diff --git a/src/hunk_header.rs b/src/hunk_header.rs index f9b1b240..505b21c7 100644 --- a/src/hunk_header.rs +++ b/src/hunk_header.rs @@ -94,7 +94,7 @@ fn get_painted_file_with_line_number( let mut file_with_line_number = Vec::new(); let plus_line_number = line_numbers[line_numbers.len() - 1].0; if config.hunk_header_style_include_file_path { - file_with_line_number.push(config.file_style.paint(plus_file)) + file_with_line_number.push(config.hunk_header_file_style.paint(plus_file)) }; if config.hunk_header_style_include_line_number && !config.hunk_header_style.is_raw @@ -103,11 +103,11 @@ fn get_painted_file_with_line_number( if !file_with_line_number.is_empty() { file_with_line_number.push(ansi_term::ANSIString::from(":")); } - if let Some(style) = config.hunk_header_style.decoration_ansi_term_style() { - file_with_line_number.push(style.paint(format!("{}", plus_line_number))) - } else { - file_with_line_number.push(ansi_term::ANSIString::from(format!("{}", plus_line_number))) - } + file_with_line_number.push( + config + .hunk_header_line_number_style + .paint(format!("{}", plus_line_number)), + ) } let file_with_line_number = ansi_term::ANSIStrings(&file_with_line_number).to_string(); if config.hyperlinks { diff --git a/src/options/set.rs b/src/options/set.rs index cebdd2f7..eb74a31d 100644 --- a/src/options/set.rs +++ b/src/options/set.rs @@ -136,6 +136,8 @@ pub fn set_options( file_renamed_label, file_style, hunk_header_decoration_style, + hunk_header_file_style, + hunk_header_line_number_style, hunk_header_style, hyperlinks, hyperlinks_file_link_format, diff --git a/src/parse_style.rs b/src/parse_style.rs index 6b7179a9..cf595553 100644 --- a/src/parse_style.rs +++ b/src/parse_style.rs @@ -37,7 +37,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( + pub fn from_str_with_handling_of_special_decoration_attributes( style_string: &str, default: Option, decoration_style_string: Option<&str>, diff --git a/src/style.rs b/src/style.rs index a15f3eab..350023ca 100644 --- a/src/style.rs +++ b/src/style.rs @@ -73,19 +73,6 @@ impl Style { } } - pub fn decoration_ansi_term_style(&self) -> Option { - match self.decoration_style { - DecorationStyle::Box(style) => Some(style), - DecorationStyle::Underline(style) => Some(style), - DecorationStyle::Overline(style) => Some(style), - DecorationStyle::UnderOverline(style) => Some(style), - DecorationStyle::BoxWithUnderline(style) => Some(style), - DecorationStyle::BoxWithOverline(style) => Some(style), - DecorationStyle::BoxWithUnderOverline(style) => Some(style), - DecorationStyle::NoDecoration => None, - } - } - pub fn is_applied_to(&self, s: &str) -> bool { match ansi::parse_first_style(s) { Some(parsed_style) => ansi_term_style_equality(parsed_style, self.ansi_term_style), diff --git a/src/tests/test_example_diffs.rs b/src/tests/test_example_diffs.rs index b3a8bf36..51c713fe 100644 --- a/src/tests/test_example_diffs.rs +++ b/src/tests/test_example_diffs.rs @@ -1005,7 +1005,7 @@ src/align.rs #[test] fn test_hunk_header_style_with_file() { let config = integration_test_utils::make_config_from_args(&[ - "--file-style", + "--hunk-header-file-style", "yellow", "--hunk-header-style", "file line-number red", @@ -1034,7 +1034,7 @@ src/align.rs:71: impl<'a> Alignment<'a> { │ #[test] fn test_hunk_header_style_with_file_no_frag() { let config = integration_test_utils::make_config_from_args(&[ - "--file-style", + "--hunk-header-file-style", "yellow", "--hunk-header-style", "file line-number red", @@ -1079,6 +1079,8 @@ src/delta.rs:1: │ let config = integration_test_utils::make_config_from_args(&[ "--hunk-header-style", "line-number normal", + "--hunk-header-line-number-style", + "normal", "--hunk-header-decoration-style", "omit", ]); -- cgit v1.2.3