1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-01-31 10:11:07 +00:00

Moved tab expansion to happen after syntax highlighting.

This commit is contained in:
eth-p 2018-09-11 13:02:22 -07:00
parent eb6e43b9a9
commit b4096e5627
No known key found for this signature in database
GPG Key ID: 1F8DF8091CD46FBC
2 changed files with 18 additions and 10 deletions

View File

@ -1,9 +1,8 @@
use console::AnsiCodeIterator; use console::AnsiCodeIterator;
/// Expand tabs like an ANSI-enabled expand(1). /// Expand tabs like an ANSI-enabled expand(1).
pub fn expand(line: &str, width: usize) -> String { pub fn expand(line: &str, width: usize, mut cursor: usize) -> String {
let mut buffer = String::with_capacity(line.len() * 2); let mut buffer = String::with_capacity(line.len() * 2);
let mut cursor = 0;
for chunk in AnsiCodeIterator::new(line) { for chunk in AnsiCodeIterator::new(line) {
match chunk { match chunk {

View File

@ -149,6 +149,14 @@ impl<'a> InteractivePrinter<'a> {
Ok(()) Ok(())
} }
fn preprocess(&self, text: &str, cursor: usize) -> String {
if self.config.tab_width > 0 {
expand(text, self.config.tab_width, cursor)
} else {
text.to_string()
}
}
} }
impl<'a> Printer for InteractivePrinter<'a> { impl<'a> Printer for InteractivePrinter<'a> {
@ -201,12 +209,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
line_number: usize, line_number: usize,
line_buffer: &[u8], line_buffer: &[u8],
) -> Result<()> { ) -> Result<()> {
let mut line = String::from_utf8_lossy(&line_buffer).to_string(); let line = String::from_utf8_lossy(&line_buffer).to_string();
// Preprocess.
if self.config.tab_width > 0 {
line = expand(&line, self.config.tab_width);
}
// Highlight. // Highlight.
let regions = self.highlighter.highlight(line.as_ref()); let regions = self.highlighter.highlight(line.as_ref());
@ -218,6 +221,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
let mut cursor: usize = 0; let mut cursor: usize = 0;
let mut cursor_max: usize = self.config.term_width; let mut cursor_max: usize = self.config.term_width;
let mut cursor_total: usize = 0;
let mut panel_wrap: Option<String> = None; let mut panel_wrap: Option<String> = None;
// Line decorations. // Line decorations.
@ -246,7 +250,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
.iter() .iter()
.map(|&(style, text)| as_terminal_escaped( .map(|&(style, text)| as_terminal_escaped(
style, style,
text, &*self.preprocess(text, 0),
true_color, true_color,
colored_output, colored_output,
)).collect::<Vec<_>>() )).collect::<Vec<_>>()
@ -274,7 +278,10 @@ impl<'a> Printer for InteractivePrinter<'a> {
// Regular text. // Regular text.
(text, false) => { (text, false) => {
let text = text.trim_right_matches(|c| c == '\r' || c == '\n'); let text = self.preprocess(
text.trim_right_matches(|c| c == '\r' || c == '\n'),
cursor_total,
);
let mut chars = text.chars(); let mut chars = text.chars();
let mut remaining = text.chars().count(); let mut remaining = text.chars().count();
@ -285,6 +292,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
if remaining <= available { if remaining <= available {
let text = chars.by_ref().take(remaining).collect::<String>(); let text = chars.by_ref().take(remaining).collect::<String>();
cursor += remaining; cursor += remaining;
cursor_total += remaining;
write!( write!(
handle, handle,
@ -322,6 +330,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
// It wraps. // It wraps.
let text = chars.by_ref().take(available).collect::<String>(); let text = chars.by_ref().take(available).collect::<String>();
cursor = 0; cursor = 0;
cursor_total += available;
remaining -= available; remaining -= available;
write!( write!(