From d90797f8e947f01da74fd2aa065f48df3e50ab0b Mon Sep 17 00:00:00 2001 From: eth-p <32112321+eth-p@users.noreply.github.com> Date: Tue, 11 Sep 2018 13:45:49 -0700 Subject: [PATCH] Fixed tab expansion not working in --wrap=never mode. --- src/preprocessor.rs | 10 +++++----- src/printer.rs | 29 ++++++++++++----------------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/preprocessor.rs b/src/preprocessor.rs index f2970429..0a99e07d 100644 --- a/src/preprocessor.rs +++ b/src/preprocessor.rs @@ -1,7 +1,7 @@ use console::AnsiCodeIterator; /// 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); 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') { // Add previous text. if index > 0 { - cursor += index; + *cursor += index; buffer.push_str(&text[0..index]); } // Add tab. - let spaces = width - (cursor % width); - cursor += spaces; + let spaces = width - (*cursor % width); + *cursor += spaces; buffer.push_str(&*" ".repeat(spaces)); // Next. text = &text[index + 1..text.len()]; } - cursor += text.len(); + *cursor += text.len(); buffer.push_str(text); } } diff --git a/src/printer.rs b/src/printer.rs index 3e5cb16e..ae9823f6 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -150,7 +150,7 @@ impl<'a> InteractivePrinter<'a> { Ok(()) } - fn preprocess(&self, text: &str, cursor: usize) -> String { + fn preprocess(&self, text: &str, cursor: &mut usize) -> String { if self.config.tab_width > 0 { expand(text, self.config.tab_width, cursor) } else { @@ -243,19 +243,15 @@ impl<'a> Printer for InteractivePrinter<'a> { let true_color = self.config.true_color; let colored_output = self.config.colored_output; - write!( - handle, - "{}", - regions - .iter() - .map(|&(style, text)| as_terminal_escaped( - style, - &*self.preprocess(text, 0), - true_color, - colored_output, - )).collect::>() - .join("") - )?; + for &(style, region) in regions.iter() { + let text = &*self.preprocess(region, &mut cursor_total); + write!(handle, "{}", as_terminal_escaped( + style, + &*text, + true_color, + colored_output, + ))?; + } } else { for &(style, region) in regions.iter() { let mut ansi_iterator = AnsiCodeIterator::new(region); @@ -280,8 +276,9 @@ impl<'a> Printer for InteractivePrinter<'a> { (text, false) => { let text = self.preprocess( text.trim_right_matches(|c| c == '\r' || c == '\n'), - cursor_total, + &mut cursor_total, ); + let mut chars = text.chars(); let mut remaining = text.chars().count(); @@ -292,7 +289,6 @@ impl<'a> Printer for InteractivePrinter<'a> { if remaining <= available { let text = chars.by_ref().take(remaining).collect::(); cursor += remaining; - cursor_total += remaining; write!( handle, @@ -330,7 +326,6 @@ impl<'a> Printer for InteractivePrinter<'a> { // It wraps. let text = chars.by_ref().take(available).collect::(); cursor = 0; - cursor_total += available; remaining -= available; write!(