Repository: dandavison/delta
Pull request #430 (2020-12-05 17:38:30)

Fix prefix painting
Always paint prefix with `minus-style` / `plus-style`; previously the prefix was being painted with whatever style the line started with (and thus possibly an emph style).


Patch:
diff --git a/src/delta.rs b/src/delta.rs
index 46eecd6..bbb7a9c 100644
--- a/src/delta.rs
+++ b/src/delta.rs
@@ -456,7 +456,7 @@ fn handle_hunk_header_line(
                 &mut painter.output_buffer,
                 config,
                 &mut None,
-                "",
+                None,
                 None,
                 Some(false),
             );
diff --git a/src/features/side_by_side.rs b/src/features/side_by_side.rs
index 805a61a..f468089 100644
--- a/src/features/side_by_side.rs
+++ b/src/features/side_by_side.rs
@@ -84,9 +84,9 @@ pub fn paint_minus_and_plus_lines_side_by_side<'a>(
             },
             line_numbers_data,
             if config.keep_plus_minus_markers {
-                "-"
+                Some(config.minus_style.paint("-"))
             } else {
-                ""
+                None
             },
             background_color_extends_to_terminal_width,
             config,
@@ -101,9 +101,9 @@ pub fn paint_minus_and_plus_lines_side_by_side<'a>(
             },
             line_numbers_data,
             if config.keep_plus_minus_markers {
-                "+"
+                Some(config.plus_style.paint("+"))
             } else {
-                ""
+                None
             },
             background_color_extends_to_terminal_width,
             config,
