mirror of https://github.com/sharkdp/bat
Added tab expansion preprocessing step.
parent
8b4abb03db
commit
b23ff24ebc
@ -0,0 +1,35 @@
|
||||
use console::AnsiCodeIterator;
|
||||
|
||||
/// Expand tabs like an ANSI-enabled expand(1).
|
||||
pub fn expand(line: &str, width: usize) -> String {
|
||||
let mut buffer = String::with_capacity(line.len() * 2);
|
||||
let mut cursor = 0;
|
||||
|
||||
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));
|
||||
|
||||
// Next.
|
||||
text = &text[index + 1..text.len()];
|
||||
}
|
||||
|
||||
cursor += text.len();
|
||||
buffer.push_str(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
buffer
|
||||
}
|
Loading…
Reference in New Issue