on master help sign in
add line highlight
commit1a6709c2cb3d8403e2f709a7e8b66c031216e559 authorTaylor <tskinn12@gmail.com> dateTue 9 Oct 2018, 22:25 (8 years ago) parent5842d58
4 files changed+38−2
src/app.rs+4 src/clap_app.rs+11 src/printer.rs+20−1 src/terminal.rs+3−1
src/app.rs+4
@@ -64,6 +64,9 @@ pub struct Config<'a> {
6464
6565 /// The syntax highlighting theme
6666 pub theme: String,
67+
68+ /// A line to highlight
69+ pub highlight_line: Option<usize>,
6770}
6871
6972fn is_truecolor_terminal() -> bool {
@@ -182,6 +185,7 @@ impl App {
182185 .or_else(|| env::var("BAT_THEME").ok())
183186 .unwrap_or(String::from(BAT_THEME_DEFAULT)),
184187 line_range: transpose(self.matches.value_of("line-range").map(LineRange::from))?,
188+ highlight_line: self.matches.value_of("highlight-line").and_then(|w| w.parse().ok()),
185189 output_components,
186190 })
187191 }
src/clap_app.rs+11
@@ -141,6 +141,17 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
141141 '--line-range 40:' prints lines 40 to the end of the file",
142142 ),
143143 )
144+ .arg(
145+ Arg::with_name("highlight-line")
146+ .long("highlight-line")
147+ .overrides_with("highlight-line")
148+ .takes_value(true)
149+ .value_name("n")
150+ .help("Highlight a line.")
151+ .long_help(
152+ "Highlight the nth line. The background color is changed to create contrast.",
153+ ),
154+ )
144155 .arg(
145156 Arg::with_name("color")
146157 .long("color")
src/printer.rs+20−1
@@ -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
@@ -296,6 +301,18 @@ impl<'a> Printer for InteractivePrinter<'a> {
296301 }
297302 }
298303
304+ // Line highlighting
305+ let background = match self.config.highlight_line {
306+ Some(line) => {
307+ if line_number == line {
308+ self.background_highlight
309+ } else {
310+ None
311+ }
312+ }
313+ _ => None
314+ };
315+
299316 // Line contents.
300317 if self.config.output_wrap == OutputWrap::None {
301318 let true_color = self.config.true_color;
@@ -306,7 +323,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
306323 write!(
307324 handle,
308325 "{}",
309− as_terminal_escaped(style, &*text, true_color, colored_output,)
326+ as_terminal_escaped(style, &*text, true_color, colored_output, background)
310327 )?;
311328 }
312329
@@ -362,6 +379,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
362379 ),
363380 self.config.true_color,
364381 self.config.colored_output,
382+ background
365383 )
366384 )?;
367385 break;
@@ -401,6 +419,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
401419 ),
402420 self.config.true_color,
403421 self.config.colored_output,
422+ background
404423 ),
405424 panel_wrap.clone().unwrap()
406425 )?;
src/terminal.rs+3−1
@@ -18,8 +18,9 @@ pub fn as_terminal_escaped(
1818 text: &str,
1919 true_color: bool,
2020 colored: bool,
21+ background_color: Option<highlighting::Color>,
2122) -> String {
22− let style = if !colored {
23+ let mut style = if !colored {
2324 Style::default()
2425 } else {
2526 let color = to_ansi_color(style.foreground, true_color);
@@ -35,5 +36,6 @@ pub fn as_terminal_escaped(
3536 }
3637 };
3738
39+ style.background = background_color.map(|c| to_ansi_color(c, true_color));
3840 style.paint(text).to_string()
3941}