1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-02-22 04:48:48 +00:00

Fixed tab expansion not working in --wrap=never mode.

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

View File

@ -1,7 +1,7 @@
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, mut cursor: usize) -> String { pub fn expand(line: &str, width: usize, cursor: &mut usize) -> String {
let mut buffer = String::with_capacity(line.len() * 2); let mut buffer = String::with_capacity(line.len() * 2);
for chunk in AnsiCodeIterator::new(line) { for chunk in AnsiCodeIterator::new(line) {
@ -11,20 +11,20 @@ pub fn expand(line: &str, width: usize, mut cursor: usize) -> String {
while let Some(index) = text.find('\t') { while let Some(index) = text.find('\t') {
// Add previous text. // Add previous text.
if index > 0 { if index > 0 {
cursor += index; *cursor += index;
buffer.push_str(&text[0..index]); buffer.push_str(&text[0..index]);
} }
// Add tab. // Add tab.
let spaces = width - (cursor % width); let spaces = width - (*cursor % width);
cursor += spaces; *cursor += spaces;
buffer.push_str(&*" ".repeat(spaces)); buffer.push_str(&*" ".repeat(spaces));
// Next. // Next.
text = &text[index + 1..text.len()]; text = &text[index + 1..text.len()];
} }
cursor += text.len(); *cursor += text.len();
buffer.push_str(text); buffer.push_str(text);
} }
} }

View File

@ -150,7 +150,7 @@ impl<'a> InteractivePrinter<'a> {
Ok(()) Ok(())
} }
fn preprocess(&self, text: &str, cursor: usize) -> String { fn preprocess(&self, text: &str, cursor: &mut usize) -> String {
if self.config.tab_width > 0 { if self.config.tab_width > 0 {
expand(text, self.config.tab_width, cursor) expand(text, self.config.tab_width, cursor)
} else { } else {
@ -243,19 +243,15 @@ impl<'a> Printer for InteractivePrinter<'a> {
let true_color = self.config.true_color; let true_color = self.config.true_color;
let colored_output = self.config.colored_output; let colored_output = self.config.colored_output;
write!( for &(style, region) in regions.iter() {
handle, let text = &*self.preprocess(region, &mut cursor_total);
"{}", write!(handle, "{}", as_terminal_escaped(
regions
.iter()
.map(|&(style, text)| as_terminal_escaped(
style, style,
&*self.preprocess(text, 0), &*text,
true_color, true_color,
colored_output, colored_output,
)).collect::<Vec<_>>() ))?;
.join("") }
)?;
} else { } else {
for &(style, region) in regions.iter() { for &(style, region) in regions.iter() {
let mut ansi_iterator = AnsiCodeIterator::new(region); let mut ansi_iterator = AnsiCodeIterator::new(region);
@ -280,8 +276,9 @@ impl<'a> Printer for InteractivePrinter<'a> {
(text, false) => { (text, false) => {
let text = self.preprocess( let text = self.preprocess(
text.trim_right_matches(|c| c == '\r' || c == '\n'), text.trim_right_matches(|c| c == '\r' || c == '\n'),
cursor_total, &mut cursor_total,
); );
let mut chars = text.chars(); let mut chars = text.chars();
let mut remaining = text.chars().count(); let mut remaining = text.chars().count();
@ -292,7 +289,6 @@ 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,
@ -330,7 +326,6 @@ 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!(