2018-09-11 03:11:59 +02:00
|
|
|
use console::AnsiCodeIterator;
|
|
|
|
|
|
|
|
/// Expand tabs like an ANSI-enabled expand(1).
|
2018-09-11 22:45:49 +02:00
|
|
|
pub fn expand(line: &str, width: usize, cursor: &mut usize) -> String {
|
2018-09-11 03:11:59 +02:00
|
|
|
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 {
|
2018-09-11 22:45:49 +02:00
|
|
|
*cursor += index;
|
2018-09-11 03:11:59 +02:00
|
|
|
buffer.push_str(&text[0..index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add tab.
|
2018-09-11 22:45:49 +02:00
|
|
|
let spaces = width - (*cursor % width);
|
|
|
|
*cursor += spaces;
|
2018-09-11 03:11:59 +02:00
|
|
|
buffer.push_str(&*" ".repeat(spaces));
|
|
|
|
|
|
|
|
// Next.
|
|
|
|
text = &text[index + 1..text.len()];
|
|
|
|
}
|
|
|
|
|
2018-09-11 22:45:49 +02:00
|
|
|
*cursor += text.len();
|
2018-09-11 03:11:59 +02:00
|
|
|
buffer.push_str(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer
|
|
|
|
}
|