From 6b92814ea0009d173060546dc6b459ef1aac238f Mon Sep 17 00:00:00 2001 From: sharkdp Date: Sun, 16 Dec 2018 21:00:18 +0100 Subject: [PATCH] Allow for multiple highlighted lines --- src/app.rs | 11 ++++++----- src/clap_app.rs | 6 +++--- src/printer.rs | 38 ++++++++++++++++++++++++++------------ 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/app.rs b/src/app.rs index 6ed7715a..e7ece7ef 100644 --- a/src/app.rs +++ b/src/app.rs @@ -80,8 +80,8 @@ pub struct Config<'a> { /// Whether or not to use ANSI italics pub use_italic_text: bool, - /// A line to highlight - pub highlight_line: Option, + /// Lines to highlight + pub highlight_lines: Vec, } fn is_truecolor_terminal() -> bool { @@ -268,10 +268,11 @@ impl App { Some("always") => true, _ => false, }, - highlight_line: self + highlight_lines: self .matches - .value_of("highlight-line") - .and_then(|w| w.parse().ok()), + .values_of("highlight-line") + .and_then(|ws| ws.map(|w| w.parse().ok()).collect()) + .unwrap_or_default(), }) } diff --git a/src/clap_app.rs b/src/clap_app.rs index dea5cb59..9f6f0997 100644 --- a/src/clap_app.rs +++ b/src/clap_app.rs @@ -191,12 +191,12 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> { .arg( Arg::with_name("highlight-line") .long("highlight-line") - .overrides_with("highlight-line") .takes_value(true) + .multiple(true) .value_name("N") - .help("Highlight a line.") + .help("Highlight the given line.") .long_help( - "Highlight the Nth line. The background color is changed to create contrast.", + "Highlight the N-th line with a different background color", ), ) .arg( diff --git a/src/printer.rs b/src/printer.rs index 3c449494..e66612e0 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -80,7 +80,7 @@ pub struct InteractivePrinter<'a> { pub line_changes: Option, highlighter: Option>, syntax_set: &'a SyntaxSet, - background_highlight: Option, + background_color_highlight: Option, } impl<'a> InteractivePrinter<'a> { @@ -92,7 +92,7 @@ impl<'a> InteractivePrinter<'a> { ) -> Self { let theme = assets.get_theme(&config.theme); - let background_highlight = theme.settings.line_highlight; + let background_color_highlight = theme.settings.line_highlight; let colors = if config.colored_output { Colors::colored(theme, config.true_color) @@ -160,7 +160,7 @@ impl<'a> InteractivePrinter<'a> { line_changes, highlighter, syntax_set: &assets.syntax_set, - background_highlight, + background_color_highlight, } } @@ -292,6 +292,19 @@ impl<'a> Printer for InteractivePrinter<'a> { let mut cursor_total: usize = 0; let mut panel_wrap: Option = None; + // Line highlighting + let background_color = if self + .config + .highlight_lines + .iter() + .find(|&&l| l == line_number) + .is_some() + { + self.background_color_highlight + } else { + None + }; + // Line decorations. if self.panel_width > 0 { let decorations = self @@ -306,12 +319,6 @@ impl<'a> Printer for InteractivePrinter<'a> { } } - // Line highlighting - let background = self.config.highlight_line - .filter(|line| *line == line_number) - .map(|_| self.background_highlight) - .unwrap(); - // Line contents. if self.config.output_wrap == OutputWrap::None { let true_color = self.config.true_color; @@ -324,7 +331,14 @@ impl<'a> Printer for InteractivePrinter<'a> { write!( handle, "{}", - as_terminal_escaped(style, text_trimmed, true_color, colored_output, italics, background) + as_terminal_escaped( + style, + text_trimmed, + true_color, + colored_output, + italics, + background_color + ) )?; write!(handle, "{}", &text[text_trimmed.len()..])?; } @@ -382,7 +396,7 @@ impl<'a> Printer for InteractivePrinter<'a> { self.config.true_color, self.config.colored_output, self.config.use_italic_text, - background + background_color ) )?; break; @@ -423,7 +437,7 @@ impl<'a> Printer for InteractivePrinter<'a> { self.config.true_color, self.config.colored_output, self.config.use_italic_text, - background + background_color ), panel_wrap.clone().unwrap() )?;