on master help sign in
Merge branch 'master' of https://github.com/tskinn/bat into tskinn-master
commita236a9b1955e78cd4d7cb61e7fa72e249ec7915c authorsharkdp <davidpeter@web.de> dateSun 16 Dec 2018, 20:43 (8 years ago) parentb68c5d8cea05e9
4 files changed+38−4
src/app.rs+8 src/clap_app.rs+11 src/printer.rs+16−3 src/terminal.rs+3−1
src/app.rs+8
@@ -77,7 +77,11 @@ pub struct Config<'a> {
7777 /// Command to start the pager
7878 pub pager: Option<&'a str>,
7979
80+ /// Whether or not to use ANSI italics
8081 pub use_italic_text: bool,
82+
83+ /// A line to highlight
84+ pub highlight_line: Option<usize>,
8185}
8286
8387fn is_truecolor_terminal() -> bool {
@@ -264,6 +268,10 @@ impl App {
264268 Some("always") => true,
265269 _ => false,
266270 },
271+ highlight_line: self
272+ .matches
273+ .value_of("highlight-line")
274+ .and_then(|w| w.parse().ok()),
267275 })
268276 }
269277
src/clap_app.rs+11
@@ -188,6 +188,17 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
188188 '--line-range 40:' prints lines 40 to the end of the file",
189189 ),
190190 )
191+ .arg(
192+ Arg::with_name("highlight-line")
193+ .long("highlight-line")
194+ .overrides_with("highlight-line")
195+ .takes_value(true)
196+ .value_name("N")
197+ .help("Highlight a line.")
198+ .long_help(
199+ "Highlight the Nth line. The background color is changed to create contrast.",
200+ ),
201+ )
191202 .arg(
192203 Arg::with_name("color")
193204 .long("color")
src/printer.rs+16−3
@@ -7,6 +7,7 @@ use ansi_term::Style;
77use console::AnsiCodeIterator;
88
99use syntect::easy::HighlightLines;
10+use syntect::highlighting::Color;
1011use syntect::highlighting::Theme;
1112use syntect::parsing::SyntaxSet;
1213
@@ -79,6 +80,7 @@ pub struct InteractivePrinter<'a> {
7980 pub line_changes: Option<LineChanges>,
8081 highlighter: Option<HighlightLines<'a>>,
8182 syntax_set: &'a SyntaxSet,
83+ background_highlight: Option<Color>,
8284}
8385
8486impl<'a> InteractivePrinter<'a> {
@@ -90,6 +92,8 @@ impl<'a> InteractivePrinter<'a> {
9092 ) -> Self {
9193 let theme = assets.get_theme(&config.theme);
9294
95+ let background_highlight = theme.settings.line_highlight;
96+
9397 let colors = if config.colored_output {
9498 Colors::colored(theme, config.true_color)
9599 } else {
@@ -156,6 +160,7 @@ impl<'a> InteractivePrinter<'a> {
156160 line_changes,
157161 highlighter,
158162 syntax_set: &assets.syntax_set,
163+ background_highlight,
159164 }
160165 }
161166
@@ -301,6 +306,12 @@ impl<'a> Printer for InteractivePrinter<'a> {
301306 }
302307 }
303308
309+ // Line highlighting
310+ let background = self.config.highlight_line
311+ .filter(|line| *line == line_number)
312+ .map(|_| self.background_highlight)
313+ .unwrap();
314+
304315 // Line contents.
305316 if self.config.output_wrap == OutputWrap::None {
306317 let true_color = self.config.true_color;
@@ -313,7 +324,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
313324 write!(
314325 handle,
315326 "{}",
316− as_terminal_escaped(style, text_trimmed, true_color, colored_output, italics,)
327+ as_terminal_escaped(style, text_trimmed, true_color, colored_output, italics, background)
317328 )?;
318329 write!(handle, "{}", &text[text_trimmed.len()..])?;
319330 }
@@ -370,7 +381,8 @@ impl<'a> Printer for InteractivePrinter<'a> {
370381 ),
371382 self.config.true_color,
372383 self.config.colored_output,
373− self.config.use_italic_text
384+ self.config.use_italic_text,
385+ background
374386 )
375387 )?;
376388 break;
@@ -410,7 +422,8 @@ impl<'a> Printer for InteractivePrinter<'a> {
410422 ),
411423 self.config.true_color,
412424 self.config.colored_output,
413− self.config.use_italic_text
425+ self.config.use_italic_text,
426+ background
414427 ),
415428 panel_wrap.clone().unwrap()
416429 )?;
src/terminal.rs+3−1
@@ -19,8 +19,9 @@ pub fn as_terminal_escaped(
1919 true_color: bool,
2020 colored: bool,
2121 italics: bool,
22+ background_color: Option<highlighting::Color>,
2223) -> String {
23− let style = if !colored {
24+ let mut style = if !colored {
2425 Style::default()
2526 } else {
2627 let color = to_ansi_color(style.foreground, true_color);
@@ -36,5 +37,6 @@ pub fn as_terminal_escaped(
3637 }
3738 };
3839
40+ style.background = background_color.map(|c| to_ansi_color(c, true_color));
3941 style.paint(text).to_string()
4042}