lean_ctx_patterns/ansi.rs
1// SPDX-License-Identifier: Apache-2.0
2
3//! ANSI escape handling shared by the patterns and the engine compressor.
4
5/// Removes ANSI escape codes from a string, returning clean text.
6pub fn strip_ansi(s: &str) -> String {
7 if !s.contains('\x1b') {
8 return s.to_string();
9 }
10 let mut result = String::with_capacity(s.len());
11 let mut in_escape = false;
12 for c in s.chars() {
13 if c == '\x1b' {
14 in_escape = true;
15 continue;
16 }
17 if in_escape {
18 if c.is_ascii_alphabetic() {
19 in_escape = false;
20 }
21 continue;
22 }
23 result.push(c);
24 }
25 result
26}