Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

feat: Replace manual ANSI stripping with `strip-ansi-escapes`

Maximilian Roose4f5c180e1e4
diff --git a/Cargo.lock b/Cargo.lock
index 88b8952cb..d6709c6b3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1824,7 +1824,7 @@ dependencies = [
"time",
"timer",
"unicode-width",
- "vte",
+ "vte 0.15.0",
"which",
]
@@ -1855,6 +1855,15 @@ version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a2ae44ef20feb57a68b23d846850f861394c2e02dc425a50098ae8c90267589"
+[[package]]
+name = "strip-ansi-escapes"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2a8f8038e7e7969abb3f1b7c2a811225e9296da208539e0f79c5251d6cac0025"
+dependencies = [
+ "vte 0.14.1",
+]
+
[[package]]
name = "strsim"
version = "0.11.1"
@@ -2230,6 +2239,15 @@ version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
+[[package]]
+name = "vte"
+version = "0.14.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "231fdcd7ef3037e8330d8e17e61011a2c244126acc0a982f4040ac3f9f0bc077"
+dependencies = [
+ "memchr",
+]
+
[[package]]
name = "vte"
version = "0.15.0"
@@ -2647,6 +2665,7 @@ dependencies = [
"shell-escape",
"shellexpand",
"skim",
+ "strip-ansi-escapes",
"strum",
"synoptic",
"tempfile",
diff --git a/Cargo.toml b/Cargo.toml
index 68d83d14e..dc5dab164 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -47,6 +47,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
shell-escape = "0.1"
shellexpand = "3.1"
+strip-ansi-escapes = "0.2"
strum = { version = "0.27", features = ["derive"] }
synoptic = "2"
terminal_size = "0.4"
diff --git a/src/styling/format.rs b/src/styling/format.rs
index 02c31ab63..be431c7bf 100644
--- a/src/styling/format.rs
+++ b/src/styling/format.rs
@@ -11,46 +11,9 @@ use super::highlighting::bash_token_style;
/// Default terminal width fallback if detection fails
const DEFAULT_TERMINAL_WIDTH: usize = 80;
-/// Strip ANSI escape codes from a string to get the visual content
-///
-/// Handles standard ANSI SGR (Select Graphic Rendition) codes: ESC[...m
-///
-/// # Scope
-/// - Designed for git output (log, diff) which produces well-formed SGR sequences
-/// - Only handles ESC[...m (SGR), not cursor movement or other ANSI codes
-/// - Malformed sequences (ESC without [, or ESC[ without m) are preserved in output
+/// Strip ANSI escape codes from a string using strip-ansi-escapes
fn strip_ansi_codes(s: &str) -> String {
- let mut result = String::with_capacity(s.len());
- let mut chars = s.chars().peekable();
-
- while let Some(ch) = chars.next() {
- if ch == '\x1b' && chars.peek() == Some(&'[') {
- // Start of ANSI escape sequence
- chars.next(); // consume '['
-
- // Skip until 'm' (SGR terminator) or end of string
- // Note: If sequence is incomplete (no 'm'), we'll consume rest of iterator
- // and result will be missing that content. This is acceptable for git output
- // which always produces well-formed sequences.
- let mut found_terminator = false;
- for next_ch in chars.by_ref() {
- if next_ch == 'm' {
- found_terminator = true;
- break;
- }
- }
-
- // If sequence was incomplete, we've consumed the rest of the string
- // This is a reasonable failure mode for malformed input
- if !found_terminator {
- break;
- }
- } else {
- result.push(ch);
- }
- }
-
- result
+ strip_ansi_escapes::strip_str(s)
}
/// Calculate visual width of a string, ignoring ANSI escape codes
diff --git a/tests/integration_tests/column_alignment_verification.rs b/tests/integration_tests/column_alignment_verification.rs
index 273c028e3..a2003a98b 100644
--- a/tests/integration_tests/column_alignment_verification.rs
+++ b/tests/integration_tests/column_alignment_verification.rs
@@ -18,8 +18,7 @@ use unicode_width::UnicodeWidthStr;
/// Strip ANSI escape codes from a string
fn strip_ansi_codes(s: &str) -> String {
- let re = regex::Regex::new(r"\x1b\[[0-9;]*m").unwrap();
- re.replace_all(s, "").to_string()
+ strip_ansi_escapes::strip_str(s)
}
/// Represents the start position of each column in a table row