Skip to content

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

Move status symbol computation to `ListItem` method

Maximilian Roose9d1bc8ceafb
diff --git a/src/cli.rs b/src/cli.rs
index 2ef224a3d..4f1db11b6 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -52,8 +52,7 @@ Usage: {usage}
/// Build a clap Command for Cli with the shared help template applied recursively.
pub fn build_command() -> Command {
- let cmd = Cli::command();
- apply_help_template_recursive(cmd, DEFAULT_COMMAND_NAME)
+ apply_help_template_recursive(Cli::command(), DEFAULT_COMMAND_NAME)
}
fn apply_help_template_recursive(mut cmd: Command, path: &str) -> Command {
diff --git a/src/commands/list/collect.rs b/src/commands/list/collect.rs
index f8eff0c0e..787be1271 100644
--- a/src/commands/list/collect.rs
+++ b/src/commands/list/collect.rs
@@ -26,8 +26,8 @@ use worktrunk::styling::INFO_EMOJI;
use super::ci_status::PrStatus;
use super::model::{
- AheadBehind, BranchDiffTotals, BranchOpState, CommitDetails, DisplayFields, ItemKind, ListItem,
- MainDivergence, StatusSymbols, UpstreamDivergence, UpstreamStatus, WorktreeData,
+ AheadBehind, BranchDiffTotals, CommitDetails, DisplayFields, ItemKind, ListItem,
+ UpstreamStatus, WorktreeData,
};
/// Context for status symbol computation during cell updates
@@ -123,160 +123,6 @@ pub(super) fn detect_worktree_state(repo: &Repository) -> Option<String> {
}
}
-fn compute_divergences(
- counts: &AheadBehind,
- upstream: &UpstreamStatus,
-) -> (MainDivergence, UpstreamDivergence) {
- let main_divergence = MainDivergence::from_counts(counts.ahead, counts.behind);
- let (upstream_ahead, upstream_behind) =
- upstream.active().map(|(_, a, b)| (a, b)).unwrap_or((0, 0));
- let upstream_divergence = UpstreamDivergence::from_counts(upstream_ahead, upstream_behind);
-
- (main_divergence, upstream_divergence)
-}
-
-/// Determine branch state for a worktree.
-///
-/// Returns:
-/// - `BranchOpState::None` if main worktree or no base branch
-/// - `BranchOpState::NoCommits` if no commits ahead and working tree is clean
-/// - `BranchOpState::MatchesMain` if ahead == 0 but dirty tree happens to match main (rare)
-/// - `BranchOpState::None` otherwise
-fn determine_worktree_base_state(
- is_main: bool,
- default_branch: Option<&str>,
- ahead: usize,
- working_tree_diff: Option<&LineDiff>,
- working_tree_diff_with_main: &Option<Option<LineDiff>>,
-) -> BranchOpState {
- if is_main || default_branch.is_none() {
- return BranchOpState::None;
- }
-
- let is_clean = working_tree_diff.map(|d| d.is_empty()).unwrap_or(true);
-
- // Check if no commits and clean working tree (most common removable state)
- if ahead == 0 && is_clean {
- return BranchOpState::NoCommits;
- }
-
- // Check if working tree matches main exactly (requires diff with main to be computed)
- // This is rare - only when ahead == 0, tree is dirty, but dirty changes happen to match main
- if let Some(Some(mdiff)) = working_tree_diff_with_main.as_ref()
- && mdiff.is_empty()
- && ahead == 0
- {
- BranchOpState::MatchesMain
- } else {
- BranchOpState::None
- }
-}
-
-/// Compute status symbols for a single item (worktrees and branches).
-///
-/// This is idempotent and can be called multiple times as new data arrives.
-/// It will recompute with the latest available data.
-///
-/// Branches get a subset of status symbols (no working tree changes or worktree attrs).
-// TODO(status-indicator): show a status glyph when a worktree's checked-out branch
-// differs from the branch name we associate with it (e.g., worktree exists but on another branch).
-fn compute_item_status_symbols(
- item: &mut ListItem,
- default_branch: Option<&str>,
- has_merge_tree_conflicts: bool,
- user_status: Option<String>,
- working_tree_symbols: Option<&str>,
- has_conflicts: bool,
-) {
- // Common fields for both worktrees and branches
- let default_counts = AheadBehind::default();
- let default_upstream = UpstreamStatus::default();
- let counts = item.counts.as_ref().unwrap_or(&default_counts);
- let upstream = item.upstream.as_ref().unwrap_or(&default_upstream);
- let (main_divergence, upstream_divergence) = compute_divergences(counts, upstream);
-
- match &item.kind {
- ItemKind::Worktree(data) => {
- // Full status computation for worktrees
- // Use default_branch directly (None for main worktree)
-
- // Worktree state - priority: prunable > locked (1 char max)
- let worktree_state = if data.prunable.is_some() {
- "⌫".to_string() // Prunable (directory missing)
- } else if data.locked.is_some() {
- "⊠".to_string() // Locked (protected)
- } else {
- String::new()
- };
-
- // Determine base branch state (only for non-main worktrees with base branch)
- let base_state = determine_worktree_base_state(
- data.is_main,
- default_branch,
- counts.ahead,
- data.working_tree_diff.as_ref(),
- &data.working_tree_diff_with_main,
- );
-
- // Apply priority: Conflicts > Rebase > Merge > MergeTreeConflicts > base_state
- let branch_op_state = if has_conflicts {
- BranchOpState::Conflicts
- } else if data.worktree_state.as_deref() == Some("rebase") {
- BranchOpState::Rebase
- } else if data.worktree_state.as_deref() == Some("merge") {
- BranchOpState::Merge
- } else if has_merge_tree_conflicts {
- BranchOpState::MergeTreeConflicts
- } else {
- base_state
- };
-
- // Override main_divergence for the main worktree
- let main_divergence = if data.is_main {
- MainDivergence::IsMain
- } else {
- main_divergence
- };
-
- item.status_symbols = Some(StatusSymbols {
- branch_op_state,
- worktree_state,
- main_divergence,
- upstream_divergence,
- working_tree: working_tree_symbols.unwrap_or("").to_string(),
- user_status,
- });
- }
- ItemKind::Branch => {
- // Simplified status computation for branches
- // Only compute symbols that apply to branches (no working tree, git operation, or worktree attrs)
-
- // Branch op state - branches can only show MergeTreeConflicts or NoCommits
- // (MatchesMain only applies to worktrees since branches don't have working trees)
- let branch_op_state = if has_merge_tree_conflicts {
- BranchOpState::MergeTreeConflicts
- } else if let Some(ref c) = item.counts {
- if c.ahead == 0 {
- BranchOpState::NoCommits
- } else {
- BranchOpState::None
- }
- } else {
- BranchOpState::None
- };
-
- item.status_symbols = Some(StatusSymbols {
- branch_op_state,
- worktree_state: "⎇".to_string(), // Branch indicator
- main_divergence,
- upstream_divergence,
- working_tree: String::new(),
- user_status,
- });
- }
- }
-}
-
/// Drain cell updates from the channel and apply them to items.
///
/// This is the shared logic between progressive and buffered collection modes.
@@ -695,8 +541,7 @@ pub fn collect(
} else {
Some(default_branch.as_str())
};
- compute_item_status_symbols(
- item,
+ item.compute_status_symbols(
item_default_branch,
ctx.has_merge_tree_conflicts,
ctx.user_status.clone(),
@@ -951,8 +796,7 @@ pub fn populate_items(
} else {
Some(default_branch)
};
- compute_item_status_symbols(
- item,
+ item.compute_status_symbols(
item_default_branch,
ctx.has_merge_tree_conflicts,
ctx.user_status.clone(),
diff --git a/src/commands/list/model.rs b/src/commands/list/model.rs
index 66d13840c..3866977ce 100644
--- a/src/commands/list/model.rs
+++ b/src/commands/list/model.rs
@@ -142,6 +142,21 @@ pub struct AheadBehind {
pub behind: usize,
}
+impl AheadBehind {
+ /// Compute divergence states from ahead/behind counts and upstream status.
+ pub fn compute_divergences(
+ &self,
+ upstream: &UpstreamStatus,
+ ) -> (MainDivergence, UpstreamDivergence) {
+ let main_divergence = MainDivergence::from_counts(self.ahead, self.behind);
+ let (upstream_ahead, upstream_behind) =
+ upstream.active().map(|(_, a, b)| (a, b)).unwrap_or((0, 0));
+ let upstream_divergence = UpstreamDivergence::from_counts(upstream_ahead, upstream_behind);
+
+ (main_divergence, upstream_divergence)
+ }
+}
+
#[derive(serde::Serialize, Default, Copy, Clone, Debug)]
pub struct BranchDiffTotals {
#[serde(rename = "branch_diff")]
@@ -376,6 +391,148 @@ impl ListItem {
.format_diff_plain(working_tree_diff.added, working_tree_diff.deleted);
}
}
+
+ /// Compute status symbols for this item.
+ ///
+ /// This is idempotent and can be called multiple times as new data arrives.
+ /// It will recompute with the latest available data.
+ ///
+ /// Branches get a subset of status symbols (no working tree changes or worktree attrs).
+ // TODO(status-indicator): show a status glyph when a worktree's checked-out branch
+ // differs from the branch name we associate with it (e.g., worktree exists but on another branch).
+ pub(crate) fn compute_status_symbols(
+ &mut self,
+ default_branch: Option<&str>,
+ has_merge_tree_conflicts: bool,
+ user_status: Option<String>,
+ working_tree_symbols: Option<&str>,
+ has_conflicts: bool,
+ ) {
+ // Common fields for both worktrees and branches
+ let default_counts = AheadBehind::default();
+ let default_upstream = UpstreamStatus::default();
+ let counts = self.counts.as_ref().unwrap_or(&default_counts);
+ let upstream = self.upstream.as_ref().unwrap_or(&default_upstream);
+ let (main_divergence, upstream_divergence) = counts.compute_divergences(upstream);
+
+ match &self.kind {
+ ItemKind::Worktree(data) => {
+ // Full status computation for worktrees
+ // Use default_branch directly (None for main worktree)
+
+ // Worktree state - priority: prunable > locked (1 char max)
+ let worktree_state = if data.prunable.is_some() {
+ "⌫".to_string() // Prunable (directory missing)
+ } else if data.locked.is_some() {
+ "⊠".to_string() // Locked (protected)
+ } else {
+ String::new()
+ };
+
+ // Determine base branch state (only for non-main worktrees with base branch)
+ let base_state = determine_worktree_base_state(
+ data.is_main,
+ default_branch,
+ counts.ahead,
+ data.working_tree_diff.as_ref(),
+ &data.working_tree_diff_with_main,
+ );
+
+ // Apply priority: Conflicts > Rebase > Merge > MergeTreeConflicts > base_state
+ let branch_op_state = if has_conflicts {
+ BranchOpState::Conflicts
+ } else if data.worktree_state.as_deref() == Some("rebase") {
+ BranchOpState::Rebase
+ } else if data.worktree_state.as_deref() == Some("merge") {
+ BranchOpState::Merge
+ } else if has_merge_tree_conflicts {
+ BranchOpState::MergeTreeConflicts
+ } else {
+ base_state
+ };
+
+ // Override main_divergence for the main worktree
+ let main_divergence = if data.is_main {
+ MainDivergence::IsMain
+ } else {
+ main_divergence
+ };
+
+ self.status_symbols = Some(StatusSymbols {
+ branch_op_state,
+ worktree_state,
+ main_divergence,
+ upstream_divergence,
+ working_tree: working_tree_symbols.unwrap_or("").to_string(),
+ user_status,
+ });
+ }
+ ItemKind::Branch => {
+ // Simplified status computation for branches
+ // Only compute symbols that apply to branches (no working tree, git operation, or worktree attrs)
+
+ // Branch op state - branches can only show MergeTreeConflicts or NoCommits
+ // (MatchesMain only applies to worktrees since branches don't have working trees)
+ let branch_op_state = if has_merge_tree_conflicts {
+ BranchOpState::MergeTreeConflicts
+ } else if let Some(ref c) = self.counts {
+ if c.ahead == 0 {
+ BranchOpState::NoCommits
+ } else {
+ BranchOpState::None
+ }
+ } else {
+ BranchOpState::None
+ };
+
+ self.status_symbols = Some(StatusSymbols {
+ branch_op_state,
+ worktree_state: "⎇".to_string(), // Branch indicator
+ main_divergence,
+ upstream_divergence,
+ working_tree: String::new(),
+ user_status,
+ });
+ }
+ }
+ }
+}
+
+/// Determine branch state for a worktree.
+///
+/// Returns:
+/// - `BranchOpState::None` if main worktree or no base branch
+/// - `BranchOpState::NoCommits` if no commits ahead and working tree is clean
+/// - `BranchOpState::MatchesMain` if ahead == 0 but dirty tree happens to match main (rare)
+/// - `BranchOpState::None` otherwise
+fn determine_worktree_base_state(
+ is_main: bool,
+ default_branch: Option<&str>,
+ ahead: usize,
+ working_tree_diff: Option<&LineDiff>,
+ working_tree_diff_with_main: &Option<Option<LineDiff>>,
+) -> BranchOpState {
+ if is_main || default_branch.is_none() {
+ return BranchOpState::None;
+ }
+
+ let is_clean = working_tree_diff.map(|d| d.is_empty()).unwrap_or(true);
+
+ // Check if no commits and clean working tree (most common removable state)
+ if ahead == 0 && is_clean {
+ return BranchOpState::NoCommits;
+ }
+
+ // Check if working tree matches main exactly (requires diff with main to be computed)
+ // This is rare - only when ahead == 0, tree is dirty, but dirty changes happen to match main
+ if let Some(Some(mdiff)) = working_tree_diff_with_main.as_ref()
+ && mdiff.is_empty()
+ && ahead == 0
+ {
+ BranchOpState::MatchesMain
+ } else {
+ BranchOpState::None
+ }
}
/// Main branch divergence state