From 9c76b728251c05ecace9d9b1a42830b67894039a Mon Sep 17 00:00:00 2001 From: "Ethan P." Date: Mon, 10 Jun 2024 20:07:54 -0700 Subject: [PATCH] Update expand_tabs to use bat's ANSI iterator --- src/preprocessor.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/preprocessor.rs b/src/preprocessor.rs index 3328f3b6..02d1b289 100644 --- a/src/preprocessor.rs +++ b/src/preprocessor.rs @@ -1,17 +1,18 @@ 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). 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) => { + for seq in EscapeSequenceOffsetsIterator::new(line) { + match seq { + EscapeSequenceOffsets::Text { .. } => { + let mut text = &line[seq.index_of_start()..seq.index_past_end()]; while let Some(index) = text.find('\t') { // Add previous text. if index > 0 { @@ -31,6 +32,10 @@ pub fn expand_tabs(line: &str, width: usize, cursor: &mut usize) -> String { *cursor += text.len(); buffer.push_str(text); } + _ => { + // Copy the ANSI escape sequence. + buffer.push_str(&line[seq.index_of_start()..seq.index_past_end()]) + } } }