Allow for multiple highlighted lines

This commit is contained in:
sharkdp 2018-12-16 21:00:18 +01:00
parent a236a9b195
commit 6b92814ea0
3 changed files with 35 additions and 20 deletions

View File

@ -80,8 +80,8 @@ pub struct Config<'a> {
/// Whether or not to use ANSI italics
pub use_italic_text: bool,
/// A line to highlight
pub highlight_line: Option<usize>,
/// Lines to highlight
pub highlight_lines: Vec<usize>,
}
fn is_truecolor_terminal() -> bool {
@ -268,10 +268,11 @@ impl App {
Some("always") => true,
_ => false,
},
highlight_line: self
highlight_lines: self
.matches
.value_of("highlight-line")
.and_then(|w| w.parse().ok()),
.values_of("highlight-line")
.and_then(|ws| ws.map(|w| w.parse().ok()).collect())
.unwrap_or_default(),
})
}

View File

@ -191,12 +191,12 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
.arg(
Arg::with_name("highlight-line")
.long("highlight-line")
.overrides_with("highlight-line")
.takes_value(true)
.multiple(true)
.value_name("N")
.help("Highlight a line.")
.help("Highlight the given line.")
.long_help(
"Highlight the Nth line. The background color is changed to create contrast.",
"Highlight the N-th line with a different background color",
),
)
.arg(

View File

@ -80,7 +80,7 @@ pub struct InteractivePrinter<'a> {
pub line_changes: Option<LineChanges>,
highlighter: Option<HighlightLines<'a>>,
syntax_set: &'a SyntaxSet,
background_highlight: Option<Color>,
background_color_highlight: Option<Color>,
}
impl<'a> InteractivePrinter<'a> {
@ -92,7 +92,7 @@ impl<'a> InteractivePrinter<'a> {
) -> Self {
let theme = assets.get_theme(&config.theme);
let background_highlight = theme.settings.line_highlight;
let background_color_highlight = theme.settings.line_highlight;
let colors = if config.colored_output {
Colors::colored(theme, config.true_color)
@ -160,7 +160,7 @@ impl<'a> InteractivePrinter<'a> {
line_changes,
highlighter,
syntax_set: &assets.syntax_set,
background_highlight,
background_color_highlight,
}
}
@ -292,6 +292,19 @@ impl<'a> Printer for InteractivePrinter<'a> {
let mut cursor_total: usize = 0;
let mut panel_wrap: Option<String> = None;
// Line highlighting
let background_color = if self
.config
.highlight_lines
.iter()
.find(|&&l| l == line_number)
.is_some()
{
self.background_color_highlight
} else {
None
};
// Line decorations.
if self.panel_width > 0 {
let decorations = self
@ -306,12 +319,6 @@ impl<'a> Printer for InteractivePrinter<'a> {
}
}
// Line highlighting
let background = self.config.highlight_line
.filter(|line| *line == line_number)
.map(|_| self.background_highlight)
.unwrap();
// Line contents.
if self.config.output_wrap == OutputWrap::None {
let true_color = self.config.true_color;
@ -324,7 +331,14 @@ impl<'a> Printer for InteractivePrinter<'a> {
write!(
handle,
"{}",
as_terminal_escaped(style, text_trimmed, true_color, colored_output, italics, background)
as_terminal_escaped(
style,
text_trimmed,
true_color,
colored_output,
italics,
background_color
)
)?;
write!(handle, "{}", &text[text_trimmed.len()..])?;
}
@ -382,7 +396,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
self.config.true_color,
self.config.colored_output,
self.config.use_italic_text,
background
background_color
)
)?;
break;
@ -423,7 +437,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
self.config.true_color,
self.config.colored_output,
self.config.use_italic_text,
background
background_color
),
panel_wrap.clone().unwrap()
)?;