mirror of
https://github.com/sharkdp/bat.git
synced 2025-02-21 20:38:44 +00:00
Allow for multiple highlighted lines
This commit is contained in:
parent
a236a9b195
commit
6b92814ea0
11
src/app.rs
11
src/app.rs
@ -80,8 +80,8 @@ pub struct Config<'a> {
|
|||||||
/// Whether or not to use ANSI italics
|
/// Whether or not to use ANSI italics
|
||||||
pub use_italic_text: bool,
|
pub use_italic_text: bool,
|
||||||
|
|
||||||
/// A line to highlight
|
/// Lines to highlight
|
||||||
pub highlight_line: Option<usize>,
|
pub highlight_lines: Vec<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_truecolor_terminal() -> bool {
|
fn is_truecolor_terminal() -> bool {
|
||||||
@ -268,10 +268,11 @@ impl App {
|
|||||||
Some("always") => true,
|
Some("always") => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
},
|
},
|
||||||
highlight_line: self
|
highlight_lines: self
|
||||||
.matches
|
.matches
|
||||||
.value_of("highlight-line")
|
.values_of("highlight-line")
|
||||||
.and_then(|w| w.parse().ok()),
|
.and_then(|ws| ws.map(|w| w.parse().ok()).collect())
|
||||||
|
.unwrap_or_default(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -191,12 +191,12 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
|
|||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("highlight-line")
|
Arg::with_name("highlight-line")
|
||||||
.long("highlight-line")
|
.long("highlight-line")
|
||||||
.overrides_with("highlight-line")
|
|
||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
|
.multiple(true)
|
||||||
.value_name("N")
|
.value_name("N")
|
||||||
.help("Highlight a line.")
|
.help("Highlight the given line.")
|
||||||
.long_help(
|
.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(
|
.arg(
|
||||||
|
@ -80,7 +80,7 @@ pub struct InteractivePrinter<'a> {
|
|||||||
pub line_changes: Option<LineChanges>,
|
pub line_changes: Option<LineChanges>,
|
||||||
highlighter: Option<HighlightLines<'a>>,
|
highlighter: Option<HighlightLines<'a>>,
|
||||||
syntax_set: &'a SyntaxSet,
|
syntax_set: &'a SyntaxSet,
|
||||||
background_highlight: Option<Color>,
|
background_color_highlight: Option<Color>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> InteractivePrinter<'a> {
|
impl<'a> InteractivePrinter<'a> {
|
||||||
@ -92,7 +92,7 @@ impl<'a> InteractivePrinter<'a> {
|
|||||||
) -> Self {
|
) -> Self {
|
||||||
let theme = assets.get_theme(&config.theme);
|
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 {
|
let colors = if config.colored_output {
|
||||||
Colors::colored(theme, config.true_color)
|
Colors::colored(theme, config.true_color)
|
||||||
@ -160,7 +160,7 @@ impl<'a> InteractivePrinter<'a> {
|
|||||||
line_changes,
|
line_changes,
|
||||||
highlighter,
|
highlighter,
|
||||||
syntax_set: &assets.syntax_set,
|
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 cursor_total: usize = 0;
|
||||||
let mut panel_wrap: Option<String> = None;
|
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.
|
// Line decorations.
|
||||||
if self.panel_width > 0 {
|
if self.panel_width > 0 {
|
||||||
let decorations = self
|
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.
|
// Line contents.
|
||||||
if self.config.output_wrap == OutputWrap::None {
|
if self.config.output_wrap == OutputWrap::None {
|
||||||
let true_color = self.config.true_color;
|
let true_color = self.config.true_color;
|
||||||
@ -324,7 +331,14 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
write!(
|
write!(
|
||||||
handle,
|
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()..])?;
|
write!(handle, "{}", &text[text_trimmed.len()..])?;
|
||||||
}
|
}
|
||||||
@ -382,7 +396,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
self.config.true_color,
|
self.config.true_color,
|
||||||
self.config.colored_output,
|
self.config.colored_output,
|
||||||
self.config.use_italic_text,
|
self.config.use_italic_text,
|
||||||
background
|
background_color
|
||||||
)
|
)
|
||||||
)?;
|
)?;
|
||||||
break;
|
break;
|
||||||
@ -423,7 +437,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
self.config.true_color,
|
self.config.true_color,
|
||||||
self.config.colored_output,
|
self.config.colored_output,
|
||||||
self.config.use_italic_text,
|
self.config.use_italic_text,
|
||||||
background
|
background_color
|
||||||
),
|
),
|
||||||
panel_wrap.clone().unwrap()
|
panel_wrap.clone().unwrap()
|
||||||
)?;
|
)?;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user