on v0.7.1 help sign in
bat/src/preprocessor.rs
34 lines 1.0 kB history raw
1use console::AnsiCodeIterator;
3/// Expand tabs like an ANSI-enabled expand(1).
4pub fn expand(line: &str, width: usize, cursor: &mut usize) -> String {
5 let mut buffer = String::with_capacity(line.len() * 2);
7 for chunk in AnsiCodeIterator::new(line) {
8 match chunk {
9 (text, true) => buffer.push_str(text),
10 (mut text, false) => {
11 while let Some(index) = text.find('\t') {
12 // Add previous text.
13 if index > 0 {
14 *cursor += index;
15 buffer.push_str(&text[0..index]);
16 }
18 // Add tab.
19 let spaces = width - (*cursor % width);
20 *cursor += spaces;
21 buffer.push_str(&*" ".repeat(spaces));
23 // Next.
24 text = &text[index + 1..text.len()];
25 }
27 *cursor += text.len();
28 buffer.push_str(text);
29 }
30 }
31 }
33 buffer