Skip to content

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

refactor: Extract styled_symbols logic to reuse

Maximilian Roos9247045789de
diff --git a/src/commands/list/model.rs b/src/commands/list/model.rs
index 3124e32cd..916e0788c 100644
--- a/src/commands/list/model.rs
+++ b/src/commands/list/model.rs
@@ -949,7 +949,6 @@ impl StatusSymbols {
///
/// See [`StatusSymbols`] struct doc for symbol categories.
pub fn render_with_mask(&self, mask: &PositionMask) -> String {
- use color_print::cformat;
use worktrunk::styling::StyledLine;
let mut result = String::with_capacity(64);
@@ -958,13 +957,75 @@ impl StatusSymbols {
return result;
}
- // Build list of (position_index, content, has_data) tuples
- // Ordered by importance/actionability
- // Apply colors based on semantic meaning:
- // - Red (ERROR): Actual conflicts (blocking problems)
- // - Yellow (WARNING): Git operations, locked/prunable (stuck states needing attention)
- // - Cyan: Working tree changes (activity indicator)
- // - Dimmed (HINT): Branch state symbols that indicate removability + divergence arrows (low urgency)
+ // Grid-based rendering: each position gets a fixed width for vertical alignment.
+ // CRITICAL: Always use PositionMask::FULL for consistent spacing between progressive and final rendering.
+ // The mask provides the maximum width needed for each position across all rows.
+ // Accept wider Status column with whitespace as tradeoff for perfect alignment.
+ for (pos, styled_content, has_data) in self.styled_symbols() {
+ let allocated_width = mask.width(pos);
+
+ if has_data {
+ // Use StyledLine to handle width calculation (strips ANSI codes automatically)
+ let mut segment = StyledLine::new();
+ segment.push_raw(styled_content);
+ segment.pad_to(allocated_width);
+ result.push_str(&segment.render());
+ } else {
+ // Fill empty position with spaces for alignment
+ for _ in 0..allocated_width {
+ result.push(' ');
+ }
+ }
+ }
+
+ result
+ }
+
+ /// Check if symbols are empty
+ pub fn is_empty(&self) -> bool {
+ self.branch_op_state == BranchOpState::None
+ && self.worktree_state == WorktreeState::None
+ && self.main_divergence == MainDivergence::None
+ && self.upstream_divergence == UpstreamDivergence::None
+ && self.working_tree.is_empty()
+ && self.user_status.is_none()
+ }
+
+ /// Render status symbols in compact form for statusline (no grid alignment).
+ ///
+ /// Uses the same styled symbols as `render_with_mask()`, just without padding.
+ pub fn format_compact(&self) -> String {
+ self.styled_symbols()
+ .into_iter()
+ .filter_map(|(_, styled, has_data)| has_data.then_some(styled))
+ .collect()
+ }
+
+ /// Build styled symbols array with position indices.
+ ///
+ /// Returns: `[(position_mask, styled_string, has_data); 8]`
+ ///
+ /// Order: working_tree (+!?) → branch_op_state → main_divergence → upstream_divergence → worktree_state → user_status
+ ///
+ /// Styling follows semantic meaning:
+ /// - Cyan: Working tree changes (activity indicator)
+ /// - Yellow: Git operations, locked/prunable (stuck states needing attention)
+ /// - Dimmed: Branch state symbols + divergence arrows (low urgency)
+ fn styled_symbols(&self) -> [(usize, String, bool); 8] {
+ use color_print::cformat;
+
+ // Working tree symbols split into 3 fixed columns for vertical alignment
+ let style_working = |sym: char| -> (String, bool) {
+ if self.working_tree.contains(sym) {
+ (cformat!("<cyan>{sym}</>"), true)
+ } else {
+ (String::new(), false)
+ }
+ };
+ let (staged_str, has_staged) = style_working('+');
+ let (modified_str, has_modified) = style_working('!');
+ let (untracked_str, has_untracked) = style_working('?');
+
let (branch_op_state_str, has_branch_op_state) = self
.branch_op_state
.styled()
@@ -977,17 +1038,7 @@ impl StatusSymbols {
.upstream_divergence
.styled()
.map_or((String::new(), false), |s| (s, true));
- // Working tree symbols split into 3 fixed columns for vertical alignment
- let style_working = |sym: char| -> (String, bool) {
- if self.working_tree.contains(sym) {
- (cformat!("<cyan>{sym}</>"), true)
- } else {
- (String::new(), false)
- }
- };
- let (staged_str, has_staged) = style_working('+');
- let (modified_str, has_modified) = style_working('!');
- let (untracked_str, has_untracked) = style_working('?');
+
let worktree_state_str = match self.worktree_state {
WorktreeState::None => String::new(),
// Branch indicator (⎇) is informational (dimmed)
@@ -995,15 +1046,12 @@ impl StatusSymbols {
// Worktree attrs (⚐⌫⊠) are warnings (yellow)
_ => cformat!("<yellow>{}</>", self.worktree_state),
};
+
let user_status_str = self.user_status.as_deref().unwrap_or("").to_string();
- // Position data: (position_mask, styled_content, has_data)
- // StyledLine handles width tracking automatically via .width()
- //
// CRITICAL: Display order is working_tree first (staged, modified, untracked), then other symbols.
// NEVER change this order - it ensures progressive and final rendering match exactly.
- // Tests will break if you change this, but that's expected - update the tests, not this order.
- let positions_data: [(usize, String, bool); 8] = [
+ [
(PositionMask::STAGED, staged_str, has_staged),
(PositionMask::MODIFIED, modified_str, has_modified),
(PositionMask::UNTRACKED, untracked_str, has_untracked),
@@ -1032,73 +1080,7 @@ impl StatusSymbols {
user_status_str,
self.user_status.is_some(),
),
- ];
-
- // Grid-based rendering: each position gets a fixed width for vertical alignment.
- // CRITICAL: Always use PositionMask::FULL for consistent spacing between progressive and final rendering.
- // The mask provides the maximum width needed for each position across all rows.
- // Accept wider Status column with whitespace as tradeoff for perfect alignment.
- for (pos, styled_content, has_data) in positions_data {
- let allocated_width = mask.width(pos);
-
- if has_data {
- // Use StyledLine to handle width calculation (strips ANSI codes automatically)
- let mut segment = StyledLine::new();
- segment.push_raw(styled_content);
- segment.pad_to(allocated_width);
- result.push_str(&segment.render());
- } else {
- // Fill empty position with spaces for alignment
- for _ in 0..allocated_width {
- result.push(' ');
- }
- }
- }
-
- result
- }
-
- /// Check if symbols are empty
- pub fn is_empty(&self) -> bool {
- self.branch_op_state == BranchOpState::None
- && self.worktree_state == WorktreeState::None
- && self.main_divergence == MainDivergence::None
- && self.upstream_divergence == UpstreamDivergence::None
- && self.working_tree.is_empty()
- && self.user_status.is_none()
- }
-
- /// Render status symbols in compact form for statusline (no grid alignment).
- pub fn format_compact(&self) -> String {
- use color_print::cformat;
-
- let mut result = String::new();
-
- // Working tree symbols (compact, no padding) - cyan for activity
- if !self.working_tree.is_empty() {
- result.push_str(&cformat!("<cyan>{}</>", self.working_tree));
- }
-
- // Branch/op state
- if let Some(styled) = self.branch_op_state.styled() {
- result.push_str(&styled);
- }
-
- // Worktree state (path mismatch/locked/prunable) - skip branch indicator (⎇)
- // Note: ⎇ only appears for branch-only items, never for worktrees (statusline context)
- if matches!(
- self.worktree_state,
- WorktreeState::PathMismatch | WorktreeState::Prunable | WorktreeState::Locked
- ) {
- result.push_str(&cformat!("<yellow>{}</>", self.worktree_state));
- }
-
- // User status
- if let Some(ref user_status) = self.user_status {
- result.push_str(user_status);
- }
-
- result
+ ]
}
}
diff --git a/tests/integration_tests/statusline.rs b/tests/integration_tests/statusline.rs
index a2d1f0ab5..de0179bbf 100644
--- a/tests/integration_tests/statusline.rs
+++ b/tests/integration_tests/statusline.rs
@@ -112,14 +112,14 @@ fn setup_repo_with_commits_ahead() -> TestRepo {
fn test_statusline_basic() {
let repo = setup_basic_repo();
let output = run_statusline(&repo, &[], None);
- assert_snapshot!(output, @"main");
+ assert_snapshot!(output, @"main ^");
}
#[test]
fn test_statusline_with_changes() {
let repo = setup_repo_with_changes();
let output = run_statusline(&repo, &[], None);
- assert_snapshot!(output, @"main ?");
+ assert_snapshot!(output, @"main ?^");
}
#[test]
@@ -128,7 +128,7 @@ fn test_statusline_commits_ahead() {
// Run from the feature worktree to see commits ahead
let feature_path = repo.worktree_path("feature");
let output = run_statusline_from_dir(&repo, &[], None, feature_path);
- assert_snapshot!(output, @"feature ↑2");
+ assert_snapshot!(output, @"feature ↑ ↑2");
}
// --- Claude Code Mode Tests ---
@@ -169,7 +169,7 @@ fn test_statusline_claude_code_full_context() {
let output = run_statusline(&repo, &["--claude-code"], Some(&json));
claude_code_snapshot_settings(&repo).bind(|| {
- assert_snapshot!(output, @"[PATH] main ? | Opus");
+ assert_snapshot!(output, @"[PATH] main ?^ | Opus");
});
}
@@ -184,7 +184,7 @@ fn test_statusline_claude_code_minimal() {
let output = run_statusline(&repo, &["--claude-code"], Some(&json));
claude_code_snapshot_settings(&repo).bind(|| {
- assert_snapshot!(output, @"[PATH] main");
+ assert_snapshot!(output, @"[PATH] main ^");
});
}
@@ -202,7 +202,7 @@ fn test_statusline_claude_code_with_model() {
let output = run_statusline(&repo, &["--claude-code"], Some(&json));
claude_code_snapshot_settings(&repo).bind(|| {
- assert_snapshot!(output, @"[PATH] main | Haiku");
+ assert_snapshot!(output, @"[PATH] main ^ | Haiku");
});
}
diff --git a/tests/snapshots/integration__integration_tests__list__json_tree_matches_main_after_merge.snap b/tests/snapshots/integration__integration_tests__list__json_tree_matches_main_after_merge.snap
index 5147e6685..03d2b7b09 100644
--- a/tests/snapshots/integration__integration_tests__list__json_tree_matches_main_after_merge.snap
+++ b/tests/snapshots/integration__integration_tests__list__json_tree_matches_main_after_merge.snap
@@ -58,7 +58,7 @@ exit_code: 0
"upstream_divergence": "",
"worktree_state": ""
},
- "status_line": "main",
+ "status_line": "main /u001b[2m^/u001b[22m",
"type": "worktree",
"path": "[REPO]",
"bare": false,
@@ -110,7 +110,7 @@ exit_code: 0
"worktree_state": ""
},
"commits_display": "/u001b[32m↑2/u001b[0m",
- "status_line": "feature-merged /u001b[2m≡/u001b[22m /u001b[32m↑2/u001b[0m",
+ "status_line": "feature-merged /u001b[2m≡/u001b[22m/u001b[2m↑/u001b[22m /u001b[32m↑2/u001b[0m",
"type": "worktree",
"path": "[REPO].feature-merged",
"bare": false,
diff --git a/tests/snapshots/integration__integration_tests__list__json_with_display_fields.snap b/tests/snapshots/integration__integration_tests__list__json_with_display_fields.snap
index 4b6c32bbe..be671f370 100644
--- a/tests/snapshots/integration__integration_tests__list__json_with_display_fields.snap
+++ b/tests/snapshots/integration__integration_tests__list__json_with_display_fields.snap
@@ -58,7 +58,7 @@ exit_code: 0
"upstream_divergence": "",
"worktree_state": ""
},
- "status_line": "main",
+ "status_line": "main /u001b[2m^/u001b[22m",
"type": "worktree",
"path": "[REPO]",
"bare": false,
@@ -111,7 +111,7 @@ exit_code: 0
},
"commits_display": "/u001b[32m↑2/u001b[0m /u001b[2m/u001b[31m↓2/u001b[0m",
"branch_diff_display": "/u001b[32m+1/u001b[0m",
- "status_line": "feature-ahead /u001b[36m!?/u001b[39m ±/u001b[32m+1/u001b[0m /u001b[31m-1/u001b[0m /u001b[32m↑2/u001b[0m /u001b[2m/u001b[31m↓2/u001b[0m",
+ "status_line": "feature-ahead /u001b[36m!/u001b[39m/u001b[36m?/u001b[39m/u001b[2m↕/u001b[22m ±/u001b[32m+1/u001b[0m /u001b[31m-1/u001b[0m /u001b[32m↑2/u001b[0m /u001b[2m/u001b[31m↓2/u001b[0m",
"type": "worktree",
"path": "[REPO].feature-ahead",
"bare": false,
@@ -160,7 +160,7 @@ exit_code: 0
"worktree_state": ""
},
"commits_display": "/u001b[2m/u001b[31m↓2/u001b[0m",
- "status_line": "feature-behind /u001b[2m_/u001b[22m /u001b[2m/u001b[31m↓2/u001b[0m",
+ "status_line": "feature-behind /u001b[2m_/u001b[22m/u001b[2m↓/u001b[22m /u001b[2m/u001b[31m↓2/u001b[0m",
"type": "worktree",
"path": "[REPO].feature-behind",
"bare": false,
diff --git a/tests/snapshots/integration__integration_tests__list__json_with_git_operation.snap b/tests/snapshots/integration__integration_tests__list__json_with_git_operation.snap
index e5dd7063d..109c16cb8 100644
--- a/tests/snapshots/integration__integration_tests__list__json_with_git_operation.snap
+++ b/tests/snapshots/integration__integration_tests__list__json_with_git_operation.snap
@@ -58,7 +58,7 @@ exit_code: 0
"upstream_divergence": "",
"worktree_state": ""
},
- "status_line": "main",
+ "status_line": "main /u001b[2m^/u001b[22m",
"type": "worktree",
"path": "[REPO]",
"bare": false,
diff --git a/tests/snapshots/integration__integration_tests__list__json_with_metadata.snap b/tests/snapshots/integration__integration_tests__list__json_with_metadata.snap
index ef21edc54..c00b80942 100644
--- a/tests/snapshots/integration__integration_tests__list__json_with_metadata.snap
+++ b/tests/snapshots/integration__integration_tests__list__json_with_metadata.snap
@@ -58,7 +58,7 @@ exit_code: 0
"upstream_divergence": "",
"worktree_state": ""
},
- "status_line": "main",
+ "status_line": "main /u001b[2m^/u001b[22m",
"type": "worktree",
"path": "[REPO]",
"bare": false,
diff --git a/tests/snapshots/integration__integration_tests__list__json_with_user_status.snap b/tests/snapshots/integration__integration_tests__list__json_with_user_status.snap
index e23c079f6..433095c88 100644
--- a/tests/snapshots/integration__integration_tests__list__json_with_user_status.snap
+++ b/tests/snapshots/integration__integration_tests__list__json_with_user_status.snap
@@ -58,7 +58,7 @@ exit_code: 0
"upstream_divergence": "",
"worktree_state": ""
},
- "status_line": "main",
+ "status_line": "main /u001b[2m^/u001b[22m",
"type": "worktree",
"path": "[REPO]",
"bare": false,