diff options
Diffstat (limited to 'src/preprocessor.rs')
-rw-r--r-- | src/preprocessor.rs | 45 |
1 files changed, 27 insertions, 18 deletions
diff --git a/src/preprocessor.rs b/src/preprocessor.rs index 6e775595..74590355 100644 --- a/src/preprocessor.rs +++ b/src/preprocessor.rs @@ -1,27 +1,36 @@ use std::fmt::Write; +use console::AnsiCodeIterator; + /// Expand tabs like an ANSI-enabled expand(1). -pub fn expand_tabs(mut text: &str, width: usize, cursor: &mut usize) -> String { - let mut buffer = String::with_capacity(text.len() * 2); - - while let Some(index) = text.find('\t') { - // Add previous text. - if index > 0 { - *cursor += index; - buffer.push_str(&text[0..index]); - } +pub fn expand_tabs(line: &str, width: usize, cursor: &mut usize) -> String { + let mut buffer = String::with_capacity(line.len() * 2); + + for chunk in AnsiCodeIterator::new(line) { + match chunk { + (text, true) => buffer.push_str(text), + (mut text, false) => { + while let Some(index) = text.find('\t') { + // Add previous text. + if index > 0 { + *cursor += index; + buffer.push_str(&text[0..index]); + } - // Add tab. - let spaces = width - (*cursor % width); - *cursor += spaces; - buffer.push_str(&*" ".repeat(spaces)); + // Add tab. + let spaces = width - (*cursor % width); + *cursor += spaces; + buffer.push_str(&*" ".repeat(spaces)); - // Next. - text = &text[index + 1..text.len()]; - } + // Next. + text = &text[index + 1..text.len()]; + } - *cursor += text.len(); - buffer.push_str(text); + *cursor += text.len(); + buffer.push_str(text); + } + } + } buffer } |