@@ -120,7 +120,7 @@ pub fn paint_zero_lines_side_by_side(
     output_buffer: &mut String,
     config: &Config,
     line_numbers_data: &mut Option<&mut line_numbers::LineNumbersData>,
-    prefix: &str,
+    painted_prefix: Option<ansi_term::ANSIString>,
     background_color_extends_to_terminal_width: Option<bool>,
 ) {
     for (line_index, (syntax_sections, diff_sections)) in syntax_style_sections
@@ -134,7 +134,7 @@ pub fn paint_zero_lines_side_by_side(
             state,
             line_numbers_data,
             Some(PanelSide::Left),
-            prefix,
+            painted_prefix.clone(),
             config,
         );
         // TODO: Avoid doing the superimpose_style_sections work twice.
@@ -160,7 +160,7 @@ pub fn paint_zero_lines_side_by_side(
             state,
             line_numbers_data,
             Some(PanelSide::Right),
-            prefix,
+            painted_prefix.clone(),
             config,
         );
         right_fill_right_panel_line(
@@ -184,7 +184,7 @@ fn paint_left_panel_minus_line<'a>(
     diff_style_sections: &[Vec<(Style, &str)>],
     state: &'a State,
     line_numbers_data: &mut Option<&mut line_numbers::LineNumbersData>,
-    prefix: &str,
+    painted_prefix: Option<ansi_term::ANSIString>,
     background_color_extends_to_terminal_width: Option<bool>,
     config: &Config,
 ) -> String {
@@ -195,7 +195,7 @@ fn paint_left_panel_minus_line<'a>(
         state,
         line_numbers_data,
         PanelSide::Left,
-        prefix,
+        painted_prefix,
         config,
     );
     right_pad_left_panel_line(
@@ -218,7 +218,7 @@ fn paint_right_panel_plus_line<'a>(
     diff_style_sections: &[Vec<(Style, &str)>],
     state: &'a State,
     line_numbers_data: &mut Option<&mut line_numbers::LineNumbersData>,
-    prefix: &str,
+    painted_prefix: Option<ansi_term::ANSIString>,
     background_color_extends_to_terminal_width: Option<bool>,
     config: &Config,
 ) -> String {
@@ -229,7 +229,7 @@ fn paint_right_panel_plus_line<'a>(
         state,
         line_numbers_data,
         PanelSide::Right,
-        prefix,
+        painted_prefix,
         config,
     );
     right_fill_right_panel_line(
@@ -301,7 +301,7 @@ fn paint_minus_or_plus_panel_line(
     state: &State,
     line_numbers_data: &mut Option<&mut line_numbers::LineNumbersData>,
     panel_side: PanelSide,
-    prefix: &str,
+    painted_prefix: Option<ansi_term::ANSIString>,
     config: &Config,
 ) -> (String, bool) {
     let (empty_line_syntax_sections, empty_line_diff_sections) = (Vec::new(), Vec::new());
@@ -332,7 +332,7 @@ fn paint_minus_or_plus_panel_line(
         &state_for_line_numbers_field,
         line_numbers_data,
         Some(panel_side),
-        prefix,
+        painted_prefix,
         config,
     );
 
diff --git a/src/paint.rs b/src/paint.rs
index 2758219..bbff5a8 100644
--- a/src/paint.rs
+++ b/src/paint.rs
@@ -164,9 +164,9 @@ impl<'a> Painter<'a> {
                     self.config,
                     &mut Some(&mut self.line_numbers_data),
                     if self.config.keep_plus_minus_markers {
-                        "-"
+                        Some(self.config.minus_style.paint("-"))
                     } else {
-                        ""
+                        None
                     },
                     Some(self.config.minus_empty_line_marker_style),
                     None,
@@ -181,9 +181,9 @@ impl<'a> Painter<'a> {
                     self.config,
                     &mut Some(&mut self.line_numbers_data),
                     if self.config.keep_plus_minus_markers {
-                        "+"
+                        Some(self.config.plus_style.paint("+"))
                     } else {
-                        ""
+                        None
                     },
                     Some(self.config.plus_empty_line_marker_style),
                     None,
@@ -196,10 +196,10 @@ impl<'a> Painter<'a> {
 
     pub fn paint_zero_line(&mut self, line: &str) {
         let state = State::HunkZero;
-        let prefix = if self.config.keep_plus_minus_markers && !line.is_empty() {
-            &line[..1]
+        let painted_prefix = if self.config.keep_plus_minus_markers && !line.is_empty() {
+            Some(self.config.zero_style.paint(&line[..1]))
         } else {
-            ""
+            None
         };
         let lines = vec![(self.prepare(line, true), state.clone())];
         let syntax_style_sections = Painter::get_syntax_style_sections_for_lines(
@@ -218,7 +218,7 @@ impl<'a> Painter<'a> {
                 &mut self.output_buffer,
                 self.config,
                 &mut Some(&mut self.line_numbers_data),
-                prefix,
+                painted_prefix,
                 None,
             );
         } else {
@@ -229,7 +229,7 @@ impl<'a> Painter<'a> {
                 &mut self.output_buffer,
                 self.config,
                 &mut Some(&mut self.line_numbers_data),
-                prefix,
+                painted_prefix,
                 None,
                 None,
             );
@@ -246,14 +246,13 @@ impl<'a> Painter<'a> {
         output_buffer: &mut String,
         config: &config::Config,
         line_numbers_data: &mut Option<&mut line_numbers::LineNumbersData>,
-        prefix: &str,
+        painted_prefix: Option<ansi_term::ANSIString>,
         empty_line_style: Option<Style>, // a style with background color to highlight an empty line
         background_color_extends_to_terminal_width: Option<bool>,
     ) {
         // There's some unfortunate hackery going on here for two reasons:
         //
-        // 1. The prefix needs to be injected into the output stream. We paint
-        //    this with whatever style the line starts with.
+        // 1. The prefix needs to be injected into the output stream.
         //
         // 2. We must ensure that we fill rightwards with the appropriate
         //    non-emph background color. In that case we don't use the last
@@ -269,7 +268,7 @@ impl<'a> Painter<'a> {
                 state,
                 line_numbers_data,
                 None,
-                prefix,
+                painted_prefix.clone(),
                 config,
             );
             let (should_right_fill_background_color, fill_style) =
@@ -380,7 +379,7 @@ impl<'a> Painter<'a> {
         state: &State,
         line_numbers_data: &mut Option<&mut line_numbers::LineNumbersData>,
         side_by_side_panel: Option<side_by_side::PanelSide>,
-        prefix: &str,
+        painted_prefix: Option<ansi_term::ANSIString>,
         config: &config::Config,
     ) -> (String, bool) {
         let output_line_numbers = config.line_numbers && line_numbers_data.is_some();
@@ -417,8 +416,8 @@ impl<'a> Painter<'a> {
             config.null_syntect_style,
         ) {
             if !handled_prefix {
-                if prefix != "" {
-                    ansi_strings.push(section_style.paint(prefix));
+                if let Some(painted_prefix) = painted_prefix.clone() {
+                    ansi_strings.push(painted_prefix);
                 }
                 if !text.is_empty() {
                     text.remove(0);


Test patch:
diff --git a/src/tests/ansi_test_utils.rs b/src/tests/ansi_test_utils.rs
index 1d5c8f2..d0456d9 100644
--- a/src/tests/ansi_test_utils.rs
+++ b/src/tests/ansi_test_utils.rs
@@ -145,7 +145,7 @@ pub mod ansi_test_utils {
             &mut output_buffer,
             config,
             &mut None,
-            prefix,
+            Some(config.null_style.paint(prefix)),
             None,
             None,
         );