on master help sign in
Allow for multiple highlighted lines
6b92814sharkdp
src/app.rs+6−5
@@ -80,8 +80,8 @@ pub struct Config<'a> {
80 /// Whether or not to use ANSI italics
80 /// Whether or not to use ANSI italics
81 pub use_italic_text: bool,
81 pub use_italic_text: bool,
82
82
83− /// A line to highlight
83+ /// Lines to highlight
84− pub highlight_line: Option<usize>,
84+ pub highlight_lines: Vec<usize>,
85}
85}
86
86
87fn is_truecolor_terminal() -> bool {
87fn is_truecolor_terminal() -> bool {
@@ -268,10 +268,11 @@ impl App {
268 Some("always") => true,
268 Some("always") => true,
269 _ => false,
269 _ => false,
270 },
270 },
271− highlight_line: self
271+ highlight_lines: self
272 .matches
272 .matches
273− .value_of("highlight-line")
273+ .values_of("highlight-line")
274− .and_then(|w| w.parse().ok()),
274+ .and_then(|ws| ws.map(|w| w.parse().ok()).collect())
275+ .unwrap_or_default(),
275 })
276 })
276 }
277 }
277
278
src/clap_app.rs+3−3
@@ -191,12 +191,12 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
191 .arg(
191 .arg(
192 Arg::with_name("highlight-line")
192 Arg::with_name("highlight-line")
193 .long("highlight-line")
193 .long("highlight-line")
194− .overrides_with("highlight-line")
195 .takes_value(true)
194 .takes_value(true)
195+ .multiple(true)
196 .value_name("N")
196 .value_name("N")
197− .help("Highlight a line.")
197+ .help("Highlight the given line.")
198 .long_help(
198 .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",
200 ),
200 ),
201 )
201 )
202 .arg(
202 .arg(
src/printer.rs+26−12
@@ -80,7 +80,7 @@ pub struct InteractivePrinter<'a> {
80 pub line_changes: Option<LineChanges>,
80 pub line_changes: Option<LineChanges>,
81 highlighter: Option<HighlightLines<'a>>,
81 highlighter: Option<HighlightLines<'a>>,
82 syntax_set: &'a SyntaxSet,
82 syntax_set: &'a SyntaxSet,
83− background_highlight: Option<Color>,
83+ background_color_highlight: Option<Color>,
84}
84}
85
85
86impl<'a> InteractivePrinter<'a> {
86impl<'a> InteractivePrinter<'a> {
@@ -92,7 +92,7 @@ impl<'a> InteractivePrinter<'a> {
92 ) -> Self {
92 ) -> Self {
93 let theme = assets.get_theme(&config.theme);
93 let theme = assets.get_theme(&config.theme);
94
94
95− let background_highlight = theme.settings.line_highlight;
95+ let background_color_highlight = theme.settings.line_highlight;
96
96
97 let colors = if config.colored_output {
97 let colors = if config.colored_output {
98 Colors::colored(theme, config.true_color)
98 Colors::colored(theme, config.true_color)
@@ -160,7 +160,7 @@ impl<'a> InteractivePrinter<'a> {
160 line_changes,
160 line_changes,
161 highlighter,
161 highlighter,
162 syntax_set: &assets.syntax_set,
162 syntax_set: &assets.syntax_set,
163− background_highlight,
163+ background_color_highlight,
164 }
164 }
165 }
165 }
166
166
@@ -292,6 +292,19 @@ impl<'a> Printer for InteractivePrinter<'a> {
292 let mut cursor_total: usize = 0;
292 let mut cursor_total: usize = 0;
293 let mut panel_wrap: Option<String> = None;
293 let mut panel_wrap: Option<String> = None;
294
294
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+
295 // Line decorations.
308 // Line decorations.
296 if self.panel_width > 0 {
309 if self.panel_width > 0 {
297 let decorations = self
310 let decorations = self
@@ -306,12 +319,6 @@ impl<'a> Printer for InteractivePrinter<'a> {
306 }
319 }
307 }
320 }
308
321
309− // Line highlighting
310− let background = self.config.highlight_line
311− .filter(|line| *line == line_number)
312− .map(|_| self.background_highlight)
313− .unwrap();
314−
315 // Line contents.
322 // Line contents.
316 if self.config.output_wrap == OutputWrap::None {
323 if self.config.output_wrap == OutputWrap::None {
317 let true_color = self.config.true_color;
324 let true_color = self.config.true_color;
@@ -324,7 +331,14 @@ impl<'a> Printer for InteractivePrinter<'a> {
324 write!(
331 write!(
325 handle,
332 handle,
326 "{}",
333 "{}",
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+ )
328 )?;
342 )?;
329 write!(handle, "{}", &text[text_trimmed.len()..])?;
343 write!(handle, "{}", &text[text_trimmed.len()..])?;
330 }
344 }
@@ -382,7 +396,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
382 self.config.true_color,
396 self.config.true_color,
383 self.config.colored_output,
397 self.config.colored_output,
384 self.config.use_italic_text,
398 self.config.use_italic_text,
385− background
399+ background_color
386 )
400 )
387 )?;
401 )?;
388 break;
402 break;
@@ -423,7 +437,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
423 self.config.true_color,
437 self.config.true_color,
424 self.config.colored_output,
438 self.config.colored_output,
425 self.config.use_italic_text,
439 self.config.use_italic_text,
426− background
440+ background_color
427 ),
441 ),
428 panel_wrap.clone().unwrap()
442 panel_wrap.clone().unwrap()
429 )?;
443 )?;