Skip to content

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

feat: improve performance and eliminate Worktree parameters

Maximilian Roosdb9dfdde8991
diff --git a/src/commands/list/collect.rs b/src/commands/list/collect.rs
index 8bd93037d..59ce052ab 100644
--- a/src/commands/list/collect.rs
+++ b/src/commands/list/collect.rs
@@ -118,7 +118,7 @@ pub(super) fn detect_worktree_state(repo: &Repository) -> Option<String> {
/// It will recompute with the latest available data.
///
/// Branches get a subset of status symbols (no working tree, git operation, or worktree attrs).
-fn compute_item_status_symbols(item: &mut ListItem, primary: &Worktree) {
+fn compute_item_status_symbols(item: &mut ListItem, base_branch: Option<&str>) {
// Common fields for both worktrees and branches
let default_counts = AheadBehind::default();
let default_upstream = UpstreamStatus::default();
@@ -130,10 +130,7 @@ fn compute_item_status_symbols(item: &mut ListItem, primary: &Worktree) {
match &item.kind {
ItemKind::Worktree(data) => {
// Full status computation for worktrees
- let base_branch = primary
- .branch
- .as_deref()
- .filter(|_| data.path != primary.path);
+ // Use base_branch directly (None for primary worktree)
// Worktree attributes (used directly from data instead of separate Worktree)
let mut worktree_attrs = String::new();
@@ -410,53 +407,6 @@ pub fn collect(
Vec::new()
};
- let branch_names: Vec<String> = branches_without_worktrees
- .iter()
- .map(|(name, _sha)| name.clone())
- .collect();
-
- // Calculate layout from basic worktree info + branch names
- let layout = super::layout::calculate_layout_from_basics(
- &sorted_worktrees,
- &branch_names,
- show_full,
- fetch_ci,
- );
-
- // Single-line invariant: use safe width to prevent line wrapping
- // (which breaks indicatif's line-based cursor math).
- let max_width = super::layout::get_safe_list_width();
-
- let clamp = |s: &str| -> String {
- if console::measure_text_width(s) > max_width {
- console::truncate_str(s, max_width, "…").into_owned()
- } else {
- s.to_owned()
- }
- };
-
- // Create MultiProgress with explicit draw target and cursor mode
- // Use stderr for progress bars so they don't interfere with stdout directives
- let multi = if show_progress {
- let mp = MultiProgress::with_draw_target(indicatif::ProgressDrawTarget::stderr_with_hz(10));
- mp.set_move_cursor(true); // Stable since bar count is fixed
- mp
- } else {
- MultiProgress::new()
- };
-
- let message_style = ProgressStyle::with_template("{msg}").unwrap();
-
- // Create header progress bar (part of transient UI, cleared with finish_and_clear)
- let header_pb = if show_progress {
- let pb = multi.add(ProgressBar::hidden());
- pb.set_style(message_style.clone());
- pb.set_message(clamp(&layout.format_header_line()));
- Some(pb)
- } else {
- None
- };
-
// Initialize worktree items with identity fields and None for computed fields
let mut all_items: Vec<super::model::ListItem> = sorted_worktrees
.iter()
@@ -501,27 +451,61 @@ pub fn collect(
});
}
+ // Calculate layout from items (both worktrees and branches)
+ let layout = super::layout::calculate_layout_from_basics(&all_items, show_full, fetch_ci);
+
+ // Single-line invariant: use safe width to prevent line wrapping
+ // (which breaks indicatif's line-based cursor math).
+ let max_width = super::layout::get_safe_list_width();
+
+ let clamp = |s: &str| -> String {
+ if console::measure_text_width(s) > max_width {
+ console::truncate_str(s, max_width, "…").into_owned()
+ } else {
+ s.to_owned()
+ }
+ };
+
+ // Create MultiProgress with explicit draw target and cursor mode
+ // Use stderr for progress bars so they don't interfere with stdout directives
+ let multi = if show_progress {
+ let mp = MultiProgress::with_draw_target(indicatif::ProgressDrawTarget::stderr_with_hz(10));
+ mp.set_move_cursor(true); // Stable since bar count is fixed
+ mp
+ } else {
+ MultiProgress::new()
+ };
+
+ let message_style = ProgressStyle::with_template("{msg}").unwrap();
+
+ // Create header progress bar (part of transient UI, cleared with finish_and_clear)
+ let header_pb = if show_progress {
+ let pb = multi.add(ProgressBar::hidden());
+ pb.set_style(message_style.clone());
+ pb.set_message(clamp(&layout.format_header_line()));
+ Some(pb)
+ } else {
+ None
+ };
+
// Create progress bars for all items (worktrees + branches)
let progress_bars: Vec<_> = all_items
.iter()
- .enumerate()
- .map(|(idx, item)| {
+ .map(|item| {
let pb = multi.add(ProgressBar::new_spinner());
if show_progress {
pb.set_style(message_style.clone());
// Render skeleton immediately with clamping
- let skeleton = if idx < sorted_worktrees.len() {
- // Worktree skeleton
- let wt = &sorted_worktrees[idx];
- let is_primary = wt.path == primary.path;
- let is_current = current_worktree_path
- .as_ref()
- .map(|cp| &wt.path == cp)
+ let skeleton = if item.worktree_data().is_some() {
+ // Worktree skeleton - show known data (branch, path, commit)
+ let is_current = item
+ .worktree_path()
+ .and_then(|p| current_worktree_path.as_ref().map(|cp| p == cp))
.unwrap_or(false);
- layout.format_skeleton_row(wt, is_primary, is_current)
+ layout.format_skeleton_row(item, is_current)
} else {
- // Branch skeleton
+ // Branch skeleton - use full item rendering
layout.format_list_item_line(item, current_worktree_path.as_ref())
};
pb.set_message(clamp(&skeleton));
@@ -537,8 +521,11 @@ pub fn collect(
// Footer progress bar with loading status
// Uses determinate bar (no spinner) with {wide_msg} to prevent clearing artifacts
let total_cells = all_items.len() * layout.columns.len();
- let num_worktrees = sorted_worktrees.len();
- let num_branches = branches_without_worktrees.len();
+ let num_worktrees = all_items
+ .iter()
+ .filter(|item| item.worktree_data().is_some())
+ .count();
+ let num_branches = all_items.len() - num_worktrees;
let footer_base = if show_branches && num_branches > 0 {
format!(
"Showing {} worktrees, {} branches",
@@ -632,10 +619,13 @@ pub fn collect(
let mut completed_cells = 0;
// Drain cell updates with conditional progressive rendering
+ let base_branch = primary.branch.as_deref();
drain_cell_updates(rx, &mut all_items, |item_idx, info| {
// Compute/recompute status symbols as data arrives (both modes)
// This is idempotent and updates status as new data (like upstream) arrives
- compute_item_status_symbols(info, &primary);
+ // Base branch is None for primary worktree, Some(primary.branch) for others
+ let item_base_branch = if info.is_primary() { None } else { base_branch };
+ compute_item_status_symbols(info, item_base_branch);
// Progressive mode only: update UI
if show_progress {
diff --git a/src/commands/list/layout.rs b/src/commands/list/layout.rs
index e73d68a21..4108e41b0 100644
--- a/src/commands/list/layout.rs
+++ b/src/commands/list/layout.rs
@@ -840,38 +840,37 @@ pub fn calculate_responsive_layout(
///
/// Note: This is work-in-progress for improved progressive rendering.
pub fn calculate_layout_from_basics(
- worktrees: &[worktrunk::git::Worktree],
- branch_names: &[String],
+ items: &[super::model::ListItem],
show_full: bool,
fetch_ci: bool,
) -> LayoutConfig {
let terminal_width = get_safe_list_width();
- // Calculate common prefix from paths
- let paths: Vec<&Path> = worktrees.iter().map(|wt| wt.path.as_path()).collect();
+ // Calculate common prefix from worktree paths
+ let paths: Vec<&Path> = items
+ .iter()
+ .filter_map(|item| item.worktree_path())
+ .map(|p| p.as_path())
+ .collect();
let common_prefix = find_common_prefix(&paths);
// Calculate actual widths for things we know
- // Include both worktree branch names AND branch names without worktrees
- let max_worktree_branch = worktrees
+ // Include branch names from both worktrees and standalone branches
+ let max_branch = items
.iter()
- .filter_map(|wt| wt.branch.as_deref())
+ .filter_map(|item| item.branch.as_deref())
.map(|b| b.width())
.max()
.unwrap_or(0);
- let max_standalone_branch = branch_names.iter().map(|b| b.width()).max().unwrap_or(0);
-
- let max_branch = fit_header(
- HEADER_BRANCH,
- max_worktree_branch.max(max_standalone_branch),
- );
+ let max_branch = fit_header(HEADER_BRANCH, max_branch);
- let path_data_width = worktrees
+ let path_data_width = items
.iter()
- .map(|wt| {
+ .filter_map(|item| item.worktree_path())
+ .map(|path| {
use crate::display::shorten_path;
- shorten_path(wt.path.as_path(), &common_prefix).width()
+ shorten_path(path.as_path(), &common_prefix).width()
})
.max()
.unwrap_or(0);
diff --git a/src/commands/list/model.rs b/src/commands/list/model.rs
index 9aae164c8..b0c71193e 100644
--- a/src/commands/list/model.rs
+++ b/src/commands/list/model.rs
@@ -120,13 +120,13 @@ pub struct CommitDetails {
pub commit_message: String,
}
-#[derive(serde::Serialize, Default, Clone, Debug)]
+#[derive(serde::Serialize, Default, Copy, Clone, Debug)]
pub struct AheadBehind {
pub ahead: usize,
pub behind: usize,
}
-#[derive(serde::Serialize, Default, Clone, Debug)]
+#[derive(serde::Serialize, Default, Copy, Clone, Debug)]
pub struct BranchDiffTotals {
#[serde(rename = "branch_diff")]
pub diff: LineDiff,
@@ -226,11 +226,11 @@ impl ListItem {
}
pub fn counts(&self) -> AheadBehind {
- self.counts.clone().unwrap_or_default()
+ self.counts.unwrap_or_default()
}
pub fn branch_diff(&self) -> BranchDiffTotals {
- self.branch_diff.clone().unwrap_or_default()
+ self.branch_diff.unwrap_or_default()
}
pub fn upstream(&self) -> UpstreamStatus {
diff --git a/src/commands/list/render.rs b/src/commands/list/render.rs
index 98238a861..215da2834 100644
--- a/src/commands/list/render.rs
+++ b/src/commands/list/render.rs
@@ -360,17 +360,16 @@ impl LayoutConfig {
}
/// Render a skeleton row showing known data (branch, path) with placeholders for other columns
- pub fn format_skeleton_row(
- &self,
- wt: &worktrunk::git::Worktree,
- is_primary: bool,
- is_current: bool,
- ) -> String {
+ pub fn format_skeleton_row(&self, item: &super::model::ListItem, is_current: bool) -> String {
use crate::display::shorten_path;
use unicode_width::UnicodeWidthStr;
- let branch = wt.branch.as_deref().unwrap_or("(detached)");
- let shortened_path = shorten_path(&wt.path, &self.common_prefix);
+ let branch = item.branch_name();
+ let is_primary = item.is_primary();
+ let shortened_path = item
+ .worktree_path()
+ .map(|p| shorten_path(p, &self.common_prefix))
+ .unwrap_or_default();
let dim = Style::new().dimmed();
let spinner = "⋯"; // Placeholder character
@@ -417,7 +416,8 @@ impl LayoutConfig {
}
ColumnKind::Commit => {
// Show actual commit hash (always available)
- let short_head = &wt.head[..8.min(wt.head.len())];
+ let head = item.head();
+ let short_head = &head[..8.min(head.len())];
cell.push_styled(short_head, dim);
}
_ => {