on master help sign in
Fixed tab expansion not working in --wrap=never mode.
commitd90797f8e947f01da74fd2aa065f48df3e50ab0b authoreth-p <32112321+eth-p@users.noreply.github.com> dateTue 11 Sep 2018, 13:45 (8 years ago) parentb4096e5
2 files changed+17−22
src/preprocessor.rs+5−5 src/printer.rs+12−17
src/preprocessor.rs+5−5
@@ -1,7 +1,7 @@
11use console::AnsiCodeIterator;
22
33/// Expand tabs like an ANSI-enabled expand(1).
4−pub fn expand(line: &str, width: usize, mut cursor: usize) -> String {
4+pub fn expand(line: &str, width: usize, cursor: &mut usize) -> String {
55 let mut buffer = String::with_capacity(line.len() * 2);
66
77 for chunk in AnsiCodeIterator::new(line) {
@@ -11,20 +11,20 @@ pub fn expand(line: &str, width: usize, mut cursor: usize) -> String {
1111 while let Some(index) = text.find('\t') {
1212 // Add previous text.
1313 if index > 0 {
14− cursor += index;
14+ *cursor += index;
1515 buffer.push_str(&text[0..index]);
1616 }
1717
1818 // Add tab.
19− let spaces = width - (cursor % width);
20− cursor += spaces;
19+ let spaces = width - (*cursor % width);
20+ *cursor += spaces;
2121 buffer.push_str(&*" ".repeat(spaces));
2222
2323 // Next.
2424 text = &text[index + 1..text.len()];
2525 }
2626
27− cursor += text.len();
27+ *cursor += text.len();
2828 buffer.push_str(text);
2929 }
3030 }
src/printer.rs+12−17
@@ -150,7 +150,7 @@ impl<'a> InteractivePrinter<'a> {
150150 Ok(())
151151 }
152152
153− fn preprocess(&self, text: &str, cursor: usize) -> String {
153+ fn preprocess(&self, text: &str, cursor: &mut usize) -> String {
154154 if self.config.tab_width > 0 {
155155 expand(text, self.config.tab_width, cursor)
156156 } else {
@@ -243,19 +243,15 @@ impl<'a> Printer for InteractivePrinter<'a> {
243243 let true_color = self.config.true_color;
244244 let colored_output = self.config.colored_output;
245245
246− write!(
247− handle,
248− "{}",
249− regions
250− .iter()
251− .map(|&(style, text)| as_terminal_escaped(
252− style,
253− &*self.preprocess(text, 0),
254− true_color,
255− colored_output,
256− )).collect::<Vec<_>>()
257− .join("")
258− )?;
246+ for &(style, region) in regions.iter() {
247+ let text = &*self.preprocess(region, &mut cursor_total);
248+ write!(handle, "{}", as_terminal_escaped(
249+ style,
250+ &*text,
251+ true_color,
252+ colored_output,
253+ ))?;
254+ }
259255 } else {
260256 for &(style, region) in regions.iter() {
261257 let mut ansi_iterator = AnsiCodeIterator::new(region);
@@ -280,8 +276,9 @@ impl<'a> Printer for InteractivePrinter<'a> {
280276 (text, false) => {
281277 let text = self.preprocess(
282278 text.trim_right_matches(|c| c == '\r' || c == '\n'),
283− cursor_total,
279+ &mut cursor_total,
284280 );
281+
285282 let mut chars = text.chars();
286283 let mut remaining = text.chars().count();
287284
@@ -292,7 +289,6 @@ impl<'a> Printer for InteractivePrinter<'a> {
292289 if remaining <= available {
293290 let text = chars.by_ref().take(remaining).collect::<String>();
294291 cursor += remaining;
295− cursor_total += remaining;
296292
297293 write!(
298294 handle,
@@ -330,7 +326,6 @@ impl<'a> Printer for InteractivePrinter<'a> {
330326 // It wraps.
331327 let text = chars.by_ref().take(available).collect::<String>();
332328 cursor = 0;
333− cursor_total += available;
334329 remaining -= available;
335330
336331 write!(