From 31066fc8803e47e5f992c53ab611e8077093693a Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Sat, 19 Dec 2020 12:32:30 +0000 Subject: Do not paint hunk header twice The text being passed to the draw function has already been painted. --- src/delta.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/delta.rs b/src/delta.rs index adcb40ae..de9d68e2 100644 --- a/src/delta.rs +++ b/src/delta.rs @@ -509,7 +509,7 @@ fn handle_hunk_header_line( &painter.output_buffer, &painter.output_buffer, &config.decorations_width, - config.hunk_header_style, + config.null_style, decoration_ansi_term_style, )?; painter.output_buffer.clear(); -- cgit v1.2.3 From 3c31223dbfcbcb32c3c2da990091cc65e9f6eca1 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Sat, 19 Dec 2020 12:35:17 +0000 Subject: Add failing test of hunk-header-style 'file' attribute Ref #309 --- src/parse_style.rs | 2 ++ src/tests/test_example_diffs.rs | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/parse_style.rs b/src/parse_style.rs index 565defa8..1fa4fb03 100644 --- a/src/parse_style.rs +++ b/src/parse_style.rs @@ -241,6 +241,8 @@ fn parse_ansi_term_style( style.is_strikethrough = true; } else if word == "ul" || word == "underline" { style.is_underline = true; + } else if word == "file" { + // Allow: this is meaningful in hunk-header-style. } else if !seen_foreground { if word == "syntax" { is_syntax_highlighted = true; diff --git a/src/tests/test_example_diffs.rs b/src/tests/test_example_diffs.rs index f4121588..72c4c5f0 100644 --- a/src/tests/test_example_diffs.rs +++ b/src/tests/test_example_diffs.rs @@ -1008,6 +1008,27 @@ src/align.rs assert!(output.contains("@@ -71,11 +71,8 @@ impl<'a> Alignment<'a> {")); } + #[test] + fn test_hunk_header_style_with_file() { + let config = integration_test_utils::make_config_from_args(&[ + "--file-style", + "yellow", + "--hunk-header-style", + "file red", + ]); + let output = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, &config); + + ansi_test_utils::assert_line_has_style( + &output, + 11, + "src/align.rs: impl<'a> Alignment<'a> {", + "yellow", + &config, + ); + let output = strip_ansi_codes(&output); + assert!(output.contains("src/align.rs: impl<'a> Alignment<'a> {")); + } + #[test] fn test_commit_style_with_color_only_has_style() { let config = integration_test_utils::make_config_from_args(&[ -- cgit v1.2.3 From 334dec742d78d6d72715eaefaaa1c8313c6315fb Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Sat, 19 Dec 2020 12:36:07 +0000 Subject: Implement hunk-header-style 'file' attribute Fixes #309 --- src/cli.rs | 3 ++- src/config.rs | 5 +++++ src/delta.rs | 8 ++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/cli.rs b/src/cli.rs index 41483a99..22e3de2b 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -365,7 +365,8 @@ pub struct Opt { #[structopt(long = "hunk-header-style", default_value = "syntax")] /// Style (foreground, background, attributes) for the hunk-header. See STYLES section. The - /// style 'omit' can be used to remove the hunk header section from the output. + /// special attribute 'file' can be used to include the file path in the hunk header. The style + /// 'omit' can be used to remove the hunk header section from the output. pub hunk_header_style: String, #[structopt(long = "hunk-header-decoration-style", default_value = "blue box")] diff --git a/src/config.rs b/src/config.rs index 28e571d6..6d47ac59 100644 --- a/src/config.rs +++ b/src/config.rs @@ -32,6 +32,7 @@ pub struct Config { pub file_style: Style, pub git_config_entries: HashMap, pub hunk_header_style: Style, + pub hunk_header_style_include_file_path: bool, pub hyperlinks: bool, pub hyperlinks_file_link_format: String, pub inspect_raw_lines: cli::InspectRawLines, @@ -163,6 +164,10 @@ impl From for Config { file_style, git_config_entries: opt.git_config_entries, hunk_header_style, + hunk_header_style_include_file_path: opt + .hunk_header_style + .split(' ') + .any(|s| s == "file"), hyperlinks: opt.hyperlinks, hyperlinks_file_link_format: opt.hyperlinks_file_link_format, inspect_raw_lines: opt.computed.inspect_raw_lines, diff --git a/src/delta.rs b/src/delta.rs index de9d68e2..e401e269 100644 --- a/src/delta.rs +++ b/src/delta.rs @@ -1,4 +1,5 @@ use std::borrow::Cow; +use std::fmt::Write as FmtWrite; use std::io::BufRead; use std::io::Write; @@ -485,6 +486,13 @@ fn handle_hunk_header_line( writeln!(painter.writer)?; } if !line.is_empty() { + if config.hunk_header_style_include_file_path { + let _ = write!( + &mut painter.output_buffer, + "{}: ", + config.file_style.paint(plus_file) + ); + }; let lines = vec![(line, State::HunkHeader)]; let syntax_style_sections = Painter::get_syntax_style_sections_for_lines( &lines, -- cgit v1.2.3