Skip to content

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

feat: Respect COLUMNS env var for terminal width

Maximilian Roose0402b2940aa
diff --git a/src/styling.rs b/src/styling.rs
index edd9c644e..e9578ccc5 100644
--- a/src/styling.rs
+++ b/src/styling.rs
@@ -199,6 +199,24 @@ impl StyledLine {
/// Default terminal width fallback if detection fails
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.
+fn get_terminal_width() -> usize {
+ // Check COLUMNS environment variable first (for testing and scripts)
+ 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)
+}
+
/// Wraps text at word boundaries to fit within the specified width
///
/// # Arguments
@@ -283,12 +301,8 @@ pub fn format_with_gutter(content: &str, left_margin: &str, max_width: Option<us
let gutter = Style::new().bg_color(Some(Color::Ansi(AnsiColor::Black)));
let mut output = String::new();
- // Use provided width or detect terminal width
- let term_width = max_width.unwrap_or_else(|| {
- terminal_size::terminal_size()
- .map(|(w, _)| w.0 as usize)
- .unwrap_or(DEFAULT_TERMINAL_WIDTH)
- });
+ // Use provided width or detect terminal width (respects COLUMNS env var)
+ let term_width = max_width.unwrap_or_else(get_terminal_width);
// Account for gutter (1) + space (1) + left_margin
let left_margin_width = left_margin.width();
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index 9bc7adae3..0fc56f7b9 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -120,6 +120,11 @@ impl TestRepo {
cmd.env("CLICOLOR_FORCE", "1");
// Set isolated config path to prevent polluting user's config
cmd.env("WORKTRUNK_CONFIG_PATH", &self.test_config_path);
+ // Set consistent terminal width for stable snapshot output
+ // (can be overridden by individual tests that want to test specific widths)
+ if std::env::var("COLUMNS").is_err() {
+ cmd.env("COLUMNS", "150");
+ }
}
/// Get the root path of the repository
diff --git a/tests/snapshots/integration__integration_tests__list__locked_no_reason.snap b/tests/snapshots/integration__integration_tests__list__locked_no_reason.snap
index 753ebfa7b..d214babbb 100644
--- a/tests/snapshots/integration__integration_tests__list__locked_no_reason.snap
+++ b/tests/snapshots/integration__integration_tests__list__locked_no_reason.snap
@@ -6,21 +6,23 @@ info:
- list
env:
CLICOLOR_FORCE: "1"
+ COLUMNS: "150"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
GIT_CONFIG_GLOBAL: /dev/null
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpZrYQbj/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpKMuXWG/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
-Branch  Age  Commit  State  Path
-main  10 months ago b834638e ./test-repo
-locked-no-reason 10 months ago b834638e (locked) ./locked-no-reason
+Branch  Age  Commit  Message  State  Path
+main  10 months ago b834638e Initial commit ./test-repo
+locked-no-reason 10 months ago b834638e Initial commit (locked) ./locked-no-reason
Showing 2 worktrees
diff --git a/tests/snapshots/integration__integration_tests__list__locked_worktree.snap b/tests/snapshots/integration__integration_tests__list__locked_worktree.snap
index d6a40b267..c57e731cd 100644
--- a/tests/snapshots/integration__integration_tests__list__locked_worktree.snap
+++ b/tests/snapshots/integration__integration_tests__list__locked_worktree.snap
@@ -6,21 +6,23 @@ info:
- list
env:
CLICOLOR_FORCE: "1"
+ COLUMNS: "150"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
GIT_CONFIG_GLOBAL: /dev/null
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpDGUhFz/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp8CpaX3/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
-Branch  Age  Commit  Message  Path
-main  10 months ago b834638e Initial commit ./test-repo
-locked-feature 10 months ago b834638e Initial commit ./locked-feature
+Branch  Age  Commit  Message  State  Path
+main  10 months ago b834638e Initial commit ./test-repo
+locked-feature 10 months ago b834638e Initial commit (locked: Testing lock functionality) ./locked-feature
Showing 2 worktrees
diff --git a/tests/snapshots/integration__integration_tests__list__long_commit_message.snap b/tests/snapshots/integration__integration_tests__list__long_commit_message.snap
index 98a237bc9..335c662db 100644
--- a/tests/snapshots/integration__integration_tests__list__long_commit_message.snap
+++ b/tests/snapshots/integration__integration_tests__list__long_commit_message.snap
@@ -6,21 +6,23 @@ info:
- list
env:
CLICOLOR_FORCE: "1"
+ COLUMNS: "150"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
GIT_CONFIG_GLOBAL: /dev/null
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpwao8hC/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmptdCPqR/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
-Branch  Age  Commit  Message  Path
-main  10 months ago 1310bfb9 Short message ./test-repo
-feature-a 10 months ago f078644e This is a very long commit... ./feature-a
+Branch  Age  Cmts  Commit  Message  Path
+main  10 months ago 1310bfb9 Short message ./test-repo
+feature-a 10 months ago ↑0 ↓1 f078644e This is a very long commit message that should... ./feature-a
Showing 2 worktrees, 1 behind
diff --git a/tests/snapshots/integration__integration_tests__list__many_worktrees_varied.snap b/tests/snapshots/integration__integration_tests__list__many_worktrees_varied.snap
index 14620161f..50ba51442 100644
--- a/tests/snapshots/integration__integration_tests__list__many_worktrees_varied.snap
+++ b/tests/snapshots/integration__integration_tests__list__many_worktrees_varied.snap
@@ -6,24 +6,26 @@ info:
- list
env:
CLICOLOR_FORCE: "1"
+ COLUMNS: "150"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
GIT_CONFIG_GLOBAL: /dev/null
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpFpHlqd/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp9lYhHU/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
-Branch  Commit  Path
-main  b834638e ./test-repo
-medium-name b834638e ./medium-name
-short b834638e ./short
-very-long-branch-name-here b834638e ./very-long-branch-name-here
-with-changes b834638e ./with-changes
+Branch  Age  Commit  Message  Path
+main  10 months ago b834638e Initial commit ./test-repo
+medium-name 10 months ago b834638e Initial commit ./medium-name
+short 10 months ago b834638e Initial commit ./short
+very-long-branch-name-here 10 months ago b834638e Initial commit ./very-long-branch-name-here
+with-changes 10 months ago b834638e Initial commit ./with-changes
Showing 5 worktrees
diff --git a/tests/snapshots/integration__integration_tests__list__unicode_commit_message.snap b/tests/snapshots/integration__integration_tests__list__unicode_commit_message.snap
index 5fc66647a..2c1bd19cf 100644
--- a/tests/snapshots/integration__integration_tests__list__unicode_commit_message.snap
+++ b/tests/snapshots/integration__integration_tests__list__unicode_commit_message.snap
@@ -6,21 +6,23 @@ info:
- list
env:
CLICOLOR_FORCE: "1"
+ COLUMNS: "150"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
GIT_CONFIG_GLOBAL: /dev/null
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp86rMtz/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpVBlTvV/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
-Branch  Age  Commit  Message  Path
-main  10 months ago e4614cb7 Fix bug with café... ./test-repo
-feature-test 10 months ago 0c53695b Add support for... ./feature-test
+Branch  Age  Cmts  Commit  Message  Path
+main  10 months ago e4614cb7 Fix bug with café ☕ handling ./test-repo
+feature-test 10 months ago ↑0 ↓1 0c53695b Add support for 日本語 and... ./feature-test
Showing 2 worktrees, 1 behind
diff --git a/tests/snapshots/integration__integration_tests__list__with_branches_flag.snap b/tests/snapshots/integration__integration_tests__list__with_branches_flag.snap
index 8805e616e..0a833c2a9 100644
--- a/tests/snapshots/integration__integration_tests__list__with_branches_flag.snap
+++ b/tests/snapshots/integration__integration_tests__list__with_branches_flag.snap
@@ -7,24 +7,26 @@ info:
- "--branches"
env:
CLICOLOR_FORCE: "1"
+ COLUMNS: "150"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
GIT_CONFIG_GLOBAL: /dev/null
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpaWMlqS/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp0bKwSg/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
-Branch  Age  Commit  Path
-main  10 months ago b834638e ./test-repo
-feature-with-worktree 10 months ago b834638e ./feature-with-worktree
-another-branch 10 months ago b834638e
-feature-without-worktree 10 months ago b834638e
-fix-bug 10 months ago b834638e
+Branch  Age  Commit  Message  Path
+main  10 months ago b834638e Initial commit ./test-repo
+feature-with-worktree 10 months ago b834638e Initial commit ./feature-with-worktree
+another-branch 10 months ago b834638e Initial commit
+feature-without-worktree 10 months ago b834638e Initial commit
+fix-bug 10 months ago b834638e Initial commit
Showing 2 worktrees, 3 branches
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_stdout_stderr_ordering.snap b/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_stdout_stderr_ordering.snap
index addab1beb..aa6566f93 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_stdout_stderr_ordering.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_pre_merge_command_stdout_stderr_ordering.snap
@@ -12,23 +12,20 @@ info:
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
GIT_CONFIG_GLOBAL: /dev/null
GIT_CONFIG_SYSTEM: /dev/null
- GIT_EDITOR: ""
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpEhsvbw/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp2NpdwS/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
🔄 Validating merge...
🔄 Running pre-merge command check1:
-  [PROJECT_ROOT]/tests/fixtures/mixed-output.sh
-  check1 3
+  [PROJECT_ROOT]/tests/fixtures/mixed-output.sh check1 3
🔄 Running pre-merge command check2:
-  [PROJECT_ROOT]/tests/fixtures/mixed-output.sh
-  check2 3
+  [PROJECT_ROOT]/tests/fixtures/mixed-output.sh check2 3
🔄 Rebasing onto main...
🔄 Pushing 1 commit to main @ [SHA]
diff --git a/tests/snapshots/integration__integration_tests__post_start_commands__post_create_template_expansion.snap b/tests/snapshots/integration__integration_tests__post_start_commands__post_create_template_expansion.snap
index d03f9d913..1430a4be9 100644
--- a/tests/snapshots/integration__integration_tests__post_start_commands__post_create_template_expansion.snap
+++ b/tests/snapshots/integration__integration_tests__post_start_commands__post_create_template_expansion.snap
@@ -8,6 +8,7 @@ info:
- feature/test
env:
CLICOLOR_FORCE: "1"
+ COLUMNS: "150"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
GIT_CONFIG_GLOBAL: /dev/null
@@ -16,7 +17,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp6jYMFA/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpVEaiug/test-config.toml
---
success: true
exit_code: 0
@@ -28,14 +29,10 @@ exit_code: 0
  echo 'Branch: feature-test' >> info.txt
🔄 Executing (post-create):
-  echo 'Worktree:
-  [REPO].feature-test'
-  >> info.txt
+  echo 'Worktree: [REPO].feature-test' >> info.txt
🔄 Executing (post-create):
-  echo 'Root:
-  [REPO]'
-  >> info.txt
+  echo 'Root: [REPO]' >> info.txt
✅ Created new worktree for feature/test at [REPO].feature-test
diff --git a/tests/snapshots/integration__integration_tests__spacing_edge_cases__long_branch_names.snap b/tests/snapshots/integration__integration_tests__spacing_edge_cases__long_branch_names.snap
index 0145ad499..c25cd3f3e 100644
--- a/tests/snapshots/integration__integration_tests__spacing_edge_cases__long_branch_names.snap
+++ b/tests/snapshots/integration__integration_tests__spacing_edge_cases__long_branch_names.snap
@@ -6,23 +6,25 @@ info:
- list
env:
CLICOLOR_FORCE: "1"
+ COLUMNS: "150"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
GIT_CONFIG_GLOBAL: /dev/null
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpxbRVlC/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp9fmblF/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
-Branch  Commit  Path
-main  b834638e ./test-repo
-another-extremely-long-name-here b834638e ./another-extremely-long-name-here
-short b834638e ./short
-very-long-feature-branch-name b834638e ./very-long-feature-branch-name
+Branch  Age  Commit  Message  Path
+main  10 months ago b834638e Initial commit ./test-repo
+another-extremely-long-name-here 10 months ago b834638e Initial commit ./another-extremely-long-name-here
+short 10 months ago b834638e Initial commit ./short
+very-long-feature-branch-name 10 months ago b834638e Initial commit ./very-long-feature-branch-name
Showing 4 worktrees
diff --git a/tests/snapshots/integration__integration_tests__spacing_edge_cases__mixed_length_branch_names.snap b/tests/snapshots/integration__integration_tests__spacing_edge_cases__mixed_length_branch_names.snap
index 901c91897..c6bb6eac6 100644
--- a/tests/snapshots/integration__integration_tests__spacing_edge_cases__mixed_length_branch_names.snap
+++ b/tests/snapshots/integration__integration_tests__spacing_edge_cases__mixed_length_branch_names.snap
@@ -6,23 +6,25 @@ info:
- list
env:
CLICOLOR_FORCE: "1"
+ COLUMNS: "150"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
GIT_CONFIG_GLOBAL: /dev/null
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpuQ3ikY/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpLwaTxi/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
-Branch Commit  Path
-main  b834638e ./test-repo
-long b834638e ./extremely-long-branch-name-that-might-cause-layout-issues
-medium b834638e ./medium-length-name
-x b834638e ./x
+Branch Age  Commit  Message  Path
+main  10 months ago b834638e Initial commit ./test-repo
+long 10 months ago b834638e Initial commit ./extremely-long-branch-name-that-might-cause-layout-issues
+medium 10 months ago b834638e Initial commit ./medium-length-name
+x 10 months ago b834638e Initial commit ./x
Showing 4 worktrees