From 1a6709c2cb3d8403e2f709a7e8b66c031216e559 Mon Sep 17 00:00:00 2001 From: Taylor Date: Tue, 9 Oct 2018 22:25:33 -0600 Subject: [PATCH 1/7] add line highlight --- src/app.rs | 4 ++++ src/clap_app.rs | 11 +++++++++++ src/printer.rs | 21 ++++++++++++++++++++- src/terminal.rs | 4 +++- 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/app.rs b/src/app.rs index 91a87975..360c7304 100644 --- a/src/app.rs +++ b/src/app.rs @@ -64,6 +64,9 @@ pub struct Config<'a> { /// The syntax highlighting theme pub theme: String, + + /// A line to highlight + pub highlight_line: Option, } fn is_truecolor_terminal() -> bool { @@ -182,6 +185,7 @@ impl App { .or_else(|| env::var("BAT_THEME").ok()) .unwrap_or(String::from(BAT_THEME_DEFAULT)), line_range: transpose(self.matches.value_of("line-range").map(LineRange::from))?, + highlight_line: self.matches.value_of("highlight-line").and_then(|w| w.parse().ok()), output_components, }) } diff --git a/src/clap_app.rs b/src/clap_app.rs index ab1881db..11d79b51 100644 --- a/src/clap_app.rs +++ b/src/clap_app.rs @@ -141,6 +141,17 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> { '--line-range 40:' prints lines 40 to the end of the file", ), ) + .arg( + Arg::with_name("highlight-line") + .long("highlight-line") + .overrides_with("highlight-line") + .takes_value(true) + .value_name("n") + .help("Highlight a line.") + .long_help( + "Highlight the nth line. The background color is changed to create contrast.", + ), + ) .arg( Arg::with_name("color") .long("color") diff --git a/src/printer.rs b/src/printer.rs index 5bb72fc8..03f01f97 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -7,6 +7,7 @@ use ansi_term::Style; use console::AnsiCodeIterator; use syntect::easy::HighlightLines; +use syntect::highlighting::Color; use syntect::highlighting::Theme; use syntect::parsing::SyntaxSet; @@ -79,6 +80,7 @@ pub struct InteractivePrinter<'a> { pub line_changes: Option, highlighter: Option>, syntax_set: &'a SyntaxSet, + background_highlight: Option, } impl<'a> InteractivePrinter<'a> { @@ -90,6 +92,8 @@ impl<'a> InteractivePrinter<'a> { ) -> Self { let theme = assets.get_theme(&config.theme); + let background_highlight = theme.settings.line_highlight; + let colors = if config.colored_output { Colors::colored(theme, config.true_color) } else { @@ -156,6 +160,7 @@ impl<'a> InteractivePrinter<'a> { line_changes, highlighter, syntax_set: &assets.syntax_set, + background_highlight, } } @@ -296,6 +301,18 @@ impl<'a> Printer for InteractivePrinter<'a> { } } + // Line highlighting + let background = match self.config.highlight_line { + Some(line) => { + if line_number == line { + self.background_highlight + } else { + None + } + } + _ => None + }; + // Line contents. if self.config.output_wrap == OutputWrap::None { let true_color = self.config.true_color; @@ -306,7 +323,7 @@ impl<'a> Printer for InteractivePrinter<'a> { write!( handle, "{}", - as_terminal_escaped(style, &*text, true_color, colored_output,) + as_terminal_escaped(style, &*text, true_color, colored_output, background) )?; } @@ -362,6 +379,7 @@ impl<'a> Printer for InteractivePrinter<'a> { ), self.config.true_color, self.config.colored_output, + background ) )?; break; @@ -401,6 +419,7 @@ impl<'a> Printer for InteractivePrinter<'a> { ), self.config.true_color, self.config.colored_output, + background ), panel_wrap.clone().unwrap() )?; diff --git a/src/terminal.rs b/src/terminal.rs index 7a6850ca..b634357a 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -18,8 +18,9 @@ pub fn as_terminal_escaped( text: &str, true_color: bool, colored: bool, + background_color: Option, ) -> String { - let style = if !colored { + let mut style = if !colored { Style::default() } else { let color = to_ansi_color(style.foreground, true_color); @@ -35,5 +36,6 @@ pub fn as_terminal_escaped( } }; + style.background = background_color.map(|c| to_ansi_color(c, true_color)); style.paint(text).to_string() } From 2a7851530dbf37ade9e5493c021d34fb4f3e700b Mon Sep 17 00:00:00 2001 From: sharkdp Date: Wed, 10 Oct 2018 20:45:10 +0200 Subject: [PATCH 2/7] Fix formatting --- src/app.rs | 5 ++++- src/printer.rs | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/app.rs b/src/app.rs index 360c7304..8fdb61c3 100644 --- a/src/app.rs +++ b/src/app.rs @@ -185,7 +185,10 @@ impl App { .or_else(|| env::var("BAT_THEME").ok()) .unwrap_or(String::from(BAT_THEME_DEFAULT)), line_range: transpose(self.matches.value_of("line-range").map(LineRange::from))?, - highlight_line: self.matches.value_of("highlight-line").and_then(|w| w.parse().ok()), + highlight_line: self + .matches + .value_of("highlight-line") + .and_then(|w| w.parse().ok()), output_components, }) } diff --git a/src/printer.rs b/src/printer.rs index 03f01f97..68b2a998 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -309,8 +309,8 @@ impl<'a> Printer for InteractivePrinter<'a> { } else { None } - } - _ => None + } + _ => None, }; // Line contents. From cea05e9f22b40edf2ccd2a0676827974796972a2 Mon Sep 17 00:00:00 2001 From: Taylor Date: Wed, 10 Oct 2018 22:19:40 -0600 Subject: [PATCH 3/7] be consistent --- src/clap_app.rs | 4 ++-- src/printer.rs | 14 ++++---------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/clap_app.rs b/src/clap_app.rs index 11d79b51..36a4b60f 100644 --- a/src/clap_app.rs +++ b/src/clap_app.rs @@ -146,10 +146,10 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> { .long("highlight-line") .overrides_with("highlight-line") .takes_value(true) - .value_name("n") + .value_name("N") .help("Highlight a line.") .long_help( - "Highlight the nth line. The background color is changed to create contrast.", + "Highlight the Nth line. The background color is changed to create contrast.", ), ) .arg( diff --git a/src/printer.rs b/src/printer.rs index 68b2a998..dd4abdd9 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -302,16 +302,10 @@ impl<'a> Printer for InteractivePrinter<'a> { } // Line highlighting - let background = match self.config.highlight_line { - Some(line) => { - if line_number == line { - self.background_highlight - } else { - None - } - } - _ => None, - }; + 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 { From 6b92814ea0009d173060546dc6b459ef1aac238f Mon Sep 17 00:00:00 2001 From: sharkdp Date: Sun, 16 Dec 2018 21:00:18 +0100 Subject: [PATCH 4/7] 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() )?; From cf7ed042c170ba41b5ad582271fdf24d2d5c1279 Mon Sep 17 00:00:00 2001 From: sharkdp Date: Sun, 16 Dec 2018 21:53:15 +0100 Subject: [PATCH 5/7] Colorize the whole line --- src/printer.rs | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/src/printer.rs b/src/printer.rs index e66612e0..0c549feb 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -293,17 +293,15 @@ impl<'a> Printer for InteractivePrinter<'a> { let mut panel_wrap: Option = None; // Line highlighting - let background_color = if self + let highlight_this_line = self .config .highlight_lines .iter() - .find(|&&l| l == line_number) - .is_some() - { - self.background_color_highlight - } else { - None - }; + .any(|&l| l == line_number); + + let background_color = self + .background_color_highlight + .filter(|_| highlight_this_line); // Line decorations. if self.panel_width > 0 { @@ -340,7 +338,20 @@ impl<'a> Printer for InteractivePrinter<'a> { background_color ) )?; - write!(handle, "{}", &text[text_trimmed.len()..])?; + + if text.len() != text_trimmed.len() { + if let Some(background_color) = background_color { + let mut ansi_style = Style::default(); + ansi_style.background = Some(to_ansi_color(background_color, true_color)); + let width = if cursor_total <= cursor_max { + cursor_max - cursor_total + 1 + } else { + 0 + }; + write!(handle, "{}", ansi_style.paint(" ".repeat(width)))?; + } + write!(handle, "{}", &text[text_trimmed.len()..])?; + } } if line.bytes().next_back() != Some(b'\n') { @@ -450,6 +461,17 @@ impl<'a> Printer for InteractivePrinter<'a> { } } + if let Some(background_color) = background_color { + let mut ansi_style = Style::default(); + ansi_style.background = + Some(to_ansi_color(background_color, self.config.true_color)); + + write!( + handle, + "{}", + ansi_style.paint(" ".repeat(cursor_max - cursor)) + )?; + } write!(handle, "\n")?; } From c2847f6a9f886473bf7a2b5bb4742fcee4e8cc7a Mon Sep 17 00:00:00 2001 From: sharkdp Date: Sun, 16 Dec 2018 22:17:39 +0100 Subject: [PATCH 6/7] Short options for line-range and highlight-line --- src/clap_app.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/clap_app.rs b/src/clap_app.rs index 9f6f0997..e9f9d3a0 100644 --- a/src/clap_app.rs +++ b/src/clap_app.rs @@ -175,6 +175,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> { .arg( Arg::with_name("line-range") .long("line-range") + .short("r") .multiple(true) .takes_value(true) .number_of_values(1) @@ -191,6 +192,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> { .arg( Arg::with_name("highlight-line") .long("highlight-line") + .short("H") .takes_value(true) .multiple(true) .value_name("N") From bd6868453705ba27b012b6909dac0d6d7fb204a3 Mon Sep 17 00:00:00 2001 From: sharkdp Date: Sun, 16 Dec 2018 22:35:22 +0100 Subject: [PATCH 7/7] Fix the number of values to one --- src/clap_app.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/clap_app.rs b/src/clap_app.rs index e9f9d3a0..abecb68a 100644 --- a/src/clap_app.rs +++ b/src/clap_app.rs @@ -194,6 +194,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> { .long("highlight-line") .short("H") .takes_value(true) + .number_of_values(1) .multiple(true) .value_name("N") .help("Highlight the given line.")