Skip to content

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

fix(list): measure truncated cells by grapheme cluster so VS16 emoji don't overflow (#4682)

Worktrunk Bot5b35ef62f6f2
diff --git a/Cargo.lock b/Cargo.lock
index 1d15b2fae..61170c86f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4796,6 +4796,7 @@ dependencies = [
"tracing-subscriber",
"tree-sitter-bash",
"tree-sitter-highlight",
+ "unicode-segmentation",
"unicode-width 0.2.3",
"urlencoding",
"vergen-gitcl",
diff --git a/Cargo.toml b/Cargo.toml
index c04ca5997..9a9c12abe 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -224,6 +224,7 @@ toml_edit = { version = "0.25", features = ["serde"] }
# To build without C compilation requirements, use: cargo install worktrunk --no-default-features --features cli
tree-sitter-bash = { version = "0.25.1", optional = true }
tree-sitter-highlight = { version = "0.27", optional = true }
+unicode-segmentation = "1.12"
unicode-width = "0.2.1"
wrap-ansi = "0.1"
osc8 = "0.1.0"
diff --git a/src/styling/line.rs b/src/styling/line.rs
index e7f9d014d..953d98d19 100644
--- a/src/styling/line.rs
+++ b/src/styling/line.rs
@@ -4,14 +4,15 @@
use ansi_str::AnsiStr;
use anstyle::Style;
-use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
+use unicode_segmentation::UnicodeSegmentation;
+use unicode_width::UnicodeWidthStr;
/// Truncate a styled string to a visible width budget, preserving escapes.
///
/// The budget counts what the terminal draws: escape sequences (ANSI/OSC) are
-/// zero-width, and characters are measured by their unicode width, so a cut
-/// never lands inside an escape and never overshoots on wide characters. Text
-/// that already fits is returned untouched.
+/// zero-width, and grapheme clusters are measured by their unicode width, so a
+/// cut never lands inside an escape or a cluster and never overshoots on wide
+/// characters. Text that already fits is returned untouched.
///
/// A cut ends the kept prefix with "…", after trimming the trailing whitespace
/// the cut exposed so no space hangs before the marker. Trimming is
@@ -48,13 +49,15 @@ fn truncate_visible_with_ellipsis(rendered: &str, max_width: usize, ellipsis: &s
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);
+ // Measure whole grapheme clusters: an emoji presentation sequence such as
+ // `♻\u{fe0f}` draws two cells, though its chars measure 1 + 0.
+ for (i, g) in plain_str.grapheme_indices(true) {
+ let w = UnicodeWidthStr::width(g);
if width + w > budget {
break;
}
width += w;
- cut_at = i + ch.len_utf8();
+ cut_at = i + g.len();
}
let mut out = rendered.ansi_cut(..cut_at).into_owned();
@@ -257,6 +260,14 @@ mod tests {
// A wide character straddling the cut is dropped, not split
assert_eq!(truncate_visible("café ☕ and more", 7), "café…");
+ // A VS16 emoji presentation sequence draws two cells, not one, and is
+ // kept or dropped whole
+ assert_eq!(
+ truncate_visible("♻\u{fe0f} refactor: something long", 12),
+ "♻\u{fe0f} refactor…"
+ );
+ assert_eq!(truncate_visible("♻\u{fe0f}♻\u{fe0f}", 3), "♻\u{fe0f}…");
+
// Zero width → empty
assert!(truncate_visible("hello world", 0).is_empty());