Skip to content

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

Fix: Ensure column headers are considered for width calculation

Maximilian Roos016cb02cc243
diff --git a/src/commands/list/layout.rs b/src/commands/list/layout.rs
index b6dbba09e..92c6090e8 100644
--- a/src/commands/list/layout.rs
+++ b/src/commands/list/layout.rs
@@ -43,14 +43,15 @@ pub struct LayoutConfig {
}
pub fn calculate_column_widths(infos: &[WorktreeInfo]) -> ColumnWidths {
- let mut max_branch = 0;
- let mut max_time = 0;
- let mut max_message = 0;
- let mut max_ahead_behind = 0;
- let mut max_working_diff = 0;
- let mut max_branch_diff = 0;
- let mut max_upstream = 0;
- let mut max_states = 0;
+ // Initialize with header label widths to ensure headers always fit
+ let mut max_branch = "Branch".width();
+ let mut max_time = "Age".width();
+ let mut max_message = "Message".width();
+ let mut max_ahead_behind = "Cmts".width();
+ let mut max_working_diff = "WT +/-".width();
+ let mut max_branch_diff = "Cmt +/-".width();
+ let mut max_upstream = "Remote".width();
+ let mut max_states = "State".width();
for info in infos {
// Branch name
diff --git a/src/commands/list/mod.rs b/src/commands/list/mod.rs
index ebaa0019a..5ca665f15 100644
--- a/src/commands/list/mod.rs
+++ b/src/commands/list/mod.rs
@@ -1,6 +1,9 @@
mod layout;
mod render;
+#[cfg(test)]
+mod spacing_test;
+
use rayon::prelude::*;
use worktrunk::git::{GitError, Repository};
diff --git a/src/commands/list/spacing_test.rs b/src/commands/list/spacing_test.rs
new file mode 100644
index 000000000..644bb0414
--- /dev/null
+++ b/src/commands/list/spacing_test.rs
@@ -0,0 +1,84 @@
+//! Test to reproduce the spacing alignment bug
+//!
+//! This test verifies that header and data rows align properly,
+//! regardless of whether cells have ANSI styling applied.
+
+use worktrunk::styling::{StyledLine, dim_style, primary_style};
+
+#[test]
+fn test_header_data_alignment_with_and_without_styling() {
+ // Simulate what happens in the actual rendering
+ let dim = dim_style();
+ let primary = primary_style();
+
+ // Header (always styled with dim)
+ let mut header = StyledLine::new();
+ let branch_width = 6;
+ let branch_header = format!("{:width$}", "Branch", width = branch_width);
+ header.push_styled(branch_header, dim);
+ header.push_raw(" "); // separator
+ let age_header = format!("{:width$}", "Age", width = 14);
+ header.push_styled(age_header, dim);
+
+ println!(
+ "Header visual: |{}|",
+ header.render().replace("\x1b", "\\x1b")
+ );
+
+ // Data row 1: with styling (styled cell)
+ let mut row1 = StyledLine::new();
+ let branch1_text = format!("{:width$}", "main", width = branch_width);
+ row1.push_styled(branch1_text.clone(), primary); // Styled
+ row1.push_raw(" "); // separator
+ let time1_text = format!("{:width$}", "23 minutes ago", width = 14);
+ row1.push_styled(time1_text, dim);
+
+ println!(
+ "Row 1 styled visual: |{}|",
+ row1.render().replace("\x1b", "\\x1b")
+ );
+ println!(
+ "Row 1 styled: branch_text='{}' len={}",
+ branch1_text,
+ branch1_text.len()
+ );
+
+ // Data row 2: without styling (raw cell)
+ let mut row2 = StyledLine::new();
+ let branch2_text = format!("{:width$}", "foo", width = branch_width);
+ row2.push_raw(branch2_text.clone()); // NOT styled
+ row2.push_raw(" "); // separator
+ let time2_text = format!("{:width$}", "23 minutes ago", width = 14);
+ row2.push_styled(time2_text, dim);
+
+ println!(
+ "Row 2 unstyled visual: |{}|",
+ row2.render().replace("\x1b", "\\x1b")
+ );
+ println!(
+ "Row 2 unstyled: branch_text='{}' len={}",
+ branch2_text,
+ branch2_text.len()
+ );
+
+ // Check that all rows have the same width up to the time column
+ let header_width_before_time = branch_width + 2; // branch + separator
+ let row1_width_before_time = branch_width + 2;
+ let row2_width_before_time = branch_width + 2;
+
+ println!(
+ "\nExpected width before time column: {}",
+ header_width_before_time
+ );
+ println!("Row 1 width before time: {}", row1_width_before_time);
+ println!("Row 2 width before time: {}", row2_width_before_time);
+
+ assert_eq!(
+ header_width_before_time, row1_width_before_time,
+ "Header and styled row should align"
+ );
+ assert_eq!(
+ header_width_before_time, row2_width_before_time,
+ "Header and unstyled row should align"
+ );
+}
diff --git a/tests/integration_tests/mod.rs b/tests/integration_tests/mod.rs
index 60b9bed3b..619f5dbaa 100644
--- a/tests/integration_tests/mod.rs
+++ b/tests/integration_tests/mod.rs
@@ -7,4 +7,5 @@ pub mod list;
pub mod merge;
pub mod push;
pub mod remove;
+pub mod spacing_edge_cases;
pub mod switch;
diff --git a/tests/integration_tests/spacing_edge_cases.rs b/tests/integration_tests/spacing_edge_cases.rs
new file mode 100644
index 000000000..d06ce9ea0
--- /dev/null
+++ b/tests/integration_tests/spacing_edge_cases.rs
@@ -0,0 +1,92 @@
+use crate::common::TestRepo;
+use insta::Settings;
+use insta_cmd::{assert_cmd_snapshot, get_cargo_bin};
+use std::process::Command;
+
+/// Helper to create snapshot with normalized paths and SHAs
+fn snapshot_list(test_name: &str, repo: &TestRepo) {
+ let mut settings = Settings::clone_current();
+ settings.set_snapshot_path("../snapshots");
+
+ // Normalize paths
+ settings.add_filter(repo.root_path().to_str().unwrap(), "[REPO]");
+ for (name, path) in &repo.worktrees {
+ settings.add_filter(
+ path.to_str().unwrap(),
+ format!("[WORKTREE_{}]", name.to_uppercase().replace('-', "_")),
+ );
+ }
+
+ // Normalize git SHAs
+ settings.add_filter(r"\b[0-9a-f]{7,40}\b", "[SHA] ");
+ settings.add_filter(r"\\", "/");
+
+ settings.bind(|| {
+ let mut cmd = Command::new(get_cargo_bin("wt"));
+ repo.clean_cli_env(&mut cmd);
+ cmd.arg("list").current_dir(repo.root_path());
+ assert_cmd_snapshot!(test_name, cmd);
+ });
+}
+
+#[test]
+fn test_short_branch_names_shorter_than_header() {
+ let mut repo = TestRepo::new();
+ repo.commit("Initial commit");
+
+ // Create worktrees with very short branch names (shorter than "Branch" header)
+ repo.add_worktree("a", "a");
+ repo.add_worktree("bb", "bb");
+ repo.add_worktree("ccc", "ccc");
+
+ snapshot_list("short_branch_names", &repo);
+}
+
+#[test]
+fn test_long_branch_names_longer_than_header() {
+ let mut repo = TestRepo::new();
+ repo.commit("Initial commit");
+
+ // Create worktrees with very long branch names
+ repo.add_worktree(
+ "very-long-feature-branch-name",
+ "very-long-feature-branch-name",
+ );
+ repo.add_worktree(
+ "another-extremely-long-name-here",
+ "another-extremely-long-name-here",
+ );
+ repo.add_worktree("short", "short");
+
+ snapshot_list("long_branch_names", &repo);
+}
+
+#[test]
+fn test_unicode_branch_names_width_calculation() {
+ let mut repo = TestRepo::new();
+ repo.commit("Initial commit");
+
+ // Create worktrees with unicode characters that have different visual widths
+ // Note: Git may have restrictions on branch names, so use valid characters
+ repo.add_worktree("café", "cafe");
+ repo.add_worktree("naïve", "naive");
+ repo.add_worktree("résumé", "resume");
+
+ snapshot_list("unicode_branch_names", &repo);
+}
+
+#[test]
+fn test_mixed_length_branch_names() {
+ let mut repo = TestRepo::new();
+ repo.commit("Initial commit");
+
+ // Mix of very short, medium, and very long branch names
+ repo.add_worktree("x", "x");
+ repo.add_worktree("medium-length-name", "medium");
+ repo.add_worktree(
+ "extremely-long-branch-name-that-might-cause-layout-issues",
+ "long",
+ );
+
+ snapshot_list("mixed_length_branch_names", &repo);
+}
diff --git a/tests/snapshots/integration__integration_tests__list__detached_head.snap b/tests/snapshots/integration__integration_tests__list__detached_head.snap
index 004bc97b3..083da6dac 100644
--- a/tests/snapshots/integration__integration_tests__list__detached_head.snap
+++ b/tests/snapshots/integration__integration_tests__list__detached_head.snap
@@ -18,7 +18,7 @@ info:
success: true
exit_code: 0
----- stdout -----
-Branch  Age  Commit  Message  Path
-(detached) 9 months ago b834638e Initial commit  .
+Branch  Age  Cmts Cmt +/- WT +/- Remote Commit  Message  State Path
+(detached) 9 months ago b834638e Initial commit  .
----- stderr -----
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 5b9c78dd4..074311edd 100644
--- a/tests/snapshots/integration__integration_tests__list__locked_no_reason.snap
+++ b/tests/snapshots/integration__integration_tests__list__locked_no_reason.snap
@@ -18,8 +18,8 @@ info:
success: true
exit_code: 0
----- stdout -----
-Branch  Age  Commit  State  Path
-main  9 months ago b834638e ./main
-locked-no-reason 9 months ago [SHA] (locked) ./locked-no-reason
+Branch  Age  Cmts Cmt +/- WT +/- Remote Commit  State  Path
+main  9 months ago b834638e ./main
+locked-no-reason 9 months ago [SHA] (locked) ./locked-no-reason
----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__list__locked_worktree.snap b/tests/snapshots/integration__integration_tests__list__locked_worktree.snap
index 8a84c57aa..cf0bcb200 100644
--- a/tests/snapshots/integration__integration_tests__list__locked_worktree.snap
+++ b/tests/snapshots/integration__integration_tests__list__locked_worktree.snap
@@ -18,8 +18,8 @@ info:
success: true
exit_code: 0
----- stdout -----
-Branch  Age  Commit  State  Path
-main  9 months ago b834638e ./main
-locked-feature 9 months ago [SHA] (locked: Testing lock functionality) ./locked-feature
+Branch  Age  Cmts Cmt +/- WT +/- Remote Commit  State  Path
+main  9 months ago b834638e ./main
+locked-feature 9 months ago [SHA] (locked: Testing lock functionality) ./locked-feature
----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__list__long_branch_name.snap b/tests/snapshots/integration__integration_tests__list__long_branch_name.snap
index 72d9204fb..1daa23ef1 100644
--- a/tests/snapshots/integration__integration_tests__list__long_branch_name.snap
+++ b/tests/snapshots/integration__integration_tests__list__long_branch_name.snap
@@ -18,8 +18,8 @@ info:
success: true
exit_code: 0
----- stdout -----
-Branch  Commit  Path
-main  b834638e ./main
-feature-this-is-a-very-long-branch-name-that-should-test-column-alignment [SHA] ./feature-this-is-a-very-long-branch-name-that-should-test-column-alignment
+Branch  Cmts Cmt +/- WT +/- Remote Commit  State Path
+main  b834638e ./main
+feature-this-is-a-very-long-branch-name-that-should-test-column-alignment [SHA] ./feature-this-is-a-very-long-branch-name-that-should-test-column-alignment
----- stderr -----
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 e06c3a7a9..3038f1011 100644
--- a/tests/snapshots/integration__integration_tests__list__long_commit_message.snap
+++ b/tests/snapshots/integration__integration_tests__list__long_commit_message.snap
@@ -18,8 +18,8 @@ info:
success: true
exit_code: 0
----- stdout -----
-Branch  Age  Cmts  Commit  Message  Path
-main  9 months ago 1310bfb9 Short message  ./main
-feature-a 9 months ago ↑0 ↓1 [SHA] This is a very...  ./feature-a
+Branch  Age  Cmts  Cmt +/- WT +/- Remote Commit  Message  State Path
+main  9 months ago 1310bfb9 Short message  ./main
+feature-a 9 months ago ↑0 ↓1 [SHA] This is a very...  ./feature-a
----- stderr -----
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 810392251..fa9c5240d 100644
--- a/tests/snapshots/integration__integration_tests__list__many_worktrees_varied.snap
+++ b/tests/snapshots/integration__integration_tests__list__many_worktrees_varied.snap
@@ -18,11 +18,11 @@ info:
success: true
exit_code: 0
----- stdout -----
-Branch  Age  Commit  Path
-main  9 months ago b834638e ./main
-medium-name 9 months ago [SHA] ./medium-name
-short 9 months ago [SHA] ./short
-very-long-branch-name-here 9 months ago [SHA] ./very-long-branch-name-here
-with-changes 9 months ago [SHA] ./with-changes
+Branch  Age  Cmts Cmt +/- WT +/- Remote Commit  State Path
+main  9 months ago b834638e ./main
+medium-name 9 months ago [SHA] ./medium-name
+short 9 months ago [SHA] ./short
+very-long-branch-name-here 9 months ago [SHA] ./very-long-branch-name-here
+with-changes 9 months ago [SHA] ./with-changes
----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__list__multiple_worktrees.snap b/tests/snapshots/integration__integration_tests__list__multiple_worktrees.snap
index 195826e73..5f3111c8c 100644
--- a/tests/snapshots/integration__integration_tests__list__multiple_worktrees.snap
+++ b/tests/snapshots/integration__integration_tests__list__multiple_worktrees.snap
@@ -18,9 +18,9 @@ info:
success: true
exit_code: 0
----- stdout -----
-Branch  Age  Commit  Message  Path
-main  9 months ago b834638e Initial commit  ./main
-feature-a 9 months ago [SHA] Initial commit  ./feature-a
-feature-b 9 months ago [SHA] Initial commit  ./feature-b
+Branch  Age  Cmts Cmt +/- WT +/- Remote Commit  Message  State Path
+main  9 months ago b834638e Initial commit  ./main
+feature-a 9 months ago [SHA] Initial commit  ./feature-a
+feature-b 9 months ago [SHA] Initial commit  ./feature-b
----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__list__single_worktree.snap b/tests/snapshots/integration__integration_tests__list__single_worktree.snap
index 003914fe5..ecd3144da 100644
--- a/tests/snapshots/integration__integration_tests__list__single_worktree.snap
+++ b/tests/snapshots/integration__integration_tests__list__single_worktree.snap
@@ -18,7 +18,7 @@ info:
success: true
exit_code: 0
----- stdout -----
-Branch Age  Commit  Message  Path
-main 9 months ago b834638e Initial commit  .
+Branch Age  Cmts Cmt +/- WT +/- Remote Commit  Message  State Path
+main  9 months ago b834638e Initial commit  .
----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__list__unicode_branch_name.snap b/tests/snapshots/integration__integration_tests__list__unicode_branch_name.snap
index b5fd47d5f..2150a5f78 100644
--- a/tests/snapshots/integration__integration_tests__list__unicode_branch_name.snap
+++ b/tests/snapshots/integration__integration_tests__list__unicode_branch_name.snap
@@ -18,9 +18,9 @@ info:
success: true
exit_code: 0
----- stdout -----
-Branch  Age  Commit  Path
-main  9 months ago b834638e ./main
-feature-日本語-test 9 months ago [SHA] ./feature-日本語-test
-fix-émoji-🎉 9 months ago [SHA] ./fix-émoji-🎉
+Branch  Age  Cmts Cmt +/- WT +/- Remote Commit  State Path
+main  9 months ago b834638e ./main
+feature-日本語-test 9 months ago [SHA] ./feature-日本語-test
+fix-émoji-🎉 9 months ago [SHA] ./fix-émoji-🎉
----- stderr -----
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 d20107656..43a4b9dec 100644
--- a/tests/snapshots/integration__integration_tests__list__unicode_commit_message.snap
+++ b/tests/snapshots/integration__integration_tests__list__unicode_commit_message.snap
@@ -18,8 +18,8 @@ info:
success: true
exit_code: 0
----- stdout -----
-Branch  Age  Cmts  Commit  Path
-main  9 months ago e4614cb7 ./main
-feature-test 9 months ago ↑0 ↓1 [SHA] ./feature-test
+Branch  Age  Cmts  Cmt +/- WT +/- Remote Commit  State Path
+main  9 months ago e4614cb7 ./main
+feature-test 9 months ago ↑0 ↓1 [SHA] ./feature-test
----- stderr -----
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
new file mode 100644
index 000000000..364260c9f
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__spacing_edge_cases__long_branch_names.snap
@@ -0,0 +1,27 @@
+---
+source: tests/integration_tests/spacing_edge_cases.rs
+info:
+ program: wt
+ args:
+ - list
+ env:
+ CLICOLOR_FORCE: "1"
+ 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: "1704067200"
+---
+success: true
+exit_code: 0
+----- stdout -----
+Branch  Age  Cmts Cmt +/- WT +/- Remote Commit  State Path
+main  9 months ago b834638e ./main
+another-extremely-long-name-here 9 months ago [SHA] ./another-extremely-long-name-here
+short 9 months ago [SHA] ./short
+very-long-feature-branch-name 9 months ago [SHA] ./very-long-feature-branch-name
+
+----- stderr -----
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
new file mode 100644
index 000000000..8c2be8020
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__spacing_edge_cases__mixed_length_branch_names.snap
@@ -0,0 +1,27 @@
+---
+source: tests/integration_tests/spacing_edge_cases.rs
+info:
+ program: wt
+ args:
+ - list
+ env:
+ CLICOLOR_FORCE: "1"
+ 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: "1704067200"
+---
+success: true
+exit_code: 0
+----- stdout -----
+Branch Age  Cmts Cmt +/- WT +/- Remote Commit  Message  State Path
+main  9 months ago b834638e Initial commit  ./main
+long 9 months ago [SHA] Initial commit  ./extremely-long-branch-name-that-might-cause-layout-issues
+medium 9 months ago [SHA] Initial commit  ./medium-length-name
+x 9 months ago [SHA] Initial commit  ./x
+
+----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__spacing_edge_cases__short_branch_names.snap b/tests/snapshots/integration__integration_tests__spacing_edge_cases__short_branch_names.snap
new file mode 100644
index 000000000..3db8243b1
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__spacing_edge_cases__short_branch_names.snap
@@ -0,0 +1,27 @@
+---
+source: tests/integration_tests/spacing_edge_cases.rs
+info:
+ program: wt
+ args:
+ - list
+ env:
+ CLICOLOR_FORCE: "1"
+ 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: "1704067200"
+---
+success: true
+exit_code: 0
+----- stdout -----
+Branch Age  Cmts Cmt +/- WT +/- Remote Commit  Message  State Path
+main  9 months ago b834638e Initial commit  ./main
+a 9 months ago [SHA] Initial commit  ./a
+bb 9 months ago [SHA] Initial commit  ./bb
+ccc 9 months ago [SHA] Initial commit  ./ccc
+
+----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__spacing_edge_cases__unicode_branch_names.snap b/tests/snapshots/integration__integration_tests__spacing_edge_cases__unicode_branch_names.snap
new file mode 100644
index 000000000..e2b21c675
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__spacing_edge_cases__unicode_branch_names.snap
@@ -0,0 +1,27 @@
+---
+source: tests/integration_tests/spacing_edge_cases.rs
+info:
+ program: wt
+ args:
+ - list
+ env:
+ CLICOLOR_FORCE: "1"
+ 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: "1704067200"
+---
+success: true
+exit_code: 0
+----- stdout -----
+Branch Age  Cmts Cmt +/- WT +/- Remote Commit  Message  State Path
+main  9 months ago b834638e Initial commit  ./main
+cafe 9 months ago [SHA] Initial commit  ./café
+naive 9 months ago [SHA] Initial commit  ./naïve
+resume 9 months ago [SHA] Initial commit  ./résumé
+
+----- stderr -----