Skip to content

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

Refactor terminal width detection priority

Maximilian Roos980e2374f639
diff --git a/src/styling/mod.rs b/src/styling/mod.rs
index 848acdf8c..6d31b5556 100644
--- a/src/styling/mod.rs
+++ b/src/styling/mod.rs
@@ -37,20 +37,27 @@ const DEFAULT_TERMINAL_WIDTH: usize = 80;
/// Get terminal width, defaulting to 80 if detection fails
///
-/// Checks COLUMNS environment variable first (for testing and scripts),
-/// then falls back to actual terminal size detection.
+/// Prefers direct terminal size detection over COLUMNS environment variable,
+/// because tools like cargo may set COLUMNS incorrectly.
+///
+/// Checks stderr first since that's where table output goes, then stdout.
pub fn get_terminal_width() -> usize {
- // Check COLUMNS environment variable first (for testing and scripts)
+ // Prefer direct terminal detection (more accurate than COLUMNS which may be stale/wrong)
+ // Check stderr first (where table output goes), then stdout
+ if let Some((terminal_size::Width(w), _)) =
+ terminal_size::terminal_size_of(std::io::stderr()).or_else(terminal_size::terminal_size)
+ {
+ return w as usize;
+ }
+
+ // Fall back to COLUMNS env var (for scripts, piped contexts, or when detection fails)
if let Ok(cols) = std::env::var("COLUMNS")
&& let Ok(width) = cols.parse::<usize>()
{
return width;
}
- // Fall back to actual terminal size
- terminal_size::terminal_size()
- .map(|(terminal_size::Width(w), _)| w as usize)
- .unwrap_or(DEFAULT_TERMINAL_WIDTH)
+ DEFAULT_TERMINAL_WIDTH
}
/// Calculate visual width of a string, ignoring ANSI escape codes
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__select__select_abort_escape.snap b/tests/integration_tests/snapshots/integration__integration_tests__select__select_abort_escape.snap
index fc304de2f..055778c35 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__select__select_abort_escape.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__select__select_abort_escape.snap
@@ -3,8 +3,8 @@ source: tests/integration_tests/select.rs
expression: normalized
---
> [QUERY] │[PREVIEW_HEADER]
- Branch Status HEAD± main↕ Path Remote⇅ Com│Enter: switch | Esc: cancel | ctrl-u/d: scroll | alt-p: tog
-> @ main ^ . a..│
+ Branch Status HEAD± main↕ Remote⇅ Commit │Enter: switch | Esc: cancel | ctrl-u/d: scroll | alt-p: tog
+> @ main ^ a1e809f5 │
│⚪ main has no uncommitted changes
│
│
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__select__select_multiple_worktrees.snap b/tests/integration_tests/snapshots/integration__integration_tests__select__select_multiple_worktrees.snap
index dfdfaf837..602c216f2 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__select__select_multiple_worktrees.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__select__select_multiple_worktrees.snap
@@ -3,10 +3,10 @@ source: tests/integration_tests/select.rs
expression: normalized
---
> [QUERY] │[PREVIEW_HEADER]
- Branch Status HEAD± main↕ Remote⇅ Comm│Enter: switch | Esc: cancel | ctrl-u/d: scroll | alt-p: tog
-> @ main ^ a1..│
- + feature-one _ a1..│⚪ main has no uncommitted changes
- + feature-two _ a1..│
+ Branch Status HEAD± main↕ Remote⇅ Age │Enter: switch | Esc: cancel | ctrl-u/d: scroll | alt-p: tog
+> @ main ^ 1d │
+ + feature-one _ 1d │⚪ main has no uncommitted changes
+ + feature-two _ 1d │
│
│
│
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__select__select_with_branches.snap b/tests/integration_tests/snapshots/integration__integration_tests__select__select_with_branches.snap
index abdd4801a..a304d5229 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__select__select_with_branches.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__select__select_with_branches.snap
@@ -4,9 +4,9 @@ expression: normalized
---
> [QUERY] │[PREVIEW_HEADER]
Branch Status HEAD± main↕ Remote⇅ │Enter: switch | Esc: cancel | ctrl-u/d: scroll | alt-p: tog
-> @ main ^ ..│
- + active-worktree _ ..│⚪ main has no uncommitted changes
- orphan-branch /_ ..│
+> @ main ^ │
+ + active-worktree _ │⚪ main has no uncommitted changes
+ orphan-branch /_ │
│
│
│