diff options
author | sharkdp <davidpeter@web.de> | 2018-12-16 20:43:36 +0100 |
---|---|---|
committer | sharkdp <davidpeter@web.de> | 2018-12-16 20:43:36 +0100 |
commit | a236a9b1955e78cd4d7cb61e7fa72e249ec7915c (patch) | |
tree | a3242a966b614ab74f93455be83c556086ac64fe | |
parent | b68c5d857688201db44a188cbd89c93f9841ea7f (diff) | |
parent | cea05e9f22b40edf2ccd2a0676827974796972a2 (diff) |
Merge branch 'master' of https://github.com/tskinn/bat into tskinn-master
-rw-r--r-- | src/app.rs | 8 | ||||
-rw-r--r-- | src/clap_app.rs | 11 | ||||
-rw-r--r-- | src/printer.rs | 19 | ||||
-rw-r--r-- | src/terminal.rs | 4 |
4 files changed, 38 insertions, 4 deletions
@@ -77,7 +77,11 @@ pub struct Config<'a> { /// Command to start the pager pub pager: Option<&'a str>, + /// Whether or not to use ANSI italics pub use_italic_text: bool, + + /// A line to highlight + pub highlight_line: Option<usize>, } fn is_truecolor_terminal() -> bool { @@ -264,6 +268,10 @@ impl App { Some("always") => true, _ => false, }, + highlight_line: self + .matches + .value_of("highlight-line") + .and_then(|w| w.parse().ok()), }) } diff --git a/src/clap_app.rs b/src/clap_app.rs index 1fba4b1a..dea5cb59 100644 --- a/src/clap_app.rs +++ b/src/clap_app.rs @@ -189,6 +189,17 @@ 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) + .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") .overrides_with("color") diff --git a/src/printer.rs b/src/printer.rs index 0cf63a7c..3c449494 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<LineChanges>, highlighter: Option<HighlightLines<'a>>, syntax_set: &'a SyntaxSet, + background_highlight: Option<Color>, } 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, } } @@ -301,6 +306,12 @@ 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; @@ -313,7 +324,7 @@ impl<'a> Printer for InteractivePrinter<'a> { write!( handle, "{}", - as_terminal_escaped(style, text_trimmed, true_color, colored_output, italics,) + as_terminal_escaped(style, text_trimmed, true_color, colored_output, italics, background) )?; write!(handle, "{}", &text[text_trimmed.len()..])?; } @@ -370,7 +381,8 @@ impl<'a> Printer for InteractivePrinter<'a> { ), self.config.true_color, self.config.colored_output, - self.config.use_italic_text + self.config.use_italic_text, + background ) )?; break; @@ -410,7 +422,8 @@ impl<'a> Printer for InteractivePrinter<'a> { ), self.config.true_color, self.config.colored_output, - self.config.use_italic_text + self.config.use_italic_text, + background ), panel_wrap.clone().unwrap() )?; diff --git a/src/terminal.rs b/src/terminal.rs index eeb15cbd..f01e9d60 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -19,8 +19,9 @@ pub fn as_terminal_escaped( true_color: bool, colored: bool, italics: bool, + background_color: Option<highlighting::Color>, ) -> String { - let style = if !colored { + let mut style = if !colored { Style::default() } else { let color = to_ansi_color(style.foreground, true_color); @@ -36,5 +37,6 @@ pub fn as_terminal_escaped( } }; + style.background = background_color.map(|c| to_ansi_color(c, true_color)); style.paint(text).to_string() } |