on master help sign in
Allow for multiple highlighted lines
commit6b92814ea0009d173060546dc6b459ef1aac238f authorsharkdp <davidpeter@web.de> dateSun 16 Dec 2018, 21:00 (8 years ago) parenta236a9b
3 files changed+35−20
src/app.rs+6−5 src/clap_app.rs+3−3 src/printer.rs+26−12
src/app.rs+6−5
@@ -80,8 +80,8 @@ pub struct Config<'a> {
8080 /// Whether or not to use ANSI italics
8181 pub use_italic_text: bool,
8282
83− /// A line to highlight
84− pub highlight_line: Option<usize>,
83+ /// Lines to highlight
84+ pub highlight_lines: Vec<usize>,
8585}
8686
8787fn is_truecolor_terminal() -> bool {
@@ -268,10 +268,11 @@ impl App {
268268 Some("always") => true,
269269 _ => false,
270270 },
271− highlight_line: self
271+ highlight_lines: self
272272 .matches
273− .value_of("highlight-line")
274− .and_then(|w| w.parse().ok()),
273+ .values_of("highlight-line")
274+ .and_then(|ws| ws.map(|w| w.parse().ok()).collect())
275+ .unwrap_or_default(),
275276 })
276277 }
277278
src/clap_app.rs+3−3
@@ -191,12 +191,12 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
191191 .arg(
192192 Arg::with_name("highlight-line")
193193 .long("highlight-line")
194− .overrides_with("highlight-line")
195194 .takes_value(true)
195+ .multiple(true)
196196 .value_name("N")
197− .help("Highlight a line.")
197+ .help("Highlight the given line.")
198198 .long_help(
199− "Highlight the Nth line. The background color is changed to create contrast.",
199+ "Highlight the N-th line with a different background color",
200200 ),
201201 )
202202 .arg(
src/printer.rs+26−12
@@ -80,7 +80,7 @@ pub struct InteractivePrinter<'a> {
8080 pub line_changes: Option<LineChanges>,
8181 highlighter: Option<HighlightLines<'a>>,
8282 syntax_set: &'a SyntaxSet,
83− background_highlight: Option<Color>,
83+ background_color_highlight: Option<Color>,
8484}
8585
8686impl<'a> InteractivePrinter<'a> {
@@ -92,7 +92,7 @@ impl<'a> InteractivePrinter<'a> {
9292 ) -> Self {
9393 let theme = assets.get_theme(&config.theme);
9494
95− let background_highlight = theme.settings.line_highlight;
95+ let background_color_highlight = theme.settings.line_highlight;
9696
9797 let colors = if config.colored_output {
9898 Colors::colored(theme, config.true_color)
@@ -160,7 +160,7 @@ impl<'a> InteractivePrinter<'a> {
160160 line_changes,
161161 highlighter,
162162 syntax_set: &assets.syntax_set,
163− background_highlight,
163+ background_color_highlight,
164164 }
165165 }
166166
@@ -292,6 +292,19 @@ impl<'a> Printer for InteractivePrinter<'a> {
292292 let mut cursor_total: usize = 0;
293293 let mut panel_wrap: Option<String> = None;
294294
295+ // Line highlighting
296+ let background_color = if self
297+ .config
298+ .highlight_lines
299+ .iter()
300+ .find(|&&l| l == line_number)
301+ .is_some()
302+ {
303+ self.background_color_highlight
304+ } else {
305+ None
306+ };
307+
295308 // Line decorations.
296309 if self.panel_width > 0 {
297310 let decorations = self
@@ -306,12 +319,6 @@ impl<'a> Printer for InteractivePrinter<'a> {
306319 }
307320 }
308321
309− // Line highlighting
310− let background = self.config.highlight_line
311− .filter(|line| *line == line_number)
312− .map(|_| self.background_highlight)
313− .unwrap();
314−
315322 // Line contents.
316323 if self.config.output_wrap == OutputWrap::None {
317324 let true_color = self.config.true_color;
@@ -324,7 +331,14 @@ impl<'a> Printer for InteractivePrinter<'a> {
324331 write!(
325332 handle,
326333 "{}",
327− as_terminal_escaped(style, text_trimmed, true_color, colored_output, italics, background)
334+ as_terminal_escaped(
335+ style,
336+ text_trimmed,
337+ true_color,
338+ colored_output,
339+ italics,
340+ background_color
341+ )
328342 )?;
329343 write!(handle, "{}", &text[text_trimmed.len()..])?;
330344 }
@@ -382,7 +396,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
382396 self.config.true_color,
383397 self.config.colored_output,
384398 self.config.use_italic_text,
385− background
399+ background_color
386400 )
387401 )?;
388402 break;
@@ -423,7 +437,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
423437 self.config.true_color,
424438 self.config.colored_output,
425439 self.config.use_italic_text,
426− background
440+ background_color
427441 ),
428442 panel_wrap.clone().unwrap()
429443 )?;