Skip to content

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

perf(statusline): gate parent-TTY walker behind statusline-only helper (#2260)

Maximilian Roos83cd651fce02
diff --git a/src/commands/statusline.rs b/src/commands/statusline.rs
index a86dbe146..b967205de 100644
--- a/src/commands/statusline.rs
+++ b/src/commands/statusline.rs
@@ -16,7 +16,9 @@ use dunce::canonicalize;
use ansi_str::AnsiStr;
use anyhow::{Context, Result};
use worktrunk::git::Repository;
-use worktrunk::styling::{fix_dim_after_color_reset, terminal_width, truncate_visible};
+use worktrunk::styling::{
+ fix_dim_after_color_reset, terminal_width_for_statusline, truncate_visible,
+};
use super::list::{self, CollectOptions, StatuslineSegment, json_output};
use crate::cli::OutputFormat;
@@ -237,7 +239,7 @@ pub fn run(format: OutputFormat) -> Result<()> {
}
// Fit segments to terminal width using priority-based dropping
- let max_width = terminal_width();
+ let max_width = terminal_width_for_statusline();
// Reserve 1 char for leading space (ellipsis handled by truncate_visible fallback)
let content_budget = max_width.saturating_sub(1);
let fitted_segments = StatuslineSegment::fit_to_width(segments, content_budget);
diff --git a/src/styling/mod.rs b/src/styling/mod.rs
index 26527927a..78c6ff84a 100644
--- a/src/styling/mod.rs
+++ b/src/styling/mod.rs
@@ -75,6 +75,11 @@ pub fn verbosity() -> u8 {
/// When detection fails (piped context, no TTY), returns `usize::MAX` rather than
/// an arbitrary default. Callers that need width-based formatting will produce
/// full output, letting the consumer handle truncation.
+///
+/// Does **not** probe the parent process tree — that fallback is expensive
+/// (spawns `ps` up to 10 times plus `stty`) and only useful for `wt statusline`
+/// under Claude Code, where no TTY is inherited. Statusline calls
+/// [`terminal_width_for_statusline`] instead.
pub fn terminal_width() -> usize {
// Prefer direct terminal detection (more accurate than COLUMNS which may be stale/wrong)
// Check stderr first (status messages), then stdout (table output)
@@ -91,16 +96,37 @@ pub fn terminal_width() -> usize {
return width;
}
- // Try parent TTY detection (Unix only)
- // This is used when running in a subprocess without direct TTY access,
- // such as Claude Code's statusline hook.
+ // Can't detect width — don't truncate, let the consumer handle it
+ usize::MAX
+}
+
+/// Terminal width for `wt statusline`, including a subprocess-compat fallback.
+///
+/// Claude Code invokes `wt statusline` as a subprocess with pipes for stdin,
+/// stdout, and stderr — so [`terminal_width`] always falls through to its
+/// `usize::MAX` sentinel, and the statusline output would overflow the bar.
+/// As a last resort, this walks up to 10 parent processes looking for a TTY
+/// and asks `stty size` for its dimensions, reserving 20% for Claude Code's
+/// own UI messages.
+///
+/// Every other caller should use [`terminal_width`] — the parent-TTY walk is
+/// a statusline-specific workaround, not a general fallback.
+pub fn terminal_width_for_statusline() -> usize {
+ statusline_width_fallback(terminal_width())
+}
+
+/// Apply the parent-TTY fallback to a width returned by [`terminal_width`].
+///
+/// Split from [`terminal_width_for_statusline`] so tests can exercise the
+/// fallback path without racing the process-wide `COLUMNS` env var.
+fn statusline_width_fallback(base: usize) -> usize {
#[cfg(unix)]
- if let Some(width) = detect_parent_tty_width() {
+ if base == usize::MAX
+ && let Some(width) = detect_parent_tty_width()
+ {
return width;
}
-
- // Can't detect width — don't truncate, let the consumer handle it
- usize::MAX
+ base
}
/// Detect terminal width by walking up the process tree to find a TTY.
@@ -183,6 +209,29 @@ mod tests {
use anstyle::Style;
use unicode_width::UnicodeWidthStr;
+ #[test]
+ fn statusline_width_fallback_returns_base_when_known() {
+ // Fast path: if `terminal_width()` found a real width, use it as-is.
+ assert_eq!(statusline_width_fallback(80), 80);
+ assert_eq!(statusline_width_fallback(1), 1);
+ }
+
+ #[test]
+ fn statusline_width_fallback_probes_parent_tty_when_unknown() {
+ // Slow path: `usize::MAX` signals "direct detection failed" — the
+ // helper then walks the process tree. Whether a TTY is found depends
+ // on the test environment, so only assert the return type.
+ let _ = statusline_width_fallback(usize::MAX);
+ }
+
+ #[test]
+ fn terminal_width_for_statusline_returns_a_width() {
+ // End-to-end smoke test. Under cargo test, `COLUMNS=80` is set in
+ // `.cargo/config.toml`, so the fast path returns 80.
+ let width = terminal_width_for_statusline();
+ assert!(width > 0);
+ }
+
#[test]
fn test_toml_formatting() {
let toml_content = r#"worktree-path = "../{{ repo }}.{{ branch }}"