Debian Codegraph @

debian/rust-vendor/console/src/ansi.rs

raw
1 #[cfg(feature = "alloc")]
2 use alloc::{borrow::Cow, string::String};
3 use core::{
4 fmt::Display,
5 iter::{FusedIterator, Peekable},
6 str::CharIndices,
7 };
8
9 #[derive(Debug, Clone, Copy, Default)]
10 enum State {
11 #[default]
12 Start,
13 S1,
14 S2,
15 S3,
16 S4,
17 S5,
18 S6,
19 S7,
20 S8,
21 S9,
22 S10,
23 S11,
24 Trap,
25 }
26
27 impl State {
28 fn is_final(&self) -> bool {
29 #[allow(clippy::match_like_matches_macro)]
30 match self {
31 Self::S3 | Self::S5 | Self::S6 | Self::S7 | Self::S8 | Self::S9 | Self::S11 => true,
32 _ => false,
33 }
34 }
35
36 fn is_trapped(&self) -> bool {
37 #[allow(clippy::match_like_matches_macro)]
38 match self {
39 Self::Trap => true,
40 _ => false,
41 }
42 }
43
44 fn transition(&mut self, c: char) {
45 *self = match c {
46 '\u{1b}' | '\u{9b}' => match self {
47 Self::Start => Self::S1,
48 _ => Self::Trap,
49 },
50 '(' | ')' => match self {
51 Self::S1 => Self::S2,
52 Self::S2 | Self::S4 => Self::S4,
53 _ => Self::Trap,
54 },
55 ';' => match self {
56 Self::S1 | Self::S2 | Self::S4 => Self::S4,
57 Self::S5 | Self::S6 | Self::S7 | Self::S8 | Self::S10 => Self::S10,
58 _ => Self::Trap,
59 },
60
61 '[' | '#' | '?' => match self {
62 Self::S1 | Self::S2 | Self::S4 => Self::S4,
63 _ => Self::Trap,
64 },
65 '0'..='2' => match self {
66 Self::S1 | Self::S4 => Self::S5,
67 Self::S2 => Self::S3,
68 Self::S5 => Self::S6,
69 Self::S6 => Self::S7,
70 Self::S7 => Self::S8,
71 Self::S8 => Self::S9,
72 Self::S10 => Self::S5,
73 _ => Self::Trap,
74 },
75 '3'..='9' => match self {
76 Self::S1 | Self::S4 => Self::S5,
77 Self::S2 => Self::S5,
78 Self::S5 => Self::S6,
79 Self::S6 => Self::S7,
80 Self::S7 => Self::S8,
81 Self::S8 => Self::S9,
82 Self::S10 => Self::S5,
83 _ => Self::Trap,
84 },
85 'A'..='P' | 'R' | 'Z' | 'c' | 'f'..='n' | 'q' | 'r' | 'y' | '=' | '>' | '<' => {
86 match self {
87 Self::S1
88 | Self::S2
89 | Self::S4
90 | Self::S5
91 | Self::S6
92 | Self::S7
93 | Self::S8
94 | Self::S10 => Self::S11,
95 _ => Self::Trap,
96 }
97 }
98 _ => Self::Trap,
99 };
100 }
101 }
102
103 #[derive(Debug)]
104 struct Matches<'a> {
105 s: &'a str,
106 it: Peekable<CharIndices<'a>>,
107 }
108
109 impl<'a> Matches<'a> {
110 fn new(s: &'a str) -> Self {
111 let it = s.char_indices().peekable();
112 Self { s, it }
113 }
114 }
115
116 #[derive(Debug)]
117 struct Match<'a> {
118 text: &'a str,
119 start: usize,
120 end: usize,
121 }
122
123 impl<'a> Match<'a> {
124 #[inline]
125 pub(crate) fn as_str(&self) -> &'a str {
126 &self.text[self.start..self.end]
127 }
128 }
129
130 impl<'a> Iterator for Matches<'a> {
131 type Item = Match<'a>;
132
133 fn next(&mut self) -> Option<Self::Item> {
134 find_ansi_code_exclusive(&mut self.it).map(|(start, end)| Match {
135 text: self.s,
136 start,
137 end,
138 })
139 }
140 }
141
142 impl FusedIterator for Matches<'_> {}
143
144 fn find_ansi_code_exclusive(it: &mut Peekable<CharIndices>) -> Option<(usize, usize)> {
145 'outer: loop {
146 if let (start, '\u{1b}') | (start, '\u{9b}') = it.peek()? {
147 let start = *start;
148 let mut state = State::default();
149 let mut maybe_end = None;
150
151 loop {
152 let item = it.peek();
153
154 if let Some((idx, c)) = item {
155 state.transition(*c);
156
157 if state.is_final() {
158 maybe_end = Some(*idx);
159 }
160 }
161
162 // The match is greedy so run till we hit the trap state no matter what. A valid
163 // match is just one that was final at some point
164 if state.is_trapped() || item.is_none() {
165 match maybe_end {
166 Some(end) => {
167 // All possible final characters are a single byte so it's safe to make
168 // the end exclusive by just adding one
169 return Some((start, end + 1));
170 }
171 // The character we are peeking right now might be the start of a match so
172 // we want to continue the loop without popping off that char
173 None => continue 'outer,
174 }
175 }
176
177 it.next();
178 }
179 }
180
181 it.next();
182 }
183 }
184
185 /// Helper function to strip ansi codes.
186 #[cfg(feature = "alloc")]
187 pub fn strip_ansi_codes(s: &str) -> Cow<'_, str> {
188 let mut char_it = s.char_indices().peekable();
189 match find_ansi_code_exclusive(&mut char_it) {
190 Some(_) => {
191 let stripped: String = AnsiCodeIterator::new(s)
192 .filter_map(|(text, is_ansi)| if is_ansi { None } else { Some(text) })
193 .collect();
194 Cow::Owned(stripped)
195 }
196 None => Cow::Borrowed(s),
197 }
198 }
199
200 /// A wrapper struct that implements [`core::fmt::Display`], only displaying non-ansi parts.
201 pub struct WithoutAnsi<'a> {
202 str: &'a str,
203 }
204
205 impl<'a> WithoutAnsi<'a> {
206 pub fn new(str: &'a str) -> Self {
207 Self { str }
208 }
209 }
210
211 impl Display for WithoutAnsi<'_> {
212 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
213 for (str, is_ansi) in AnsiCodeIterator::new(self.str) {
214 if !is_ansi {
215 f.write_str(str)?;
216 }
217 }
218 Ok(())
219 }
220 }
221
222 /// An iterator over ansi codes in a string.
223 ///
224 /// This type can be used to scan over ansi codes in a string.
225 /// It yields tuples in the form `(s, is_ansi)` where `s` is a slice of
226 /// the original string and `is_ansi` indicates if the slice contains
227 /// ansi codes or string values.
228 pub struct AnsiCodeIterator<'a> {
229 s: &'a str,
230 pending_item: Option<(&'a str, bool)>,
231 last_idx: usize,
232 cur_idx: usize,
233 iter: Matches<'a>,
234 }
235
236 impl<'a> AnsiCodeIterator<'a> {
237 /// Creates a new ansi code iterator.
238 pub fn new(s: &'a str) -> AnsiCodeIterator<'a> {
239 AnsiCodeIterator {
240 s,
241 pending_item: None,
242 last_idx: 0,
243 cur_idx: 0,
244 iter: Matches::new(s),
245 }
246 }
247
248 /// Returns the string slice up to the current match.
249 pub fn current_slice(&self) -> &str {
250 &self.s[..self.cur_idx]
251 }
252
253 /// Returns the string slice from the current match to the end.
254 pub fn rest_slice(&self) -> &str {
255 &self.s[self.cur_idx..]
256 }
257 }
258
259 impl<'a> Iterator for AnsiCodeIterator<'a> {
260 type Item = (&'a str, bool);
261
262 fn next(&mut self) -> Option<(&'a str, bool)> {
263 if let Some(pending_item) = self.pending_item.take() {
264 self.cur_idx += pending_item.0.len();
265 Some(pending_item)
266 } else if let Some(m) = self.iter.next() {
267 let s = &self.s[self.last_idx..m.start];
268 self.last_idx = m.end;
269 if s.is_empty() {
270 self.cur_idx = m.end;
271 Some((m.as_str(), true))
272 } else {
273 self.cur_idx = m.start;
274 self.pending_item = Some((m.as_str(), true));
275 Some((s, false))
276 }
277 } else if self.last_idx < self.s.len() {
278 let rv = &self.s[self.last_idx..];
279 self.cur_idx = self.s.len();
280 self.last_idx = self.s.len();
281 Some((rv, false))
282 } else {
283 None
284 }
285 }
286 }
287
288 impl FusedIterator for AnsiCodeIterator<'_> {}
289
290 #[cfg(test)]
291 mod tests {
292 use super::*;
293
294 use core::fmt::Write;
295 use proptest::prelude::*;
296 use regex::Regex;
297 use std::sync::OnceLock;
298
299 // The manual dfa `State` is a handwritten translation from the previously used regex. That
300 // regex is kept here and used to ensure that the new matches are the same as the old
301 fn strip_ansi_re() -> &'static Regex {
302 static RE: OnceLock<Regex> = OnceLock::new();
303
304 RE.get_or_init(|| Regex::new(
305 r"[\x1b\x9b]([()][012AB]|[\[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-PRZcf-nqry=><])",
306 ).unwrap())
307 }
308
309 impl<'a> PartialEq<Match<'a>> for regex::Match<'_> {
310 fn eq(&self, other: &Match<'a>) -> bool {
311 self.start() == other.start && self.end() == other.end
312 }
313 }
314
315 proptest! {
316 #[test]
317 fn dfa_matches_old_regex(s in r"([\x1b\x9b]?.*){0,5}") {
318 let old_matches: Vec<_> = strip_ansi_re().find_iter(&s).collect();
319 let new_matches: Vec<_> = Matches::new(&s).collect();
320 assert_eq!(old_matches, new_matches);
321 }
322 }
323
324 #[test]
325 fn dfa_matches_regex_on_small_strings() {
326 // To make sure the test runs in a reasonable time this is a slimmed down list of
327 // characters to reduce the groups that are only used with each other along with one
328 // arbitrarily chosen character not used in the regex (' ')
329 const POSSIBLE_BYTES: &[u8] = &[b' ', 0x1b, 0x9b, b'(', b'0', b'[', b';', b'3', b'C'];
330
331 fn check_all_strings_of_len(len: usize) {
332 _check_all_strings_of_len(len, &mut Vec::with_capacity(len));
333 }
334
335 fn _check_all_strings_of_len(len: usize, chunk: &mut Vec<u8>) {
336 if len == 0 {
337 if let Ok(s) = core::str::from_utf8(chunk) {
338 let old_matches: Vec<_> = strip_ansi_re().find_iter(s).collect();
339 let new_matches: Vec<_> = Matches::new(s).collect();
340 assert_eq!(old_matches, new_matches);
341 }
342
343 return;
344 }
345
346 for b in POSSIBLE_BYTES {
347 chunk.push(*b);
348 _check_all_strings_of_len(len - 1, chunk);
349 chunk.pop();
350 }
351 }
352
353 for str_len in 0..=6 {
354 check_all_strings_of_len(str_len);
355 }
356 }
357
358 #[test]
359 fn complex_data() {
360 let s = std::fs::read_to_string(
361 std::path::Path::new("tests")
362 .join("data")
363 .join("sample_zellij_session.log"),
364 )
365 .unwrap();
366
367 let old_matches: Vec<_> = strip_ansi_re().find_iter(&s).collect();
368 let new_matches: Vec<_> = Matches::new(&s).collect();
369 assert_eq!(old_matches, new_matches);
370 }
371
372 #[test]
373 fn state_machine() {
374 let ansi_code = "\x1b)B";
375 let mut state = State::default();
376 assert!(!state.is_final());
377
378 for c in ansi_code.chars() {
379 state.transition(c);
380 }
381 assert!(state.is_final());
382
383 state.transition('A');
384 assert!(state.is_trapped());
385 }
386
387 #[test]
388 fn back_to_back_entry_char() {
389 let s = "\x1b\x1bf";
390 let matches: Vec<_> = Matches::new(s).map(|m| m.as_str()).collect();
391 assert_eq!(&["\x1bf"], matches.as_slice());
392 }
393
394 #[test]
395 fn early_paren_can_use_many_chars() {
396 let s = "\x1b(C";
397 let matches: Vec<_> = Matches::new(s).map(|m| m.as_str()).collect();
398 assert_eq!(&[s], matches.as_slice());
399 }
400
401 #[test]
402 fn long_run_of_digits() {
403 let s = "\u{1b}00000";
404 let matches: Vec<_> = Matches::new(s).map(|m| m.as_str()).collect();
405 assert_eq!(&[s], matches.as_slice());
406 }
407
408 #[test]
409 fn test_without_ansi() {
410 let str_with_ansi = "\x1b[1;97;41mError\x1b[0m";
411 let without_ansi = WithoutAnsi::new(str_with_ansi);
412 for _ in 0..2 {
413 let mut output = String::default();
414 write!(output, "{without_ansi}").unwrap();
415 assert_eq!(output, "Error");
416 }
417 }
418
419 #[test]
420 fn test_ansi_iter_re_vt100() {
421 let s = "\x1b(0lpq\x1b)Benglish";
422 let mut iter = AnsiCodeIterator::new(s);
423 assert_eq!(iter.next(), Some(("\x1b(0", true)));
424 assert_eq!(iter.next(), Some(("lpq", false)));
425 assert_eq!(iter.next(), Some(("\x1b)B", true)));
426 assert_eq!(iter.next(), Some(("english", false)));
427 }
428
429 #[test]
430 fn test_ansi_iter_re() {
431 use crate::style;
432 let s = format!("Hello {}!", style("World").red().force_styling(true));
433 let mut iter = AnsiCodeIterator::new(&s);
434 assert_eq!(iter.next(), Some(("Hello ", false)));
435 assert_eq!(iter.current_slice(), "Hello ");
436 assert_eq!(iter.rest_slice(), "\x1b[31mWorld\x1b[0m!");
437 assert_eq!(iter.next(), Some(("\x1b[31m", true)));
438 assert_eq!(iter.current_slice(), "Hello \x1b[31m");
439 assert_eq!(iter.rest_slice(), "World\x1b[0m!");
440 assert_eq!(iter.next(), Some(("World", false)));
441 assert_eq!(iter.current_slice(), "Hello \x1b[31mWorld");
442 assert_eq!(iter.rest_slice(), "\x1b[0m!");
443 assert_eq!(iter.next(), Some(("\x1b[0m", true)));
444 assert_eq!(iter.current_slice(), "Hello \x1b[31mWorld\x1b[0m");
445 assert_eq!(iter.rest_slice(), "!");
446 assert_eq!(iter.next(), Some(("!", false)));
447 assert_eq!(iter.current_slice(), "Hello \x1b[31mWorld\x1b[0m!");
448 assert_eq!(iter.rest_slice(), "");
449 assert_eq!(iter.next(), None);
450 }
451
452 #[test]
453 fn test_ansi_iter_re_on_multi() {
454 use crate::style;
455 let s = format!("{}", style("a").red().bold().force_styling(true));
456 let mut iter = AnsiCodeIterator::new(&s);
457 assert_eq!(iter.next(), Some(("\x1b[31m", true)));
458 assert_eq!(iter.current_slice(), "\x1b[31m");
459 assert_eq!(iter.rest_slice(), "\x1b[1ma\x1b[0m");
460 assert_eq!(iter.next(), Some(("\x1b[1m", true)));
461 assert_eq!(iter.current_slice(), "\x1b[31m\x1b[1m");
462 assert_eq!(iter.rest_slice(), "a\x1b[0m");
463 assert_eq!(iter.next(), Some(("a", false)));
464 assert_eq!(iter.current_slice(), "\x1b[31m\x1b[1ma");
465 assert_eq!(iter.rest_slice(), "\x1b[0m");
466 assert_eq!(iter.next(), Some(("\x1b[0m", true)));
467 assert_eq!(iter.current_slice(), "\x1b[31m\x1b[1ma\x1b[0m");
468 assert_eq!(iter.rest_slice(), "");
469 assert_eq!(iter.next(), None);
470 }
471 }