diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-05-09 12:32:01 -0400 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2020-05-16 09:04:38 -0400 |
commit | 79979c272d6daa876ffb3dbad7a6550c22155c8c (patch) | |
tree | 30baf82bd2892ffb73bfd3f6dab6ce21b6ae6ebd /src/paint.rs | |
parent | 21537d6eb8f07d6c1e71c757fb8f0fd583449219 (diff) |
Hunk highlighting test and refactoring
- New command-line option --syntax highlight to select which category
of hunk lines to be syntax highlighted. --highlight-removed is now
deprecated.
- New hunk highlighting test tests syntax highlighting and background colors
- Refactor: construction of style modifiers for config struct
- Refactor: compute minus and plus foreground style sections separately
- Refactor: foreground style logic
- Refactor: rearrange style modifier construction for config
- Refactor: change variable name: => highlight_minus_lines
- Refactor: clean up --color-only config implementation
Diffstat (limited to 'src/paint.rs')
-rw-r--r-- | src/paint.rs | 95 |
1 files changed, 62 insertions, 33 deletions
diff --git a/src/paint.rs b/src/paint.rs index 9f220ae0..b702ebe9 100644 --- a/src/paint.rs +++ b/src/paint.rs @@ -9,12 +9,13 @@ use syntect::parsing::{SyntaxReference, SyntaxSet}; use crate::bat::assets::HighlightingAssets; use crate::bat::terminal::to_ansi_color; use crate::config; +use crate::delta::State; use crate::edits; use crate::paint::superimpose_style_sections::superimpose_style_sections; use crate::style; -const ANSI_CSI_ERASE_IN_LINE: &str = "\x1b[K"; -const ANSI_SGR_RESET: &str = "\x1b[0m"; +pub const ANSI_CSI_ERASE_IN_LINE: &str = "\x1b[K"; +pub const ANSI_SGR_RESET: &str = "\x1b[0m"; pub struct Painter<'a> { pub minus_lines: Vec<String>, @@ -63,13 +64,30 @@ impl<'a> Painter<'a> { } pub fn paint_buffered_lines(&mut self) { - let (minus_line_syntax_style_sections, plus_line_syntax_style_sections) = - Self::get_syntax_style_sections( - &self.minus_lines, - &self.plus_lines, - &mut self.highlighter, - self.config, - ); + let minus_fixed_foreground_style = self.get_fixed_foreground_style( + self.config.minus_foreground_style_modifier, + self.config + .lines_to_be_syntax_highlighted + .contains(State::HunkMinus as usize), + ); + let plus_fixed_foreground_style = self.get_fixed_foreground_style( + self.config.plus_foreground_style_modifier, + self.config + .lines_to_be_syntax_highlighted + .contains(State::HunkPlus as usize), + ); + let minus_line_syntax_style_sections = Self::get_syntax_style_sections_for_lines( + &self.minus_lines, + &mut self.highlighter, + minus_fixed_foreground_style, + self.config, + ); + let plus_line_syntax_style_sections = Self::get_syntax_style_sections_for_lines( + &self.plus_lines, + &mut self.highlighter, + plus_fixed_foreground_style, + self.config, + ); let (minus_line_diff_style_sections, plus_line_diff_style_sections) = Self::get_diff_style_sections(&self.minus_lines, &self.plus_lines, self.config); // TODO: lines and style sections contain identical line text @@ -99,6 +117,22 @@ impl<'a> Painter<'a> { self.plus_lines.clear(); } + fn get_fixed_foreground_style( + &self, + foreground_style_modifier: Option<StyleModifier>, + should_syntax_highlight: bool, + ) -> Option<Style> { + match ( + foreground_style_modifier, + should_syntax_highlight, + self.config.theme, + ) { + (Some(style_modifier), _, _) => Some(self.config.no_style.apply(style_modifier)), + (_, true, Some(_)) => None, + _ => Some(self.config.no_style), + } + } + /// Superimpose background styles and foreground syntax /// highlighting styles, and write colored lines to output buffer. pub fn paint_lines( @@ -161,44 +195,33 @@ impl<'a> Painter<'a> { Ok(()) } - /// Perform syntax highlighting for minus and plus lines in buffer. - fn get_syntax_style_sections<'m, 'p>( - minus_lines: &'m [String], - plus_lines: &'p [String], + fn get_syntax_style_sections_for_lines<'s>( + lines: &'s [String], highlighter: &mut HighlightLines, + fixed_style: Option<Style>, config: &config::Config, - ) -> (Vec<Vec<(Style, &'m str)>>, Vec<Vec<(Style, &'p str)>>) { - let mut minus_line_sections = Vec::new(); - for line in minus_lines.iter() { - minus_line_sections.push(Painter::get_line_syntax_style_sections( - &line, - highlighter, - &config, - config.highlight_removed, - )); - } - let mut plus_line_sections = Vec::new(); - for line in plus_lines.iter() { - plus_line_sections.push(Painter::get_line_syntax_style_sections( + ) -> Vec<Vec<(Style, &'s str)>> { + let mut line_sections = Vec::new(); + for line in lines.iter() { + line_sections.push(Painter::get_line_syntax_style_sections( &line, highlighter, + fixed_style, &config, - true, )); } - (minus_line_sections, plus_line_sections) + line_sections } pub fn get_line_syntax_style_sections( line: &'a str, highlighter: &mut HighlightLines, + fixed_style: Option<Style>, config: &config::Config, - should_syntax_highlight: bool, ) -> Vec<(Style, &'a str)> { - if should_syntax_highlight && config.theme.is_some() { - highlighter.highlight(line, &config.syntax_set) - } else { - vec![(config.no_style, line)] + match fixed_style { + Some(fixed_style) => vec![(fixed_style, line)], + None => highlighter.highlight(line, &config.syntax_set), } } @@ -249,6 +272,12 @@ pub fn paint_text_foreground(text: &str, color: Color, true_color: bool) -> Stri to_ansi_color(color, true_color).paint(text).to_string() } +#[allow(dead_code)] +pub fn paint_text_background(text: &str, color: Color, true_color: bool) -> String { + let style = ansi_term::Style::new().on(to_ansi_color(color, true_color)); + style.paint(text).to_string() +} + // See // https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit pub fn ansi_color_name_to_number(name: &str) -> Option<u8> { |