mirror of
https://github.com/sharkdp/bat.git
synced 2025-01-18 20:11:03 +00:00
Merge pull request #2857 from eth-p/skip-highlighting-when-no-color
Skip syntax parsing when color is not being output.
This commit is contained in:
commit
e1a3fc5529
@ -26,6 +26,7 @@
|
|||||||
- Pull in fix for unsafe-libyaml security advisory, see #2812 (@dtolnay)
|
- Pull in fix for unsafe-libyaml security advisory, see #2812 (@dtolnay)
|
||||||
- Update git-version dependency to use Syn v2, see #2816 (@dtolnay)
|
- Update git-version dependency to use Syn v2, see #2816 (@dtolnay)
|
||||||
- Update git2 dependency to v0.18.2, see #2852 (@eth-p)
|
- Update git2 dependency to v0.18.2, see #2852 (@eth-p)
|
||||||
|
- Improve performance when color output disabled, see #2397 and #2857 (@eth-p)
|
||||||
- Relax syntax mapping rule restrictions to allow brace expansion #2865 (@cyqsimon)
|
- Relax syntax mapping rule restrictions to allow brace expansion #2865 (@cyqsimon)
|
||||||
- Apply clippy fixes #2864 (@cyqsimon)
|
- Apply clippy fixes #2864 (@cyqsimon)
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ use bytesize::ByteSize;
|
|||||||
|
|
||||||
use syntect::easy::HighlightLines;
|
use syntect::easy::HighlightLines;
|
||||||
use syntect::highlighting::Color;
|
use syntect::highlighting::Color;
|
||||||
|
use syntect::highlighting::FontStyle;
|
||||||
use syntect::highlighting::Theme;
|
use syntect::highlighting::Theme;
|
||||||
use syntect::parsing::SyntaxSet;
|
use syntect::parsing::SyntaxSet;
|
||||||
|
|
||||||
@ -48,6 +49,22 @@ const ANSI_UNDERLINE_DISABLE: EscapeSequence = EscapeSequence::CSI {
|
|||||||
final_byte: "m",
|
final_byte: "m",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const EMPTY_SYNTECT_STYLE: syntect::highlighting::Style = syntect::highlighting::Style {
|
||||||
|
foreground: Color {
|
||||||
|
r: 127,
|
||||||
|
g: 127,
|
||||||
|
b: 127,
|
||||||
|
a: 255,
|
||||||
|
},
|
||||||
|
background: Color {
|
||||||
|
r: 127,
|
||||||
|
g: 127,
|
||||||
|
b: 127,
|
||||||
|
a: 255,
|
||||||
|
},
|
||||||
|
font_style: FontStyle::empty(),
|
||||||
|
};
|
||||||
|
|
||||||
pub enum OutputHandle<'a> {
|
pub enum OutputHandle<'a> {
|
||||||
IoWrite(&'a mut dyn io::Write),
|
IoWrite(&'a mut dyn io::Write),
|
||||||
FmtWrite(&'a mut dyn fmt::Write),
|
FmtWrite(&'a mut dyn fmt::Write),
|
||||||
@ -222,11 +239,13 @@ impl<'a> InteractivePrinter<'a> {
|
|||||||
panel_width = 0;
|
panel_width = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
let highlighter_from_set = if input
|
// Get the highlighter for the output.
|
||||||
|
let is_printing_binary = input
|
||||||
.reader
|
.reader
|
||||||
.content_type
|
.content_type
|
||||||
.map_or(false, |c| c.is_binary() && !config.show_nonprintable)
|
.map_or(false, |c| c.is_binary() && !config.show_nonprintable);
|
||||||
{
|
|
||||||
|
let highlighter_from_set = if is_printing_binary || config.colored_output == false {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
// Determine the type of syntax for highlighting
|
// Determine the type of syntax for highlighting
|
||||||
@ -346,6 +365,31 @@ impl<'a> InteractivePrinter<'a> {
|
|||||||
self.print_header_component_with_indent(handle, content)
|
self.print_header_component_with_indent(handle, content)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn highlight_regions_for_line<'b>(
|
||||||
|
&mut self,
|
||||||
|
line: &'b str,
|
||||||
|
) -> Result<Vec<(syntect::highlighting::Style, &'b str)>> {
|
||||||
|
let highlighter_from_set = match self.highlighter_from_set {
|
||||||
|
Some(ref mut highlighter_from_set) => highlighter_from_set,
|
||||||
|
_ => return Ok(vec![(EMPTY_SYNTECT_STYLE, line)]),
|
||||||
|
};
|
||||||
|
|
||||||
|
// skip syntax highlighting on long lines
|
||||||
|
let too_long = line.len() > 1024 * 16;
|
||||||
|
|
||||||
|
let for_highlighting: &str = if too_long { "\n" } else { &line };
|
||||||
|
|
||||||
|
let mut highlighted_line = highlighter_from_set
|
||||||
|
.highlighter
|
||||||
|
.highlight_line(for_highlighting, highlighter_from_set.syntax_set)?;
|
||||||
|
|
||||||
|
if too_long {
|
||||||
|
highlighted_line[0].1 = &line;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(highlighted_line)
|
||||||
|
}
|
||||||
|
|
||||||
fn preprocess(&self, text: &str, cursor: &mut usize) -> String {
|
fn preprocess(&self, text: &str, cursor: &mut usize) -> String {
|
||||||
if self.config.tab_width > 0 {
|
if self.config.tab_width > 0 {
|
||||||
return expand_tabs(text, self.config.tab_width, cursor);
|
return expand_tabs(text, self.config.tab_width, cursor);
|
||||||
@ -528,30 +572,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let regions = {
|
let regions = self.highlight_regions_for_line(&line)?;
|
||||||
let highlighter_from_set = match self.highlighter_from_set {
|
|
||||||
Some(ref mut highlighter_from_set) => highlighter_from_set,
|
|
||||||
_ => {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// skip syntax highlighting on long lines
|
|
||||||
let too_long = line.len() > 1024 * 16;
|
|
||||||
|
|
||||||
let for_highlighting: &str = if too_long { "\n" } else { &line };
|
|
||||||
|
|
||||||
let mut highlighted_line = highlighter_from_set
|
|
||||||
.highlighter
|
|
||||||
.highlight_line(for_highlighting, highlighter_from_set.syntax_set)?;
|
|
||||||
|
|
||||||
if too_long {
|
|
||||||
highlighted_line[0].1 = &line;
|
|
||||||
}
|
|
||||||
|
|
||||||
highlighted_line
|
|
||||||
};
|
|
||||||
|
|
||||||
if out_of_range {
|
if out_of_range {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user