on master help sign in
Moved tab expansion to happen after syntax highlighting.
commitb4096e562726292fa61bf9548b6ab055df31c898 authoreth-p <32112321+eth-p@users.noreply.github.com> dateTue 11 Sep 2018, 13:02 (8 years ago) parenteb6e43b
2 files changed+18−10
src/preprocessor.rs+1−2 src/printer.rs+17−8
src/preprocessor.rs+1−2
@@ -1,9 +1,8 @@
11use console::AnsiCodeIterator;
22
33/// Expand tabs like an ANSI-enabled expand(1).
4−pub fn expand(line: &str, width: usize) -> String {
4+pub fn expand(line: &str, width: usize, mut cursor: usize) -> String {
55 let mut buffer = String::with_capacity(line.len() * 2);
6− let mut cursor = 0;
76
87 for chunk in AnsiCodeIterator::new(line) {
98 match chunk {
src/printer.rs+17−8
@@ -149,6 +149,14 @@ impl<'a> InteractivePrinter<'a> {
149149
150150 Ok(())
151151 }
152+
153+ fn preprocess(&self, text: &str, cursor: usize) -> String {
154+ if self.config.tab_width > 0 {
155+ expand(text, self.config.tab_width, cursor)
156+ } else {
157+ text.to_string()
158+ }
159+ }
152160}
153161
154162impl<'a> Printer for InteractivePrinter<'a> {
@@ -201,12 +209,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
201209 line_number: usize,
202210 line_buffer: &[u8],
203211 ) -> Result<()> {
204− let mut line = String::from_utf8_lossy(&line_buffer).to_string();
205−
206− // Preprocess.
207− if self.config.tab_width > 0 {
208− line = expand(&line, self.config.tab_width);
209− }
212+ let line = String::from_utf8_lossy(&line_buffer).to_string();
210213
211214 // Highlight.
212215 let regions = self.highlighter.highlight(line.as_ref());
@@ -218,6 +221,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
218221
219222 let mut cursor: usize = 0;
220223 let mut cursor_max: usize = self.config.term_width;
224+ let mut cursor_total: usize = 0;
221225 let mut panel_wrap: Option<String> = None;
222226
223227 // Line decorations.
@@ -246,7 +250,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
246250 .iter()
247251 .map(|&(style, text)| as_terminal_escaped(
248252 style,
249− text,
253+ &*self.preprocess(text, 0),
250254 true_color,
251255 colored_output,
252256 )).collect::<Vec<_>>()
@@ -274,7 +278,10 @@ impl<'a> Printer for InteractivePrinter<'a> {
274278
275279 // Regular text.
276280 (text, false) => {
277− let text = text.trim_right_matches(|c| c == '\r' || c == '\n');
281+ let text = self.preprocess(
282+ text.trim_right_matches(|c| c == '\r' || c == '\n'),
283+ cursor_total,
284+ );
278285 let mut chars = text.chars();
279286 let mut remaining = text.chars().count();
280287
@@ -285,6 +292,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
285292 if remaining <= available {
286293 let text = chars.by_ref().take(remaining).collect::<String>();
287294 cursor += remaining;
295+ cursor_total += remaining;
288296
289297 write!(
290298 handle,
@@ -322,6 +330,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
322330 // It wraps.
323331 let text = chars.by_ref().take(available).collect::<String>();
324332 cursor = 0;
333+ cursor_total += available;
325334 remaining -= available;
326335
327336 write!(