on master help sign in
add line highlight
1a6709cTaylor
src/app.rs+4
@@ -64,6 +64,9 @@ pub struct Config<'a> {
64
64
65 /// The syntax highlighting theme
65 /// The syntax highlighting theme
66 pub theme: String,
66 pub theme: String,
67+
68+ /// A line to highlight
69+ pub highlight_line: Option<usize>,
67}
70}
68
71
69fn is_truecolor_terminal() -> bool {
72fn is_truecolor_terminal() -> bool {
@@ -182,6 +185,7 @@ impl App {
182 .or_else(|| env::var("BAT_THEME").ok())
185 .or_else(|| env::var("BAT_THEME").ok())
183 .unwrap_or(String::from(BAT_THEME_DEFAULT)),
186 .unwrap_or(String::from(BAT_THEME_DEFAULT)),
184 line_range: transpose(self.matches.value_of("line-range").map(LineRange::from))?,
187 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()),
185 output_components,
189 output_components,
186 })
190 })
187 }
191 }
src/clap_app.rs+11
@@ -141,6 +141,17 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
141 '--line-range 40:' prints lines 40 to the end of the file",
141 '--line-range 40:' prints lines 40 to the end of the file",
142 ),
142 ),
143 )
143 )
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+ )
144 .arg(
155 .arg(
145 Arg::with_name("color")
156 Arg::with_name("color")
146 .long("color")
157 .long("color")
src/printer.rs+20−1
@@ -7,6 +7,7 @@ use ansi_term::Style;
7use console::AnsiCodeIterator;
7use console::AnsiCodeIterator;
8
8
9use syntect::easy::HighlightLines;
9use syntect::easy::HighlightLines;
10+use syntect::highlighting::Color;
10use syntect::highlighting::Theme;
11use syntect::highlighting::Theme;
11use syntect::parsing::SyntaxSet;
12use syntect::parsing::SyntaxSet;
12
13
@@ -79,6 +80,7 @@ pub struct InteractivePrinter<'a> {
79 pub line_changes: Option<LineChanges>,
80 pub line_changes: Option<LineChanges>,
80 highlighter: Option<HighlightLines<'a>>,
81 highlighter: Option<HighlightLines<'a>>,
81 syntax_set: &'a SyntaxSet,
82 syntax_set: &'a SyntaxSet,
83+ background_highlight: Option<Color>,
82}
84}
83
85
84impl<'a> InteractivePrinter<'a> {
86impl<'a> InteractivePrinter<'a> {
@@ -90,6 +92,8 @@ impl<'a> InteractivePrinter<'a> {
90 ) -> Self {
92 ) -> Self {
91 let theme = assets.get_theme(&config.theme);
93 let theme = assets.get_theme(&config.theme);
92
94
95+ let background_highlight = theme.settings.line_highlight;
96+
93 let colors = if config.colored_output {
97 let colors = if config.colored_output {
94 Colors::colored(theme, config.true_color)
98 Colors::colored(theme, config.true_color)
95 } else {
99 } else {
@@ -156,6 +160,7 @@ impl<'a> InteractivePrinter<'a> {
156 line_changes,
160 line_changes,
157 highlighter,
161 highlighter,
158 syntax_set: &assets.syntax_set,
162 syntax_set: &assets.syntax_set,
163+ background_highlight,
159 }
164 }
160 }
165 }
161
166
@@ -296,6 +301,18 @@ impl<'a> Printer for InteractivePrinter<'a> {
296 }
301 }
297 }
302 }
298
303
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+
299 // Line contents.
316 // Line contents.
300 if self.config.output_wrap == OutputWrap::None {
317 if self.config.output_wrap == OutputWrap::None {
301 let true_color = self.config.true_color;
318 let true_color = self.config.true_color;
@@ -306,7 +323,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
306 write!(
323 write!(
307 handle,
324 handle,
308 "{}",
325 "{}",
309− as_terminal_escaped(style, &*text, true_color, colored_output,)
326+ as_terminal_escaped(style, &*text, true_color, colored_output, background)
310 )?;
327 )?;
311 }
328 }
312
329
@@ -362,6 +379,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
362 ),
379 ),
363 self.config.true_color,
380 self.config.true_color,
364 self.config.colored_output,
381 self.config.colored_output,
382+ background
365 )
383 )
366 )?;
384 )?;
367 break;
385 break;
@@ -401,6 +419,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
401 ),
419 ),
402 self.config.true_color,
420 self.config.true_color,
403 self.config.colored_output,
421 self.config.colored_output,
422+ background
404 ),
423 ),
405 panel_wrap.clone().unwrap()
424 panel_wrap.clone().unwrap()
406 )?;
425 )?;
src/terminal.rs+3−1
@@ -18,8 +18,9 @@ pub fn as_terminal_escaped(
18 text: &str,
18 text: &str,
19 true_color: bool,
19 true_color: bool,
20 colored: bool,
20 colored: bool,
21+ background_color: Option<highlighting::Color>,
21) -> String {
22) -> String {
22− let style = if !colored {
23+ let mut style = if !colored {
23 Style::default()
24 Style::default()
24 } else {
25 } else {
25 let color = to_ansi_color(style.foreground, true_color);
26 let color = to_ansi_color(style.foreground, true_color);
@@ -35,5 +36,6 @@ pub fn as_terminal_escaped(
35 }
36 }
36 };
37 };
37
38
39+ style.background = background_color.map(|c| to_ansi_color(c, true_color));
38 style.paint(text).to_string()
40 style.paint(text).to_string()
39}
41}