diff --git a/src/preprocessor.rs b/src/preprocessor.rs index 5d266abf..f2970429 100644 --- a/src/preprocessor.rs +++ b/src/preprocessor.rs @@ -1,9 +1,8 @@ use console::AnsiCodeIterator; /// 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 cursor = 0; for chunk in AnsiCodeIterator::new(line) { match chunk { diff --git a/src/printer.rs b/src/printer.rs index dc0137da..3e5cb16e 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -149,6 +149,14 @@ impl<'a> InteractivePrinter<'a> { 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> { @@ -201,12 +209,7 @@ impl<'a> Printer for InteractivePrinter<'a> { line_number: usize, line_buffer: &[u8], ) -> Result<()> { - let mut line = String::from_utf8_lossy(&line_buffer).to_string(); - - // Preprocess. - if self.config.tab_width > 0 { - line = expand(&line, self.config.tab_width); - } + let line = String::from_utf8_lossy(&line_buffer).to_string(); // Highlight. 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_max: usize = self.config.term_width; + let mut cursor_total: usize = 0; let mut panel_wrap: Option = None; // Line decorations. @@ -246,7 +250,7 @@ impl<'a> Printer for InteractivePrinter<'a> { .iter() .map(|&(style, text)| as_terminal_escaped( style, - text, + &*self.preprocess(text, 0), true_color, colored_output, )).collect::>() @@ -274,7 +278,10 @@ impl<'a> Printer for InteractivePrinter<'a> { // Regular text. (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 remaining = text.chars().count(); @@ -285,6 +292,7 @@ impl<'a> Printer for InteractivePrinter<'a> { if remaining <= available { let text = chars.by_ref().take(remaining).collect::(); cursor += remaining; + cursor_total += remaining; write!( handle, @@ -322,6 +330,7 @@ impl<'a> Printer for InteractivePrinter<'a> { // It wraps. let text = chars.by_ref().take(available).collect::(); cursor = 0; + cursor_total += available; remaining -= available; write!(