Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
refactor: Simplify error helpers and move width calculations to styling
Maximilian Roosaab6a8a071b0
diff --git a/src/commands/list/model.rs b/src/commands/list/model.rsindex 64c58c37c..28f8be1c6 100644--- a/src/commands/list/model.rs+++ b/src/commands/list/model.rs@@ -531,10 +531,9 @@ impl StatusSymbols {////// See [`StatusSymbols`] struct doc for symbol categories.pub fn render_with_mask(&self, mask: &PositionMask) -> String {- use unicode_width::UnicodeWidthStr;- use worktrunk::styling::{CYAN, ERROR, HINT, WARNING};+ use worktrunk::styling::{CYAN, ERROR, HINT, StyledLine, WARNING};- let mut result = String::with_capacity(12);+ let mut result = String::with_capacity(64);if self.is_empty() {return result;@@ -586,57 +585,46 @@ impl StatusSymbols {};let user_status_str = self.user_status.as_deref().unwrap_or("").to_string();- // Track (position, styled_content, visual_width, has_data)- // visual_width is the actual display width without ANSI codes+ // Position data: (position_mask, styled_content, has_data)+ // StyledLine handles width tracking automatically via .width()//// CRITICAL: Display order is working_tree first, 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, &str, usize, bool); 7] = [+ let positions_data: [(usize, String, bool); 7] = [(PositionMask::WORKING_TREE,- working_tree_str.as_str(),- self.working_tree.width(),+ working_tree_str,!self.working_tree.is_empty(),),(PositionMask::BRANCH_STATE,- branch_state_str.as_str(),- if self.branch_state != BranchState::None {- 1- } else {- 0- },+ branch_state_str,self.branch_state != BranchState::None,),(PositionMask::GIT_OPERATION,- git_operation_str.as_str(),- self.git_operation.to_string().width(),+ git_operation_str,self.git_operation != GitOperation::None,),(PositionMask::MAIN_DIVERGENCE,- main_divergence_str.as_str(),- self.main_divergence.to_string().width(),+ main_divergence_str,self.main_divergence != MainDivergence::None,),(PositionMask::UPSTREAM_DIVERGENCE,- upstream_divergence_str.as_str(),- self.upstream_divergence.to_string().width(),+ upstream_divergence_str,self.upstream_divergence != UpstreamDivergence::None,),(PositionMask::ITEM_ATTRS,- item_attrs_str.as_str(),- self.item_attrs.width(),+ item_attrs_str,!self.item_attrs.is_empty(),),(PositionMask::USER_STATUS,- user_status_str.as_str(),- self.user_status.as_ref().map(|s| s.width()).unwrap_or(0),+ user_status_str,self.user_status.is_some(),),];@@ -645,16 +633,15 @@ impl StatusSymbols {// 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, visual_width, has_data) in positions_data.iter() {- let allocated_width = mask.width(*pos);-- if *has_data {- result.push_str(styled_content);- // Pad to allocated width for alignment- let padding = allocated_width.saturating_sub(*visual_width);- for _ in 0..padding {- result.push(' ');- }+ 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 alignmentfor _ in 0..allocated_width {diff --git a/src/commands/list/render.rs b/src/commands/list/render.rsindex d70cc7517..3cf30540b 100644--- a/src/commands/list/render.rs+++ b/src/commands/list/render.rs@@ -369,7 +369,6 @@ impl LayoutConfig {/// Render a skeleton row showing known data (branch, path) with placeholders for other columnspub fn format_skeleton_row(&self, item: &super::model::ListItem, is_current: bool) -> String {use crate::display::shorten_path;- use unicode_width::UnicodeWidthStr;let branch = item.branch_name();let is_main = item.is_main();@@ -413,11 +412,7 @@ impl LayoutConfig {dim};cell.push_styled(branch, style);- // Pad to column width- let branch_width = branch.width();- if branch_width < col.width {- cell.push_raw(" ".repeat(col.width - branch_width));- }+ cell.pad_to(col.width);}ColumnKind::Path => {// Show actual path@@ -431,11 +426,7 @@ impl LayoutConfig {dim};cell.push_styled(&shortened_path, style);- // Pad to column width- let path_width = shortened_path.width();- if path_width < col.width {- cell.push_raw(" ".repeat(col.width - path_width));- }+ cell.pad_to(col.width);}ColumnKind::Commit => {// Show actual commit hash (always available)@@ -446,9 +437,7 @@ impl LayoutConfig {_ => {// Show spinner for data columnscell.push_styled(spinner, dim);- if spinner.width() < col.width {- cell.push_raw(" ".repeat(col.width - spinner.width()));- }+ cell.pad_to(col.width);}}@@ -586,10 +575,7 @@ impl ColumnLayout {// Truncate if exceeds column width, then padlet mut cell = cell.truncate_to_width(self.width);- let status_width = cell.width();- if status_width < self.width {- cell.push_raw(" ".repeat(self.width - status_width));- }+ cell.pad_to(self.width);cell}ColumnKind::WorkingDiff => {