diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-12-21 21:26:57 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-21 21:26:57 +0000 |
commit | a489f4a2e31f7b19c062d8fbf5a6a80e72b2c7a7 (patch) | |
tree | 19261671671f088921e5396dcdedd2d8149287bc | |
parent | a52e36d521fa03cffc457a031bd08340f6318767 (diff) | |
parent | 334dec742d78d6d72715eaefaaa1c8313c6315fb (diff) |
Merge pull request #453 from dandavison/309-file-path-in-hunk-header
Add file path to hunk header
-rw-r--r-- | src/cli.rs | 3 | ||||
-rw-r--r-- | src/config.rs | 5 | ||||
-rw-r--r-- | src/delta.rs | 10 | ||||
-rw-r--r-- | src/parse_style.rs | 2 | ||||
-rw-r--r-- | src/tests/test_example_diffs.rs | 21 |
5 files changed, 39 insertions, 2 deletions
@@ -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<String, GitConfigEntry>, 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<cli::Opt> 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 adcb40ae..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, @@ -509,7 +517,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(); 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 @@ -1009,6 +1009,27 @@ src/align.rs } #[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(&[ "--color-only", |