mirror of
https://github.com/sharkdp/bat.git
synced 2025-03-15 15:18:45 +00:00
add line highlight
This commit is contained in:
parent
5842d58c01
commit
1a6709c2cb
@ -64,6 +64,9 @@ pub struct Config<'a> {
|
|||||||
|
|
||||||
/// The syntax highlighting theme
|
/// The syntax highlighting theme
|
||||||
pub theme: String,
|
pub theme: String,
|
||||||
|
|
||||||
|
/// A line to highlight
|
||||||
|
pub highlight_line: Option<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_truecolor_terminal() -> bool {
|
fn is_truecolor_terminal() -> bool {
|
||||||
@ -182,6 +185,7 @@ impl App {
|
|||||||
.or_else(|| env::var("BAT_THEME").ok())
|
.or_else(|| env::var("BAT_THEME").ok())
|
||||||
.unwrap_or(String::from(BAT_THEME_DEFAULT)),
|
.unwrap_or(String::from(BAT_THEME_DEFAULT)),
|
||||||
line_range: transpose(self.matches.value_of("line-range").map(LineRange::from))?,
|
line_range: transpose(self.matches.value_of("line-range").map(LineRange::from))?,
|
||||||
|
highlight_line: self.matches.value_of("highlight-line").and_then(|w| w.parse().ok()),
|
||||||
output_components,
|
output_components,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -141,6 +141,17 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
|
|||||||
'--line-range 40:' prints lines 40 to the end of the file",
|
'--line-range 40:' prints lines 40 to the end of the file",
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("highlight-line")
|
||||||
|
.long("highlight-line")
|
||||||
|
.overrides_with("highlight-line")
|
||||||
|
.takes_value(true)
|
||||||
|
.value_name("n")
|
||||||
|
.help("Highlight a line.")
|
||||||
|
.long_help(
|
||||||
|
"Highlight the nth line. The background color is changed to create contrast.",
|
||||||
|
),
|
||||||
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("color")
|
Arg::with_name("color")
|
||||||
.long("color")
|
.long("color")
|
||||||
|
@ -7,6 +7,7 @@ use ansi_term::Style;
|
|||||||
use console::AnsiCodeIterator;
|
use console::AnsiCodeIterator;
|
||||||
|
|
||||||
use syntect::easy::HighlightLines;
|
use syntect::easy::HighlightLines;
|
||||||
|
use syntect::highlighting::Color;
|
||||||
use syntect::highlighting::Theme;
|
use syntect::highlighting::Theme;
|
||||||
use syntect::parsing::SyntaxSet;
|
use syntect::parsing::SyntaxSet;
|
||||||
|
|
||||||
@ -79,6 +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>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> InteractivePrinter<'a> {
|
impl<'a> InteractivePrinter<'a> {
|
||||||
@ -90,6 +92,8 @@ 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 colors = if config.colored_output {
|
let colors = if config.colored_output {
|
||||||
Colors::colored(theme, config.true_color)
|
Colors::colored(theme, config.true_color)
|
||||||
} else {
|
} else {
|
||||||
@ -156,6 +160,7 @@ impl<'a> InteractivePrinter<'a> {
|
|||||||
line_changes,
|
line_changes,
|
||||||
highlighter,
|
highlighter,
|
||||||
syntax_set: &assets.syntax_set,
|
syntax_set: &assets.syntax_set,
|
||||||
|
background_highlight,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -296,6 +301,18 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Line highlighting
|
||||||
|
let background = match self.config.highlight_line {
|
||||||
|
Some(line) => {
|
||||||
|
if line_number == line {
|
||||||
|
self.background_highlight
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => None
|
||||||
|
};
|
||||||
|
|
||||||
// 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;
|
||||||
@ -306,7 +323,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
write!(
|
write!(
|
||||||
handle,
|
handle,
|
||||||
"{}",
|
"{}",
|
||||||
as_terminal_escaped(style, &*text, true_color, colored_output,)
|
as_terminal_escaped(style, &*text, true_color, colored_output, background)
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -362,6 +379,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
),
|
),
|
||||||
self.config.true_color,
|
self.config.true_color,
|
||||||
self.config.colored_output,
|
self.config.colored_output,
|
||||||
|
background
|
||||||
)
|
)
|
||||||
)?;
|
)?;
|
||||||
break;
|
break;
|
||||||
@ -401,6 +419,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
),
|
),
|
||||||
self.config.true_color,
|
self.config.true_color,
|
||||||
self.config.colored_output,
|
self.config.colored_output,
|
||||||
|
background
|
||||||
),
|
),
|
||||||
panel_wrap.clone().unwrap()
|
panel_wrap.clone().unwrap()
|
||||||
)?;
|
)?;
|
||||||
|
@ -18,8 +18,9 @@ pub fn as_terminal_escaped(
|
|||||||
text: &str,
|
text: &str,
|
||||||
true_color: bool,
|
true_color: bool,
|
||||||
colored: bool,
|
colored: bool,
|
||||||
|
background_color: Option<highlighting::Color>,
|
||||||
) -> String {
|
) -> String {
|
||||||
let style = if !colored {
|
let mut style = if !colored {
|
||||||
Style::default()
|
Style::default()
|
||||||
} else {
|
} else {
|
||||||
let color = to_ansi_color(style.foreground, true_color);
|
let color = to_ansi_color(style.foreground, true_color);
|
||||||
@ -35,5 +36,6 @@ pub fn as_terminal_escaped(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
style.background = background_color.map(|c| to_ansi_color(c, true_color));
|
||||||
style.paint(text).to_string()
|
style.paint(text).to_string()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user