Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Add pre-commit hook to ban manual ANSI escape codes
Maximilian Roos806218d05d3b
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yamlindex 750342f3e..8ea6cacb0 100644--- a/.pre-commit-config.yaml+++ b/.pre-commit-config.yaml@@ -39,6 +39,13 @@ repos:language: pygreptypes: ["rust"]entry: "dbg!"+ - id: no-manual-ansi+ name: no-manual-ansi+ description: Use anstyle/osc8/strip-ansi-escapes instead of manual ANSI codes+ language: pygrep+ types: ["rust"]+ entry: '\\x1b\['+ exclude: '^(tests/|.*_test\.rs$|src/styling/(line|mod)\.rs$|src/output/directive\.rs$)'ci:# Currently network access isn't supported in the CI product.diff --git a/CLAUDE.md b/CLAUDE.mdindex 55ebe35dc..dad103621 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -311,6 +311,7 @@ eprintln!("{}", styled_text);### Design Principles+- **Use the ecosystem, not manual escape codes** - Use `anstyle` for colors, `osc8` for hyperlinks, `strip-ansi-escapes` for stripping. Never manually write ANSI codes (`\x1b[...`) in production code- **Inline over wrappers** - Use `{style}text{style:#}` pattern, not wrapper functions- **Composition over special cases** - Use `.bold()`, `.fg_color()`, not `format_X_with_Y()`- **Semantic constants** - Use `ERROR`, `WARNING`, not raw colorsdiff --git a/Cargo.lock b/Cargo.lockindex 46b2bc4fc..bf0c30080 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1248,6 +1248,12 @@ dependencies = ["hashbrown 0.14.5",]+[[package]]+name = "osc8"+version = "0.1.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "8b2d2086a3a477709f0208181e65857a9c062064ab2b3bb9abaf64f17d148bf3"+[[package]]name = "pathdiff"version = "0.2.3"@@ -2656,6 +2662,7 @@ dependencies = ["log","minijinja","nix 0.30.1",+ "osc8","portable-pty","rayon","regex",diff --git a/Cargo.toml b/Cargo.tomlindex e1574b722..409fd96d9 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -63,6 +63,7 @@ tree-sitter = { version = "0.25", optional = true }tree-sitter-bash = { version = "0.25", optional = true }tree-sitter-highlight = { version = "0.25", optional = true }unicode-width = "0.2"+osc8 = "0.1.0"[target.'cfg(unix)'.dependencies]skim = "0.20"diff --git a/src/commands/list/render.rs b/src/commands/list/render.rsindex 17db0eb4a..7fd536b6d 100644--- a/src/commands/list/render.rs+++ b/src/commands/list/render.rs@@ -598,11 +598,10 @@ mod tests {let rendered = result.render();// Strip ANSI codes to check alignment- let ansi_escape = regex::Regex::new(r"\x1b\[[0-9;]*m").unwrap();- let clean = ansi_escape.replace_all(&rendered, "");+ let clean = strip_ansi_escapes::strip_str(&rendered);// Should be " +1 -1" (with leading space for right-alignment)- assert_eq!(clean.as_ref(), " +1 -1", "Diff should be right-aligned");+ assert_eq!(clean, " +1 -1", "Diff should be right-aligned");}#[test]@@ -638,8 +637,7 @@ mod tests {// The rendered output should have correct spacinglet rendered = line.render();- let ansi_escape = regex::Regex::new(r"\x1b\[[0-9;]*m").unwrap();- let clean = ansi_escape.replace_all(&rendered, "");+ let clean = strip_ansi_escapes::strip_str(&rendered);assert_eq!(clean.width(),24,@@ -823,9 +821,8 @@ mod tests {);assert_eq!(without.width(), total);let rendered_without = without.render();- let ansi_escape = regex::Regex::new(r"\x1b\[[0-9;]*m").unwrap();- let clean_without = ansi_escape.replace_all(&rendered_without, "");- assert_eq!(clean_without.as_ref(), " ", "Should render as blank");+ let clean_without = strip_ansi_escapes::strip_str(&rendered_without);+ assert_eq!(clean_without, " ", "Should render as blank");// With always_show_zeros=true, (0, 0) renders as "↑0 ↓0"let with = format_diff_like_column(@@ -844,12 +841,8 @@ mod tests {);assert_eq!(with.width(), total);let rendered_with = with.render();- let clean_with = ansi_escape.replace_all(&rendered_with, "");- assert_eq!(- clean_with.as_ref(),- "↑0 ↓0 ",- "Should render ↑0 ↓0 with padding"- );+ let clean_with = strip_ansi_escapes::strip_str(&rendered_with);+ assert_eq!(clean_with, "↑0 ↓0 ", "Should render ↑0 ↓0 with padding");}#[test]diff --git a/src/styling/line.rs b/src/styling/line.rsindex 84684f07c..e520531ed 100644--- a/src/styling/line.rs+++ b/src/styling/line.rs@@ -108,7 +108,12 @@ mod tests {// Text with OSC 8 hyperlink should have visual width of just the textlet url = "https://github.com/user/repo/pull/123";let text_content = "●";- let hyperlinked = format!("\x1b]8;;{}\x1b\\{}\x1b]8;;\x1b\\", url, text_content);+ let hyperlinked = format!(+ "{}{}{}",+ osc8::Hyperlink::new(url),+ text_content,+ osc8::Hyperlink::END+ );let styled_str = StyledString::raw(&hyperlinked);assert_eq!(@@ -122,7 +127,9 @@ mod tests {#[test]fn test_width_strips_sgr_codes() {// Text with SGR color codes should have visual width of just the text- let colored = "\x1b[32m●\x1b[0m"; // Green ●+ use anstyle::{AnsiColor, Color, Style};+ let green = Style::new().fg_color(Some(Color::Ansi(AnsiColor::Green)));+ let colored = format!("{}●{}", green.render(), green.render_reset());let styled_str = StyledString::raw(colored);assert_eq!(@@ -136,8 +143,16 @@ mod tests {#[test]fn test_width_with_combined_ansi_codes() {// Text with both color and hyperlink+ use anstyle::{AnsiColor, Color, Style};let url = "https://example.com";- let combined = format!("\x1b[33m\x1b]8;;{}\x1b\\● passed\x1b]8;;\x1b\\\x1b[0m", url);+ let yellow = Style::new().fg_color(Some(Color::Ansi(AnsiColor::Yellow)));+ let combined = format!(+ "{}{}● passed{}{}",+ yellow.render(),+ osc8::Hyperlink::new(url),+ osc8::Hyperlink::END,+ yellow.render_reset()+ );let styled_str = StyledString::raw(&combined);// "● passed" = 1 + 1 (space) + 6 = 8diff --git a/src/styling/mod.rs b/src/styling/mod.rsindex d8e210fbe..ff57c239b 100644--- a/src/styling/mod.rs+++ b/src/styling/mod.rs@@ -32,10 +32,14 @@ pub use line::{StyledLine, StyledString};/// Wrap text with an OSC 8 hyperlink///-/// OSC 8 format: \x1b]8;;URL\x1b\\TEXT\x1b]8;;\x1b\\/// See: <https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda>pub fn hyperlink(text: &str, url: &str) -> String {- format!("\x1b]8;;{}\x1b\\{}\x1b]8;;\x1b\\", url, text)+ format!(+ "{}{}{}",+ osc8::Hyperlink::new(url),+ text,+ osc8::Hyperlink::END+ )}// Re-export for tests@@ -397,8 +401,13 @@ command = "npm install"let url = "https://github.com/user/repo/pull/123";let result = super::hyperlink(text, url);- // OSC 8 format: \x1b]8;;URL\x1b\\TEXT\x1b]8;;\x1b\\- let expected = format!("\x1b]8;;{}\x1b\\{}\x1b]8;;\x1b\\", url, text);+ // Should match osc8 library output+ let expected = format!(+ "{}{}{}",+ osc8::Hyperlink::new(url),+ text,+ osc8::Hyperlink::END+ );assert_eq!(result, expected);// Verify structure