diff --git a/src/handlers/hunk_header.rs b/src/handlers/hunk_header.rs index d1b4706d2..ce986b26a 100644 --- a/src/handlers/hunk_header.rs +++ b/src/handlers/hunk_header.rs @@ -27,8 +27,7 @@ use regex::Regex; use super::draw; use crate::config::Config; use crate::delta::{self, State, StateMachine}; -use crate::features; -use crate::paint::{BgShouldFill, Painter, StyleSectionSpecifier}; +use crate::paint::{self, BgShouldFill, Painter, StyleSectionSpecifier}; use crate::style::DecorationStyle; impl<'a> StateMachine<'a> { @@ -173,7 +172,9 @@ fn write_hunk_header( "".to_string() }; - let file_with_line_number = get_painted_file_with_line_number(line_numbers, plus_file, config); + let plus_line_number = line_numbers[line_numbers.len() - 1].0; + let file_with_line_number = + paint_file_path_with_line_number(Some(plus_line_number), plus_file, config); if !line.is_empty() || !file_with_line_number.is_empty() { write_to_output_buffer(&file_with_line_number, line, painter, config); @@ -191,41 +192,35 @@ fn write_hunk_header( Ok(()) } -fn get_painted_file_with_line_number( - line_numbers: &[(usize, usize)], +fn paint_file_path_with_line_number( + line_number: Option, plus_file: &str, config: &Config, ) -> String { - 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.hunk_header_file_style.paint(plus_file)) + let file_style = if config.hunk_header_style_include_file_path { + Some(config.hunk_header_file_style) + } else { + None }; - if config.hunk_header_style_include_line_number + let line_number_style = if config.hunk_header_style_include_line_number && !config.hunk_header_style.is_raw && !config.color_only + && line_number.is_some() { - if !file_with_line_number.is_empty() { - file_with_line_number.push(ansi_term::ANSIString::from(":")); - } - 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 && !file_with_line_number.is_empty() { - features::hyperlinks::format_osc8_file_hyperlink( - plus_file, - Some(plus_line_number), - &file_with_line_number, - config, - ) - .into() + Some(config.hunk_header_line_number_style) } else { - file_with_line_number - } + None + }; + + paint::paint_file_path_with_line_number( + line_number, + plus_file, + false, + false, + file_style, + line_number_style, + config, + ) } fn write_to_output_buffer( @@ -321,7 +316,7 @@ pub mod tests { fn test_get_painted_file_with_line_number_default() { let cfg = integration_test_utils::make_config_from_args(&[]); - let result = get_painted_file_with_line_number(&vec![(3, 4)], "some-file", &cfg); + let result = get_painted_file_with_line_number(Some(3), "some-file", &cfg); assert_eq!(result, "\u{1b}[34m3\u{1b}[0m"); } @@ -330,7 +325,7 @@ pub mod tests { fn test_get_painted_file_with_line_number_hyperlinks() { let cfg = integration_test_utils::make_config_from_args(&["--features", "hyperlinks"]); - let result = get_painted_file_with_line_number(&vec![(3, 4)], "some-file", &cfg); + let result = get_painted_file_with_line_number(Some(3), "some-file", &cfg); assert_eq!(result, "some-file"); } @@ -344,7 +339,7 @@ pub mod tests { "omit", ]); - let result = get_painted_file_with_line_number(&vec![(3, 4)], "some-file", &cfg); + let result = get_painted_file_with_line_number(Some(3), "some-file", &cfg); assert_eq!(result, ""); } @@ -360,7 +355,7 @@ pub mod tests { "hyperlinks", ]); - let result = get_painted_file_with_line_number(&vec![(3, 4)], "some-file", &cfg); + let result = get_painted_file_with_line_number(Some(3), "some-file", &cfg); assert_eq!(result, ""); } @@ -375,7 +370,7 @@ pub mod tests { "--navigate", ]); - let result = get_painted_file_with_line_number(&vec![(3, 4)], "δ some-file", &cfg); + let result = get_painted_file_with_line_number(Some(3), "δ some-file", &cfg); assert_eq!(result, ""); } diff --git a/src/paint.rs b/src/paint.rs index 848f852d2..3a923e8e2 100644 --- a/src/paint.rs +++ b/src/paint.rs @@ -7,9 +7,10 @@ use syntect::parsing::{SyntaxReference, SyntaxSet}; use unicode_segmentation::UnicodeSegmentation; use crate::ansi; -use crate::config::{self, delta_unreachable}; +use crate::config::{self, delta_unreachable, Config}; use crate::delta::State; use crate::edits; +use crate::features::hyperlinks; use crate::features::line_numbers; use crate::features::side_by_side::ansifill; use crate::features::side_by_side::{self, available_line_width, LineSegments, PanelSide}; @@ -749,6 +750,60 @@ impl<'p> Painter<'p> { } } +pub fn paint_file_path_with_line_number( + line_number: Option, + plus_file: &str, + pad_line_number: bool, + terminate_with_colon: bool, + file_style: Option