From b4fe182960e45d4fd9ebd5ac2c4d2e4b4522b144 Mon Sep 17 00:00:00 2001 From: "Ethan P." Date: Mon, 10 Jun 2024 19:58:56 -0700 Subject: [PATCH 1/3] Make EscapeSequenceOffsetsIterator pub, add fns to get indices --- src/vscreen.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/vscreen.rs b/src/vscreen.rs index e211c2aa..9e29f9cc 100644 --- a/src/vscreen.rs +++ b/src/vscreen.rs @@ -285,7 +285,7 @@ fn join( /// A range of indices for a raw ANSI escape sequence. #[derive(Debug, PartialEq)] -enum EscapeSequenceOffsets { +pub enum EscapeSequenceOffsets { Text { start: usize, end: usize, @@ -320,6 +320,32 @@ enum EscapeSequenceOffsets { }, } +impl EscapeSequenceOffsets { + /// Returns the byte-index of the first character in the escape sequence. + pub fn index_of_start(&self) -> usize { + use EscapeSequenceOffsets::*; + match self { + Text { start, .. } => *start, + Unknown { start, .. } => *start, + NF { start_sequence, .. } => *start_sequence, + OSC { start_sequence, .. } => *start_sequence, + CSI { start_sequence, .. } => *start_sequence, + } + } + + /// Returns the byte-index past the last character in the escape sequence. + pub fn index_past_end(&self) -> usize { + use EscapeSequenceOffsets::*; + match self { + Text { end, .. } => *end, + Unknown { end, .. } => *end, + NF { end, .. } => *end, + OSC { end, .. } => *end, + CSI { end, .. } => *end, + } + } +} + /// An iterator over the offests of ANSI/VT escape sequences within a string. /// /// ## Example @@ -327,7 +353,7 @@ enum EscapeSequenceOffsets { /// ```ignore /// let iter = EscapeSequenceOffsetsIterator::new("\x1B[33mThis is yellow text.\x1B[m"); /// ``` -struct EscapeSequenceOffsetsIterator<'a> { +pub struct EscapeSequenceOffsetsIterator<'a> { text: &'a str, chars: Peekable>, } From 9c76b728251c05ecace9d9b1a42830b67894039a Mon Sep 17 00:00:00 2001 From: "Ethan P." Date: Mon, 10 Jun 2024 20:07:54 -0700 Subject: [PATCH 2/3] 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()]) + } } } From 243819ecdcf25339741f9d4640e262dea677f239 Mon Sep 17 00:00:00 2001 From: "Ethan P." Date: Sat, 15 Jun 2024 16:12:04 -0700 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61af4459..e81c47e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ - Display which theme is the default one in colored output, see #2838 (@sblondon) - Add aarch64-apple-darwin ("Apple Silicon") binary tarballs to releases, see #2967 (@someposer) - Update the Lisp syntax, see #2970 (@ccqpein) +- Use bat's ANSI iterator during tab expansion, see #2998 (@eth-p) ## Syntaxes