Skip to content

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

feat: Add ZSH job control opt and enhance ZSH plugin for improved output

Maximilian Roos2a3da6c5a503
diff --git a/Cargo.lock b/Cargo.lock
index 228dbad6d..911bb2b22 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -26,6 +26,25 @@ version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
+[[package]]
+name = "ansi-str"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "060de1453b69f46304b28274f382132f4e72c55637cf362920926a70d090890d"
+dependencies = [
+ "ansitok",
+]
+
+[[package]]
+name = "ansitok"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c0a8acea8c2f1c60f0a92a8cd26bf96ca97db56f10bbcab238bbe0cceba659ee"
+dependencies = [
+ "nom",
+ "vte 0.14.1",
+]
+
[[package]]
name = "anstream"
version = "0.6.21"
@@ -1340,6 +1359,12 @@ dependencies = [
"once_cell",
]
+[[package]]
+name = "minimal-lexical"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
+
[[package]]
name = "mio"
version = "1.1.0"
@@ -1394,6 +1419,16 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451"
+[[package]]
+name = "nom"
+version = "7.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
+dependencies = [
+ "memchr",
+ "minimal-lexical",
+]
+
[[package]]
name = "num-conv"
version = "0.1.0"
@@ -2597,6 +2632,7 @@ version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "231fdcd7ef3037e8330d8e17e61011a2c244126acc0a982f4040ac3f9f0bc077"
dependencies = [
+ "arrayvec",
"memchr",
]
@@ -3002,6 +3038,7 @@ checksum = "f17a85883d4e6d00e8a97c586de764dabcc06133f7f1d55dce5cdc070ad7fe59"
name = "worktrunk"
version = "0.1.5"
dependencies = [
+ "ansi-str",
"anstream",
"anstyle",
"askama",
diff --git a/Cargo.toml b/Cargo.toml
index 70ea03579..0fe5c6483 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -35,6 +35,7 @@ path = "src/main.rs"
[dependencies]
anstream = "0.6"
anstyle = "1.0"
+ansi-str = "0.9"
askama = { version = "0.14", default-features = false, features = ["derive", "std"] }
chrono = "0.4"
clap = { version = "4.5", features = ["derive", "unstable-ext"] }
diff --git a/src/commands/list/collect.rs b/src/commands/list/collect.rs
index 4941c8108..31fc05a33 100644
--- a/src/commands/list/collect.rs
+++ b/src/commands/list/collect.rs
@@ -510,9 +510,10 @@ pub fn collect(
// (which breaks indicatif's line-based cursor math).
let max_width = super::layout::get_safe_list_width();
+ // Clamp helper to keep progress output single-line in narrow terminals.
let clamp = |s: &str| -> String {
- if console::measure_text_width(s) > max_width {
- console::truncate_str(s, max_width, "…").into_owned()
+ if crate::display::visible_width(s) > max_width {
+ crate::display::truncate_visible(s, max_width, "…")
} else {
s.to_owned()
}
@@ -568,9 +569,8 @@ pub fn collect(
.collect();
// Cache last rendered (unclamped) message per row to avoid redundant updates.
- // Store the full string so updates that are truncated for narrow terminals
- // (e.g., CI column at the end) still trigger a refresh when the underlying
- // data changes.
+ // TODO(list-progressive): if we change clamping/detection strategy, keep a test case
+ // for off-screen CI column updates to ensure we still refresh rows.
let mut last_rendered_lines: Vec<String> = vec![String::new(); all_items.len()];
// Footer progress bar with loading status
@@ -716,8 +716,7 @@ pub fn collect(
layout.format_list_item_line(info, current_worktree_path.as_ref());
let clamped = clamp(&rendered);
- // Compare using the full (unclamped) line so updates that fall
- // past the clamp boundary still cause a redraw.
+ // Compare using full line so changes beyond the clamp (e.g., CI) still refresh.
if rendered != last_rendered_lines[item_idx] {
last_rendered_lines[item_idx] = rendered;
pb.set_message(clamped);
diff --git a/src/display.rs b/src/display.rs
index 9eaf6fec8..f2ab2a2ce 100644
--- a/src/display.rs
+++ b/src/display.rs
@@ -6,8 +6,10 @@
//! - Text truncation with word boundaries
//! - Terminal width detection
+use ansi_str::AnsiStr;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
+use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
use worktrunk::path::format_path_for_display;
pub fn format_relative_time(timestamp: i64) -> String {
@@ -139,6 +141,51 @@ pub fn get_terminal_width() -> usize {
.unwrap_or(80)
}
+/// Compute visible width of a styled string by stripping ANSI/OSC first.
+pub fn visible_width(rendered: &str) -> usize {
+ UnicodeWidthStr::width(rendered.ansi_strip().as_ref())
+}
+
+/// Truncate a styled string to a visible width budget, preserving escapes.
+/// Escape sequences (ANSI/OSC) are zero-width; ellipsis is added when truncating.
+/// Appends ESC[0m on truncation to avoid style bleed.
+pub fn truncate_visible(rendered: &str, max_width: usize, ellipsis: &str) -> String {
+ if max_width == 0 {
+ return String::new();
+ }
+
+ let plain = rendered.ansi_strip();
+ let plain_str = plain.as_ref();
+ if UnicodeWidthStr::width(plain_str) <= max_width {
+ return rendered.to_owned();
+ }
+
+ let ellipsis_width = UnicodeWidthStr::width(ellipsis);
+ let budget = max_width.saturating_sub(ellipsis_width);
+ if budget == 0 {
+ let mut out = String::new();
+ out.push_str(ellipsis);
+ out.push_str("\u{1b}[0m");
+ return out;
+ }
+
+ let mut cut_at = 0;
+ let mut width = 0;
+ for (i, ch) in plain_str.char_indices() {
+ let w = UnicodeWidthChar::width(ch).unwrap_or(0);
+ if width + w > budget {
+ break;
+ }
+ width += w;
+ cut_at = i + ch.len_utf8();
+ }
+
+ let mut out = rendered.ansi_cut(..cut_at).into_owned();
+ out.push_str(ellipsis);
+ out.push_str("\u{1b}[0m");
+ out
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -221,4 +268,26 @@ mod tests {
assert!(result.width() <= 20, "Width should be <= 20");
assert!(result.ends_with('…'), "Should end with ellipsis");
}
+
+ #[test]
+ fn visible_width_strips_osc8() {
+ let s = "\u{1b}]8;;https://example.com\u{1b}\\A\u{1b}]8;;\u{1b}\\";
+ assert_eq!(visible_width(s), 1, "OSC-8 should be zero-width");
+ }
+
+ #[test]
+ fn truncate_visible_preserves_budget_and_resets() {
+ let colored = "\u{1b}[31mhello\u{1b}[0m";
+ let out = truncate_visible(colored, 3, "…");
+ assert_eq!(visible_width(&out), 3);
+ assert!(out.ends_with("\u{1b}[0m"));
+ }
+
+ #[test]
+ fn truncate_visible_handles_wide_emoji() {
+ let rocket = "🚀";
+ let out = truncate_visible(rocket, 1, "…");
+ assert_eq!(visible_width(&out), 1);
+ assert!(out.ends_with("\u{1b}[0m"));
+ }
}