Skip to content

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

refactor(list): derive the Status column width from PositionMask (#3845)

Worktrunk Bote07deadf6962
diff --git a/src/commands/list/layout.rs b/src/commands/list/layout.rs
index 9401a98cf..a9be4afaf 100644
--- a/src/commands/list/layout.rs
+++ b/src/commands/list/layout.rs
@@ -7,29 +7,33 @@
//!
//! ## Unified Position Grid
//!
-//! All status indicators use position-based alignment with selective rendering.
+//! All status indicators use position-based alignment.
//! See [`super::model::StatusSymbols`] for the complete symbol list and categories.
//!
-//! Only positions used by at least one row are included (position mask):
-//! - Within those positions, symbols align vertically for scannability
-//! - Empty positions render as single space for grid alignment
+//! Every row allocates every position ([`super::model::PositionMask::FULL`] is
+//! the only mask anything constructs):
+//! - Symbols align vertically at their position for scannability
+//! - Empty positions render as whitespace padded to the position's width
//! - No leading spaces before the first symbol
//!
-//! Example with working_tree, main_state, and user_marker used:
+//! Example with working_tree, main_state, and user_marker carrying data. Every
+//! row is the same eight columns wide, one per allocated position:
//! ```text
-//! Row 1: " _πŸ€–" (working=space, main=_, user=πŸ€–)
-//! Row 2: "?! _ " (working=?!, main=_, user=space)
-//! Row 3: " πŸ’¬" (working=space, main=space, user=πŸ’¬)
+//! Row 1: " _ πŸ€–" (working=clean, main=_, user=πŸ€–)
+//! Row 2: " !? _ " (working=!?, main=_, user=none)
+//! Row 3: " πŸ’¬" (working=clean, main=none, user=πŸ’¬)
//! ```
//!
//! ## Width Calculation
//!
//! ```text
-//! status_width = max(rendered_width_across_all_items)
+//! status_width = max(header_width, PositionMask::FULL.total_width())
//! ```
//!
-//! The width is calculated by rendering each item's status with the position
-//! mask and taking the maximum width.
+//! The width is not measured across items: the column's geometry is chosen at
+//! skeleton time, before any task result exists, and progressive and final
+//! renders have to agree on it. So the layout budgets the mask's total width
+//! and every render draws exactly that.
//!
//! ## Why This Design?
//!
@@ -37,13 +41,9 @@
//! - One alignment mechanism for all status indicators
//! - User marker treated consistently with git symbols
//!
-//! **Eliminates wasted space:**
-//! - Position mask removes columns for symbols that appear in zero rows
-//! - User marker only takes space when present
-//!
//! **Maintains alignment:**
//! - All symbols align vertically at their positions (vertical scannability)
-//! - Grid adapts to minimize width based on active positions
+//! - The grid never shifts as results arrive
//!
//! # Priority System Design
//!
@@ -754,9 +754,14 @@ fn build_estimated_widths(
// Fixed widths for slow columns (require expensive git operations)
// Values exceeding these widths use compact notation (K suffix)
//
- // Status column: Must match PositionMask::FULL width for consistent alignment
- // PositionMask::FULL allocates: 1+1+1+1+1+1+2 = 8 chars (7 positions)
- let status_fixed = fit_header(ColumnKind::Status.header(), 8);
+ // Status column: every row renders all seven positions of
+ // `PositionMask::FULL`, so the column has to be at least that wide or the
+ // cell overflows it. Read the width off the mask rather than restating its
+ // arithmetic β€” the two can't drift.
+ let status_fixed = fit_header(
+ ColumnKind::Status.header(),
+ super::model::PositionMask::FULL.total_width(),
+ );
let working_diff_fixed = fit_header(ColumnKind::WorkingDiff.header(), 9); // "+999 -999"
let ahead_behind_fixed = fit_header(ColumnKind::AheadBehind.header(), 7); // "↑99 ↓99"
let branch_diff_fixed = fit_header(ColumnKind::BranchDiff.header(), 9); // "+999 -999"
@@ -1155,7 +1160,7 @@ fn allocate_columns_with_priority(
/// - Paths (relative to main worktree)
///
/// Pre-allocated estimates (generous to minimize truncation):
-/// - Status: 8 chars (PositionMask::FULL, 7 positions)
+/// - Status: `PositionMask::FULL.total_width()` (7 positions, 8 chars today)
/// - Working diff: 9 chars ("+999 -999")
/// - Ahead/behind: 7 chars ("↑99 ↓99")
/// - Branch diff: 9 chars ("+999 -999")
diff --git a/src/commands/list/model/status_symbols.rs b/src/commands/list/model/status_symbols.rs
index a53065efe..1f11cf94d 100644
--- a/src/commands/list/model/status_symbols.rs
+++ b/src/commands/list/model/status_symbols.rs
@@ -245,19 +245,22 @@
use super::state::{Divergence, MainState, OperationState, WorktreeState};
-/// Tracks which status symbol positions are actually used across all items
-/// and the maximum width needed for each position.
+/// Per-position character widths for the Status column, used to pad each
+/// position so symbols line up vertically across rows.
///
-/// This allows the Status column to:
-/// 1. Only allocate space for positions that have data
-/// 2. Pad each position to a consistent width for vertical alignment
+/// The widths are fixed, not measured: [`FULL`](Self::FULL) is the only mask
+/// anything constructs. The Status column's geometry is chosen at skeleton
+/// time, before any task result exists, and progressive and final renders have
+/// to agree on it β€” a mask narrowed to the positions that happen to carry data
+/// would shift columns as results arrived. So every render allocates every
+/// position, and [`total_width`](Self::total_width) is what the layout budgets
+/// for the cell.
///
-/// Stores maximum character width for each of 7 positions (including user marker).
-/// A width of 0 means the position is unused.
-#[derive(Debug, Clone, Copy, Default)]
+/// No `Default`: an all-zero mask would silently collapse the cell to nothing,
+/// and there is no caller that wants one.
+#[derive(Debug, Clone, Copy)]
pub struct PositionMask {
- /// Maximum width for each position: [0, 1, 2, 3, 4, 5, 6]
- /// 0 = position unused, >0 = max characters needed
+ /// Character width allocated to each position: [0, 1, 2, 3, 4, 5, 6]
widths: [usize; 7],
}
@@ -290,6 +293,16 @@ impl PositionMask {
pub(crate) fn width(&self, pos: usize) -> usize {
self.widths[pos]
}
+
+ /// Total characters this mask renders β€” the sum of every position's
+ /// allocated width.
+ ///
+ /// The layout budgets the Status column from `FULL.total_width()` rather
+ /// than a literal, so widening a position here can't leave the column one
+ /// character short of what [`StatusSymbols::render_with_mask`] emits.
+ pub(crate) fn total_width(&self) -> usize {
+ self.widths.iter().sum()
+ }
}
/// Working tree changes as structured booleans
@@ -437,8 +450,9 @@ impl StatusSymbols {
/// - `Visible(s)` β†’ styled content padded to the slot width.
///
/// 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.
+ /// between progressive and final rendering. The mask gives each position
+ /// its allocated width, and the cell the layout budgeted is
+ /// [`PositionMask::total_width`].
pub fn render_with_mask(&self, mask: &PositionMask, placeholder: &str) -> String {
use anstyle::Style;
use worktrunk::styling::StyledLine;
@@ -786,9 +800,38 @@ mod tests {
assert_snapshot!(rendered, @"Β· Β·↑Β·Β·");
}
+ /// The layout budgets the Status column from `FULL.total_width()`, so that
+ /// number has to be what a rendered cell actually draws β€” otherwise a
+ /// widened position overflows the column it was allocated. Both the
+ /// all-loading cell and a fully-populated one are pinned, since every
+ /// position pads to its allocated width in either state.
+ #[test]
+ fn test_full_mask_total_width_matches_rendered_cell() {
+ use ansi_str::AnsiStr;
+ use unicode_width::UnicodeWidthStr;
+
+ let visible_width = |rendered: &str| UnicodeWidthStr::width(rendered.ansi_strip().as_ref());
+
+ let loading = StatusSymbols::default().render_with_mask(&PositionMask::FULL, "Β·");
+ assert_eq!(visible_width(&loading), PositionMask::FULL.total_width());
+
+ let populated = StatusSymbols {
+ working_tree: Some(WorkingTreeStatus::new(true, true, true, false, false)),
+ operation_state: Some(OperationState::InProgress(InProgressOperation::Rebase)),
+ worktree_state: Some(WorktreeState::None),
+ main_state: Some(MainState::Ahead),
+ upstream_divergence: Some(Divergence::Ahead),
+ // Two columns wide, matching the USER_MARKER allocation.
+ user_marker: Some(Some("πŸ”₯".to_string())),
+ }
+ .render_with_mask(&PositionMask::FULL, "Β·");
+ assert_eq!(visible_width(&populated), PositionMask::FULL.total_width());
+ }
+
#[test]
fn test_position_mask_width() {
let mask = PositionMask::FULL;
+ assert_eq!(mask.total_width(), 8);
// Check expected widths for each position
assert_eq!(mask.width(PositionMask::STAGED), 1);
assert_eq!(mask.width(PositionMask::MODIFIED), 1);