Skip to content

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

Move logic to struct methods

Maximilian Rooscd3712c8feb0
diff --git a/src/commands/list/layout.rs b/src/commands/list/layout.rs
index 0cbab6980..c524bfeec 100644
--- a/src/commands/list/layout.rs
+++ b/src/commands/list/layout.rs
@@ -527,7 +527,7 @@ pub fn calculate_column_widths(items: &[ListItem], fetch_ci: bool) -> LayoutMeta
// Position mask is collected here for selective rendering
if let Some(info) = worktree_info {
// Collect position usage from this item's status symbols
- let item_mask = super::model::PositionMask::from_symbols(&info.status_symbols);
+ let item_mask = info.status_symbols.position_mask();
status_position_mask.merge(&item_mask);
}
diff --git a/src/commands/list/mod.rs b/src/commands/list/mod.rs
index b35254219..6077a6b48 100644
--- a/src/commands/list/mod.rs
+++ b/src/commands/list/mod.rs
@@ -13,9 +13,7 @@ use super::repository_ext::RepositoryCliExt;
use columns::ColumnKind;
use layout::calculate_responsive_layout;
use model::{ListData, ListItem};
-use render::{
- format_ci_status_plain, format_diff_plain, format_header_line, format_list_item_line,
-};
+use render::{format_diff_plain, format_list_item_line};
use worktrunk::git::{GitError, Repository};
use worktrunk::styling::{INFO_EMOJI, println};
@@ -37,7 +35,7 @@ fn enrich_common_fields(
format_diff_plain(ColumnKind::Upstream, upstream_ahead, upstream_behind)
});
- let ci_status_display = pr_status.as_ref().map(format_ci_status_plain);
+ let ci_status_display = pr_status.as_ref().map(ci_status::PrStatus::format_plain);
model::DisplayFields {
commits_display,
@@ -107,7 +105,7 @@ pub fn handle_list(
}
crate::OutputFormat::Table => {
let layout = calculate_responsive_layout(&items, show_full, show_full);
- format_header_line(&layout);
+ layout.format_header_line();
for item in &items {
format_list_item_line(item, &layout, current_worktree_path.as_ref());
}
diff --git a/src/commands/list/model.rs b/src/commands/list/model.rs
index 5ef216f50..129e3ac0f 100644
--- a/src/commands/list/model.rs
+++ b/src/commands/list/model.rs
@@ -261,6 +261,23 @@ impl ListItem {
ListItem::Branch(info) => info.pr_status.as_ref(),
}
}
+
+ /// Determine if the item contains no unique work and can likely be removed.
+ pub(crate) fn is_potentially_removable(&self) -> bool {
+ if self.is_primary() {
+ return false;
+ }
+
+ let counts = self.counts();
+
+ if let Some(info) = self.worktree_info() {
+ let no_commits_and_clean = counts.ahead == 0 && info.working_tree_diff == (0, 0);
+ let matches_main = info.working_tree_diff_with_main == Some((0, 0));
+ no_commits_and_clean || matches_main
+ } else {
+ counts.ahead == 0
+ }
+ }
}
impl BranchInfo {
@@ -489,20 +506,6 @@ impl PositionMask {
positions: [true; 7],
};
- /// Create a mask from a StatusSymbols instance
- pub fn from_symbols(symbols: &StatusSymbols) -> Self {
- let mut positions = [false; 7];
- positions[Self::POS_0A_CONFLICTS] = symbols.has_conflicts;
- positions[Self::POS_0B_BRANCH_STATE] = symbols.branch_state != BranchState::None;
- positions[Self::POS_0C_GIT_OPERATION] = symbols.git_operation != GitOperation::None;
- positions[Self::POS_0D_WORKTREE_ATTRS] = !symbols.worktree_attrs.is_empty();
- positions[Self::POS_1_MAIN_DIVERGENCE] = symbols.main_divergence != MainDivergence::None;
- positions[Self::POS_2_UPSTREAM_DIVERGENCE] =
- symbols.upstream_divergence != UpstreamDivergence::None;
- positions[Self::POS_3_WORKING_TREE] = !symbols.working_tree.is_empty();
- Self { positions }
- }
-
/// Merge this mask with another, keeping positions that are used in either
pub fn merge(&mut self, other: &Self) {
for (i, &other_val) in other.positions.iter().enumerate() {
@@ -658,6 +661,21 @@ impl StatusSymbols {
result
}
+ /// Derive a position mask that tracks which symbol slots contain data.
+ pub fn position_mask(&self) -> PositionMask {
+ let mut positions = [false; 7];
+ positions[PositionMask::POS_0A_CONFLICTS] = self.has_conflicts;
+ positions[PositionMask::POS_0B_BRANCH_STATE] = self.branch_state != BranchState::None;
+ positions[PositionMask::POS_0C_GIT_OPERATION] = self.git_operation != GitOperation::None;
+ positions[PositionMask::POS_0D_WORKTREE_ATTRS] = !self.worktree_attrs.is_empty();
+ positions[PositionMask::POS_1_MAIN_DIVERGENCE] =
+ self.main_divergence != MainDivergence::None;
+ positions[PositionMask::POS_2_UPSTREAM_DIVERGENCE] =
+ self.upstream_divergence != UpstreamDivergence::None;
+ positions[PositionMask::POS_3_WORKING_TREE] = !self.working_tree.is_empty();
+ PositionMask { positions }
+ }
+
/// Check if symbols are empty
pub fn is_empty(&self) -> bool {
!self.has_conflicts
diff --git a/src/commands/list/render.rs b/src/commands/list/render.rs
index 8aefd753f..1246d7ab7 100644
--- a/src/commands/list/render.rs
+++ b/src/commands/list/render.rs
@@ -62,36 +62,44 @@ pub fn format_diff_plain(kind: ColumnKind, positive: usize, negative: usize) ->
format_plain_diff_from_config(positive, negative, config)
}
-/// Determine the style for a CI status (color + optional dimming)
-fn ci_status_style(pr_status: &PrStatus) -> Style {
- let color = match pr_status.ci_status {
- CiStatus::Passed => AnsiColor::Green,
- CiStatus::Running => AnsiColor::Blue,
- CiStatus::Failed => AnsiColor::Red,
- CiStatus::Conflicts => AnsiColor::Yellow,
- CiStatus::NoCI => AnsiColor::BrightBlack,
- };
+impl PrStatus {
+ /// Determine the style for a CI status (color + optional dimming)
+ fn style(&self) -> Style {
+ let color = match self.ci_status {
+ CiStatus::Passed => AnsiColor::Green,
+ CiStatus::Running => AnsiColor::Blue,
+ CiStatus::Failed => AnsiColor::Red,
+ CiStatus::Conflicts => AnsiColor::Yellow,
+ CiStatus::NoCI => AnsiColor::BrightBlack,
+ };
- if pr_status.is_stale {
- Style::new().fg_color(Some(Color::Ansi(color))).dimmed()
- } else {
- Style::new().fg_color(Some(Color::Ansi(color)))
+ if self.is_stale {
+ Style::new().fg_color(Some(Color::Ansi(color))).dimmed()
+ } else {
+ Style::new().fg_color(Some(Color::Ansi(color)))
+ }
}
-}
-/// Format CI status as plain text with ANSI colors (for json-pretty)
-pub fn format_ci_status_plain(pr_status: &PrStatus) -> String {
- let style = ci_status_style(pr_status);
+ /// Format CI status as plain text with ANSI colors (for json-pretty)
+ pub fn format_plain(&self) -> String {
+ let style = self.style();
- let status_str = match pr_status.ci_status {
- CiStatus::Passed => "passed",
- CiStatus::Running => "running",
- CiStatus::Failed => "failed",
- CiStatus::Conflicts => "conflicts",
- CiStatus::NoCI => "no-ci",
- };
+ let status_str = match self.ci_status {
+ CiStatus::Passed => "passed",
+ CiStatus::Running => "running",
+ CiStatus::Failed => "failed",
+ CiStatus::Conflicts => "conflicts",
+ CiStatus::NoCI => "no-ci",
+ };
+
+ format!("{}● {}{}", style, status_str, style.render_reset())
+ }
- format!("{}● {}{}", style, status_str, style.render_reset())
+ fn render_indicator(&self) -> StyledLine {
+ let mut segment = StyledLine::new();
+ segment.push_styled("●".to_string(), self.style());
+ segment
+ }
}
#[derive(Clone, Copy)]
@@ -179,81 +187,41 @@ fn format_diff_like_column(
segment
}
-/// Format CI status indicator using the statusline.sh color scheme
-fn format_ci_status(pr_status: &PrStatus) -> StyledLine {
- let mut segment = StyledLine::new();
- let style = ci_status_style(pr_status);
- segment.push_styled("●".to_string(), style);
- segment
-}
-
-fn render_line<F>(layout: &LayoutConfig, mut render_cell: F) -> StyledLine
-where
- F: FnMut(&ColumnLayout) -> StyledLine,
-{
- let mut line = StyledLine::new();
- if layout.columns.is_empty() {
- return line;
- }
-
- let last_index = layout.columns.len() - 1;
-
- for (index, column) in layout.columns.iter().enumerate() {
- line.pad_to(column.start);
- line.extend(render_cell(column));
-
- if index != last_index {
- line.pad_to(column.start + column.width);
+impl LayoutConfig {
+ fn render_line<F>(&self, mut render_cell: F) -> StyledLine
+ where
+ F: FnMut(&ColumnLayout) -> StyledLine,
+ {
+ let mut line = StyledLine::new();
+ if self.columns.is_empty() {
+ return line;
}
- }
- line
-}
+ let last_index = self.columns.len() - 1;
-pub fn format_header_line(layout: &LayoutConfig) {
- let style = Style::new().bold();
- let line = render_line(layout, |column| {
- let mut cell = StyledLine::new();
- if !column.header.is_empty() {
- cell.push_styled(column.header.to_string(), style);
- }
- cell
- });
+ for (index, column) in self.columns.iter().enumerate() {
+ line.pad_to(column.start);
+ line.extend(render_cell(column));
- println!("{}", line.render());
-}
+ if index != last_index {
+ line.pad_to(column.start + column.width);
+ }
+ }
-/// Check if a branch/worktree is potentially removable
-///
-/// Dims rows for worktrees/branches that likely don't contain unique work
-/// beyond what's already in the main branch. This helps focus attention on
-/// worktrees that contain work.
-///
-/// Dims when (using OR logic):
-/// - No commits AND clean working tree (ahead == 0 AND working_tree_diff == (0, 0)):
-/// The worktree has no commits ahead and no uncommitted changes
-/// - Working tree matches main (working_tree_diff_with_main == Some((0, 0))):
-/// The working tree contents are identical to main, regardless of commit history
-///
-/// Either condition alone is sufficient to dim, as both indicate "no unique work here".
-fn is_potentially_removable(item: &ListItem) -> bool {
- if item.is_primary() {
- return false;
+ line
}
- let counts = item.counts();
-
- if let Some(info) = item.worktree_info() {
- // Condition 1: No commits ahead AND no uncommitted changes
- let no_commits_and_clean = counts.ahead == 0 && info.working_tree_diff == (0, 0);
-
- // Condition 2: Working tree matches main (regardless of commit history)
- let matches_main = info.working_tree_diff_with_main == Some((0, 0));
+ pub fn format_header_line(&self) {
+ let style = Style::new().bold();
+ let line = self.render_line(|column| {
+ let mut cell = StyledLine::new();
+ if !column.header.is_empty() {
+ cell.push_styled(column.header.to_string(), style);
+ }
+ cell
+ });
- no_commits_and_clean || matches_main
- } else {
- // For branches without worktrees, just check if no commits ahead
- counts.ahead == 0
+ println!("{}", line.render());
}
}
@@ -311,7 +279,7 @@ fn resolve_text_style(
}
});
- if is_potentially_removable(item) {
+ if item.is_potentially_removable() {
Some(base_style.unwrap_or_default().dimmed())
} else {
base_style
@@ -370,7 +338,7 @@ fn render_list_cell(
else {
return StyledLine::new();
};
- render_diff_cell(column, added, deleted)
+ column.render_diff_cell(added, deleted)
}
ColumnKind::AheadBehind => {
if ctx.item.is_primary() {
@@ -381,13 +349,13 @@ fn render_list_cell(
if ahead == 0 && behind == 0 {
return StyledLine::new();
}
- render_diff_cell(column, ahead, behind)
+ column.render_diff_cell(ahead, behind)
}
ColumnKind::BranchDiff => {
if ctx.item.is_primary() {
return StyledLine::new();
}
- render_diff_cell(column, ctx.branch_diff.0, ctx.branch_diff.1)
+ column.render_diff_cell(ctx.branch_diff.0, ctx.branch_diff.1)
}
ColumnKind::Path => {
let Some(info) = ctx.worktree_info else {
@@ -406,7 +374,7 @@ fn render_list_cell(
let Some((_, ahead, behind)) = ctx.upstream.active() else {
return StyledLine::new();
};
- render_diff_cell(column, ahead, behind)
+ column.render_diff_cell(ahead, behind)
}
ColumnKind::Time => {
let mut cell = StyledLine::new();
@@ -418,7 +386,7 @@ fn render_list_cell(
let Some(pr_status) = ctx.item.pr_status() else {
return StyledLine::new();
};
- format_ci_status(pr_status)
+ pr_status.render_indicator()
}
ColumnKind::Commit => {
let mut cell = StyledLine::new();
@@ -434,14 +402,16 @@ fn render_list_cell(
}
}
-fn render_diff_cell(column: &ColumnLayout, positive: usize, negative: usize) -> StyledLine {
- let ColumnFormat::Diff(config) = column.format else {
- return StyledLine::new();
- };
+impl ColumnLayout {
+ fn render_diff_cell(&self, positive: usize, negative: usize) -> StyledLine {
+ let ColumnFormat::Diff(config) = self.format else {
+ return StyledLine::new();
+ };
- debug_assert_eq!(config.total_width, column.width);
+ debug_assert_eq!(config.total_width, self.width);
- format_diff_like_column(positive, negative, config)
+ format_diff_like_column(positive, negative, config)
+ }
}
/// Render a list item (worktree or branch) as a formatted line
@@ -456,7 +426,7 @@ pub fn format_list_item_line(
let common_prefix = &layout.common_prefix;
let max_message_len = layout.max_message_len;
- let line = render_line(layout, |column| {
+ let line = layout.render_line(|column| {
render_list_cell(
column,
&ctx,
diff --git a/src/commands/list/status_column_tests.rs b/src/commands/list/status_column_tests.rs
index 4ba973332..3f258e10c 100644
--- a/src/commands/list/status_column_tests.rs
+++ b/src/commands/list/status_column_tests.rs
@@ -30,7 +30,7 @@ mod status_column_rendering_tests {
// Expected: "≡"
#[test]
fn test_single_symbol_position_0b() {
- use super::super::model::{BranchState, PositionMask, StatusSymbols};
+ use super::super::model::{BranchState, StatusSymbols};
// Symbols: [≡]
// Max git width: 1
@@ -41,7 +41,7 @@ mod status_column_rendering_tests {
..Default::default()
};
- let mask = PositionMask::from_symbols(&symbols);
+ let mask = symbols.position_mask();
let result = symbols.render_with_mask(&mask);
assert_eq!(result, "≡");
@@ -52,7 +52,7 @@ mod status_column_rendering_tests {
// Expected: "!"
#[test]
fn test_single_symbol_position_3() {
- use super::super::model::{PositionMask, StatusSymbols};
+ use super::super::model::StatusSymbols;
// Symbols: [!]
// Max git width: 1
@@ -63,7 +63,7 @@ mod status_column_rendering_tests {
..Default::default()
};
- let mask = PositionMask::from_symbols(&symbols);
+ let mask = symbols.position_mask();
let result = symbols.render_with_mask(&mask);
assert_eq!(result, "!");
@@ -80,7 +80,7 @@ mod status_column_rendering_tests {
// Row 2: " !" (space in col 0, ! in col 1)
#[test]
fn test_two_different_positions_align() {
- use super::super::model::{BranchState, PositionMask, StatusSymbols};
+ use super::super::model::{BranchState, StatusSymbols};
// Position mask: [0b, 3] → 2-column grid
// Row 1: symbol at position 0b (col 0)
@@ -92,7 +92,7 @@ mod status_column_rendering_tests {
working_tree: "!".to_string(),
..Default::default()
};
- let mask = PositionMask::from_symbols(&mask_builder);
+ let mask = mask_builder.position_mask();
// Row 1: only position 0b
let row1 = StatusSymbols {
@@ -119,7 +119,7 @@ mod status_column_rendering_tests {
// Row 2: "?" (col 0 filled with ?)
#[test]
fn test_same_position_symbols_no_padding() {
- use super::super::model::{PositionMask, StatusSymbols};
+ use super::super::model::StatusSymbols;
// Position mask: [3] → 1-column grid
// Both rows have symbol at position 3 (col 0)
@@ -129,7 +129,7 @@ mod status_column_rendering_tests {
working_tree: "!".to_string(),
..Default::default()
};
- let mask = PositionMask::from_symbols(&mask_builder);
+ let mask = mask_builder.position_mask();
// Row 1: ! at position 3
let row1 = StatusSymbols {
@@ -154,7 +154,7 @@ mod status_column_rendering_tests {
// Expected: "≡?" (col 0=≡, col 1=?)
#[test]
fn test_multiple_symbols_one_row() {
- use super::super::model::{BranchState, PositionMask, StatusSymbols};
+ use super::super::model::{BranchState, StatusSymbols};
// Position mask: [0b, 3] → 2-column grid
// Row 1: symbols at both positions
@@ -165,7 +165,7 @@ mod status_column_rendering_tests {
..Default::default()
};
- let mask = PositionMask::from_symbols(&row);
+ let mask = row.position_mask();
assert_eq!(row.render_with_mask(&mask), "≡?");
}
@@ -180,7 +180,7 @@ mod status_column_rendering_tests {
// Row 2: "≡!" (col 0=≡, col 1=!)
#[test]
fn test_multiple_symbols_with_position_gap() {
- use super::super::model::{BranchState, PositionMask, StatusSymbols};
+ use super::super::model::{BranchState, StatusSymbols};
// Position mask: [0b, 3] → 2-column grid
// Row 1: col 0 filled, col 1 empty
@@ -192,7 +192,7 @@ mod status_column_rendering_tests {
working_tree: "!".to_string(),
..Default::default()
};
- let mask = PositionMask::from_symbols(&mask_builder);
+ let mask = mask_builder.position_mask();
// Row 1: only position 0b
let row1 = StatusSymbols {
@@ -214,13 +214,13 @@ mod status_column_rendering_tests {
// Expected: ""
#[test]
fn test_empty_status() {
- use super::super::model::{PositionMask, StatusSymbols};
+ use super::super::model::StatusSymbols;
// Git symbols: None
// User status: None
// Expected: "" (empty string)
let symbols = StatusSymbols::default();
- let mask = PositionMask::from_symbols(&symbols);
+ let mask = symbols.position_mask();
assert_eq!(symbols.render_with_mask(&mask), "");
}
@@ -239,7 +239,7 @@ mod status_column_rendering_tests {
// Row 3: " !" (col 0=space, col 1=space, col 2=!)
#[test]
fn test_three_different_positions() {
- use super::super::model::{BranchState, MainDivergence, PositionMask, StatusSymbols};
+ use super::super::model::{BranchState, MainDivergence, StatusSymbols};
// Position mask: [0b, 1, 3] → 3-column grid
// Create mask from all three positions
@@ -249,7 +249,7 @@ mod status_column_rendering_tests {
working_tree: "!".to_string(),
..Default::default()
};
- let mask = PositionMask::from_symbols(&mask_builder);
+ let mask = mask_builder.position_mask();
// Row 1: only position 0b
let row1 = StatusSymbols {
@@ -278,7 +278,7 @@ mod status_column_rendering_tests {
// Expected: "≡↻"
#[test]
fn test_adjacent_positions_no_space() {
- use super::super::model::{BranchState, GitOperation, PositionMask, StatusSymbols};
+ use super::super::model::{BranchState, GitOperation, StatusSymbols};
// Symbols: [≡↻]
// Position mask: 0b + 0c (adjacent)
@@ -289,7 +289,7 @@ mod status_column_rendering_tests {
..Default::default()
};
- let mask = PositionMask::from_symbols(&row);
+ let mask = row.position_mask();
assert_eq!(row.render_with_mask(&mask), "≡↻");
}
@@ -298,7 +298,7 @@ mod status_column_rendering_tests {
// Expected: "≡!+"
#[test]
fn test_all_positions_filled() {
- use super::super::model::{BranchState, PositionMask, StatusSymbols};
+ use super::super::model::{BranchState, StatusSymbols};
// Symbols: [≡!+]
// Position mask: 0b + 3 (+ continuation)
@@ -309,7 +309,7 @@ mod status_column_rendering_tests {
..Default::default()
};
- let mask = PositionMask::from_symbols(&row);
+ let mask = row.position_mask();
assert_eq!(row.render_with_mask(&mask), "≡!+");
}
@@ -323,7 +323,7 @@ mod status_column_rendering_tests {
// Row 3: "↓!+"
#[test]
fn test_real_world_complex_1() {
- use super::super::model::{BranchState, MainDivergence, PositionMask, StatusSymbols};
+ use super::super::model::{BranchState, MainDivergence, StatusSymbols};
// Position mask: 0b + 1 + 3 (3-column grid)
// Row 1: 0b=≡, 3=?, user=🤖 (user status tested separately)
@@ -337,7 +337,7 @@ mod status_column_rendering_tests {
working_tree: "!+".to_string(),
..Default::default()
};
- let mask = PositionMask::from_symbols(&mask_builder);
+ let mask = mask_builder.position_mask();
// Row 1: position 0b and 3 (note: user status alignment tested separately)
let row1 = StatusSymbols {
@@ -374,7 +374,7 @@ mod status_column_rendering_tests {
// Row 2: " !" (col 0=space, col 1=!)
#[test]
fn test_real_world_extreme_diffs() {
- use super::super::model::{BranchState, PositionMask, StatusSymbols};
+ use super::super::model::{BranchState, StatusSymbols};
// This is the actual failing test case from spacing_edge_cases
// Position mask: [0b, 3] → 2-column grid
@@ -387,7 +387,7 @@ mod status_column_rendering_tests {
working_tree: "?".to_string(),
..Default::default()
};
- let mask = PositionMask::from_symbols(&mask_builder);
+ let mask = mask_builder.position_mask();
// Row 1: both columns filled
let row1 = StatusSymbols {
@@ -420,7 +420,7 @@ mod status_column_rendering_tests {
// Row 1: "≡ " (7 chars with spaces for all positions)
#[test]
fn test_position_mask_removes_unused_positions() {
- use super::super::model::{BranchState, PositionMask, StatusSymbols};
+ use super::super::model::{BranchState, StatusSymbols};
// Position mask: [0b, 3] → 2-column grid
// Only used positions create columns
@@ -432,7 +432,7 @@ mod status_column_rendering_tests {
working_tree: "!".to_string(),
..Default::default()
};
- let mask = PositionMask::from_symbols(&mask_builder);
+ let mask = mask_builder.position_mask();
// Row 1: only position 0b
let row1 = StatusSymbols {