mirror of
https://github.com/sharkdp/bat
synced 2024-11-23 20:33:06 +00:00
Update expand_tabs to use bat's ANSI iterator
This commit is contained in:
parent
b4fe182960
commit
9c76b72825
1 changed files with 12 additions and 7 deletions
|
@ -1,17 +1,18 @@
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
|
||||||
use console::AnsiCodeIterator;
|
use crate::{
|
||||||
|
nonprintable_notation::NonprintableNotation,
|
||||||
use crate::nonprintable_notation::NonprintableNotation;
|
vscreen::{EscapeSequenceOffsets, EscapeSequenceOffsetsIterator},
|
||||||
|
};
|
||||||
|
|
||||||
/// Expand tabs like an ANSI-enabled expand(1).
|
/// Expand tabs like an ANSI-enabled expand(1).
|
||||||
pub fn expand_tabs(line: &str, width: usize, cursor: &mut usize) -> String {
|
pub fn expand_tabs(line: &str, width: usize, cursor: &mut usize) -> String {
|
||||||
let mut buffer = String::with_capacity(line.len() * 2);
|
let mut buffer = String::with_capacity(line.len() * 2);
|
||||||
|
|
||||||
for chunk in AnsiCodeIterator::new(line) {
|
for seq in EscapeSequenceOffsetsIterator::new(line) {
|
||||||
match chunk {
|
match seq {
|
||||||
(text, true) => buffer.push_str(text),
|
EscapeSequenceOffsets::Text { .. } => {
|
||||||
(mut text, false) => {
|
let mut text = &line[seq.index_of_start()..seq.index_past_end()];
|
||||||
while let Some(index) = text.find('\t') {
|
while let Some(index) = text.find('\t') {
|
||||||
// Add previous text.
|
// Add previous text.
|
||||||
if index > 0 {
|
if index > 0 {
|
||||||
|
@ -31,6 +32,10 @@ pub fn expand_tabs(line: &str, width: usize, cursor: &mut usize) -> String {
|
||||||
*cursor += text.len();
|
*cursor += text.len();
|
||||||
buffer.push_str(text);
|
buffer.push_str(text);
|
||||||
}
|
}
|
||||||
|
_ => {
|
||||||
|
// Copy the ANSI escape sequence.
|
||||||
|
buffer.push_str(&line[seq.index_of_start()..seq.index_past_end()])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue