Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Refactor diff display logic into a single config type
Maximilian Roos9e2cc684ff4d
diff --git a/src/commands/list/layout.rs b/src/commands/list/layout.rsindex fbef13cad..0cbab6980 100644--- a/src/commands/list/layout.rs+++ b/src/commands/list/layout.rs@@ -356,6 +356,40 @@ impl From<DiffWidths> for DiffDigits {}}+#[derive(Clone, Copy, Debug)]+pub struct DiffDisplayConfig {+ pub variant: DiffVariant,+ pub positive_style: Style,+ pub negative_style: Style,+ pub always_show_zeros: bool,+}++impl ColumnKind {+ pub fn diff_display_config(self) -> Option<DiffDisplayConfig> {+ match self {+ ColumnKind::WorkingDiff | ColumnKind::BranchDiff => Some(DiffDisplayConfig {+ variant: DiffVariant::Signs,+ positive_style: ADDITION,+ negative_style: DELETION,+ always_show_zeros: false,+ }),+ ColumnKind::AheadBehind => Some(DiffDisplayConfig {+ variant: DiffVariant::Arrows,+ positive_style: ADDITION,+ negative_style: DELETION.dimmed(),+ always_show_zeros: false,+ }),+ ColumnKind::Upstream => Some(DiffDisplayConfig {+ variant: DiffVariant::Arrows,+ positive_style: ADDITION,+ negative_style: DELETION.dimmed(),+ always_show_zeros: true,+ }),+ _ => None,+ }+ }+}+#[derive(Clone, Copy, Debug)]pub enum ColumnFormat {Text,@@ -366,10 +400,7 @@ pub enum ColumnFormat {pub struct DiffColumnConfig {pub digits: DiffDigits,pub total_width: usize,- pub variant: DiffVariant,- pub positive_style: Style,- pub negative_style: Style,- pub always_show_zeros: bool,+ pub display: DiffDisplayConfig,}#[derive(Clone, Debug)]@@ -409,62 +440,24 @@ impl ColumnIdeal {}}- fn diff(widths: DiffWidths, variant: DiffVariant, kind: ColumnKind) -> Option<Self> {+ fn diff(widths: DiffWidths, kind: ColumnKind) -> Option<Self> {if widths.total == 0 {return None;}- let styles = diff_styles_for_kind(kind);+ let display = kind.diff_display_config()?;Some(Self {width: widths.total,format: ColumnFormat::Diff(DiffColumnConfig {digits: widths.into(),total_width: widths.total,- variant,- positive_style: styles.positive_style,- negative_style: styles.negative_style,- always_show_zeros: styles.always_show_zeros,+ display,}),})}}-#[derive(Clone, Copy)]-struct DiffRenderStyle {- positive_style: Style,- negative_style: Style,- always_show_zeros: bool,-}--fn diff_styles_for_kind(kind: ColumnKind) -> DiffRenderStyle {- match kind {- ColumnKind::WorkingDiff | ColumnKind::BranchDiff => DiffRenderStyle {- positive_style: ADDITION,- negative_style: DELETION,- always_show_zeros: false,- },- ColumnKind::AheadBehind => DiffRenderStyle {- positive_style: ADDITION,- negative_style: DELETION.dimmed(),- always_show_zeros: false,- },- ColumnKind::Upstream => DiffRenderStyle {- positive_style: ADDITION,- negative_style: DELETION.dimmed(),- always_show_zeros: true,- },- _ => {- debug_assert!(false, "diff_styles_for_kind called for non-diff column");- DiffRenderStyle {- positive_style: ADDITION,- negative_style: DELETION,- always_show_zeros: false,- }- }- }-}-#[derive(Clone, Copy)]struct ColumnCandidate<'a> {spec: &'a ColumnSpec,@@ -492,24 +485,10 @@ fn ideal_for_column(ColumnKind::CiStatus => ColumnIdeal::text(widths.ci_status),ColumnKind::Commit => ColumnIdeal::text(commit_width),ColumnKind::Message => None,- ColumnKind::WorkingDiff => ColumnIdeal::diff(- widths.working_diff,- DiffVariant::Signs,- ColumnKind::WorkingDiff,- ),- ColumnKind::AheadBehind => ColumnIdeal::diff(- widths.ahead_behind,- DiffVariant::Arrows,- ColumnKind::AheadBehind,- ),- ColumnKind::BranchDiff => ColumnIdeal::diff(- widths.branch_diff,- DiffVariant::Signs,- ColumnKind::BranchDiff,- ),- ColumnKind::Upstream => {- ColumnIdeal::diff(widths.upstream, DiffVariant::Arrows, ColumnKind::Upstream)- }+ ColumnKind::WorkingDiff => ColumnIdeal::diff(widths.working_diff, ColumnKind::WorkingDiff),+ ColumnKind::AheadBehind => ColumnIdeal::diff(widths.ahead_behind, ColumnKind::AheadBehind),+ ColumnKind::BranchDiff => ColumnIdeal::diff(widths.branch_diff, ColumnKind::BranchDiff),+ ColumnKind::Upstream => ColumnIdeal::diff(widths.upstream, ColumnKind::Upstream),}}diff --git a/src/commands/list/mod.rs b/src/commands/list/mod.rsindex 4765bf4f3..cb7133249 100644--- a/src/commands/list/mod.rs+++ b/src/commands/list/mod.rs@@ -9,14 +9,14 @@ mod spacing_test;#[cfg(test)]mod status_column_tests;+use columns::ColumnKind;use layout::calculate_responsive_layout;use model::{ListData, ListItem, gather_list_data};use render::{- format_ahead_behind_plain, format_ci_status_plain, format_diff_plain, format_header_line,- format_list_item_line,+ format_ci_status_plain, format_diff_plain, format_header_line, format_list_item_line,};use worktrunk::git::{GitError, Repository};-use worktrunk::styling::{ADDITION, DELETION, INFO_EMOJI, println};+use worktrunk::styling::{INFO_EMOJI, println};/// Helper to enrich common display fields shared between worktrees and branchesfn enrich_common_fields(@@ -25,27 +25,15 @@ fn enrich_common_fields(upstream: &model::UpstreamStatus,pr_status: &Option<ci_status::PrStatus>,) -> model::DisplayFields {- let commits_display = format_ahead_behind_plain(counts.ahead, counts.behind);+ let commits_display = format_diff_plain(ColumnKind::AheadBehind, counts.ahead, counts.behind);let (added, deleted) = branch_diff.diff;- let branch_diff_display = format_diff_plain(added, deleted);+ let branch_diff_display = format_diff_plain(ColumnKind::BranchDiff, added, deleted);let upstream_display = upstream.active()- .map(|(_, upstream_ahead, upstream_behind)| {- // Always show arrows for upstream (even ↑0 ↓0) to distinguish from no remote- format_ahead_behind_plain(upstream_ahead, upstream_behind).unwrap_or_else(|| {- let dim_deletion = DELETION.dimmed();- format!(- "{}↑{}{} {}↓{}{}",- ADDITION,- 0,- ADDITION.render_reset(),- dim_deletion,- 0,- dim_deletion.render_reset()- )- })+ .and_then(|(_, upstream_ahead, upstream_behind)| {+ format_diff_plain(ColumnKind::Upstream, upstream_ahead, upstream_behind)});let ci_status_display = pr_status.as_ref().map(format_ci_status_plain);@@ -75,7 +63,7 @@ fn enrich_with_display_fields(mut item: ListItem) -> ListItem {// Working tree specific fieldlet (added, deleted) = info.working_tree_diff;- info.working_diff_display = format_diff_plain(added, deleted);+ info.working_diff_display = format_diff_plain(ColumnKind::WorkingDiff, added, deleted);}ListItem::Branch(info) => {let mut display = enrich_common_fields(diff --git a/src/commands/list/render.rs b/src/commands/list/render.rsindex c729e214c..8aefd753f 100644--- a/src/commands/list/render.rs+++ b/src/commands/list/render.rs@@ -1,62 +1,67 @@use crate::display::{format_relative_time, shorten_path, truncate_at_word_boundary};use anstyle::{AnsiColor, Color, Style};use std::path::Path;-use worktrunk::styling::{ADDITION, CURRENT, DELETION, StyledLine, println};+use worktrunk::styling::{CURRENT, StyledLine, println};use super::ci_status::{CiStatus, PrStatus};use super::columns::{ColumnKind, DiffVariant};-use super::layout::{ColumnFormat, ColumnLayout, DiffColumnConfig, LayoutConfig};+use super::layout::{+ ColumnFormat, ColumnLayout, DiffColumnConfig, DiffDisplayConfig, LayoutConfig,+};use super::model::{AheadBehind, CommitDetails, ListItem, PositionMask, UpstreamStatus, WorktreeInfo,};-/// Format ahead/behind counts as plain text with ANSI colors (for json-pretty)-pub fn format_ahead_behind_plain(ahead: usize, behind: usize) -> Option<String> {- match (ahead, behind) {- (0, 0) => None,- (a, 0) => Some(format!("{}↑{}{}", ADDITION, a, ADDITION.render_reset())),- (0, b) => {- let dim_deletion = DELETION.dimmed();- Some(format!(- "{}↓{}{}",- dim_deletion,- b,- dim_deletion.render_reset()- ))- }- (a, b) => {- let dim_deletion = DELETION.dimmed();- Some(format!(- "{}↑{}{} {}↓{}{}",- ADDITION,- a,- ADDITION.render_reset(),- dim_deletion,- b,- dim_deletion.render_reset()- ))- }+fn format_plain_diff_from_config(+ positive: usize,+ negative: usize,+ config: DiffDisplayConfig,+) -> Option<String> {+ if !config.always_show_zeros && positive == 0 && negative == 0 {+ return None;+ }++ let (positive_symbol, negative_symbol) = match config.variant {+ DiffVariant::Signs => ("+", "-"),+ DiffVariant::Arrows => ("↑", "↓"),+ };++ let mut parts = Vec::with_capacity(2);++ if positive > 0 || config.always_show_zeros {+ parts.push(format!(+ "{}{}{}{}",+ config.positive_style,+ positive_symbol,+ positive,+ config.positive_style.render_reset()+ ));}-}-/// Format diff values as plain text with ANSI colors (for json-pretty)-pub fn format_diff_plain(added: usize, deleted: usize) -> Option<String> {- match (added, deleted) {- (0, 0) => None,- (a, 0) => Some(format!("{}+{}{}", ADDITION, a, ADDITION.render_reset())),- (0, d) => Some(format!("{}-{}{}", DELETION, d, DELETION.render_reset())),- (a, d) => Some(format!(- "{}+{}{} {}-{}{}",- ADDITION,- a,- ADDITION.render_reset(),- DELETION,- d,- DELETION.render_reset()- )),+ if negative > 0 || config.always_show_zeros {+ parts.push(format!(+ "{}{}{}{}",+ config.negative_style,+ negative_symbol,+ negative,+ config.negative_style.render_reset()+ ));+ }++ if parts.is_empty() {+ None+ } else {+ Some(parts.join(" "))}}+/// Format diff-style values as plain text with ANSI colors (for json-pretty)+pub fn format_diff_plain(kind: ColumnKind, positive: usize, negative: usize) -> Option<String> {+ let config = kind.diff_display_config()?;++ 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 {@@ -122,10 +127,10 @@ fn format_diff_like_column(negative: usize,config: DiffColumnConfig,) -> StyledLine {- let render_config = diff_render_config(config.variant);+ let render_config = diff_render_config(config.display.variant);let mut segment = StyledLine::new();- if positive == 0 && negative == 0 && !config.always_show_zeros {+ if positive == 0 && negative == 0 && !config.display.always_show_zeros {segment.push_raw(" ".repeat(config.total_width));return segment;}@@ -139,26 +144,26 @@ fn format_diff_like_column(segment.push_raw(" ".repeat(extra_padding));}- if positive > 0 || (positive == 0 && config.always_show_zeros) {+ if positive > 0 || (positive == 0 && config.display.always_show_zeros) {let value = format!("{}{}", render_config.positive_symbol, positive);let formatted = match render_config.align {ValueAlign::Right => format!("{:>width$}", value, width = positive_width),ValueAlign::Left => format!("{:<width$}", value, width = positive_width),};- segment.push_styled(formatted, config.positive_style);+ segment.push_styled(formatted, config.display.positive_style);} else {segment.push_raw(" ".repeat(positive_width));}segment.push_raw(" ");- if negative > 0 || (negative == 0 && config.always_show_zeros) {+ if negative > 0 || (negative == 0 && config.display.always_show_zeros) {let value = format!("{}{}", render_config.negative_symbol, negative);let formatted = match render_config.align {ValueAlign::Right => format!("{:>width$}", value, width = negative_width),ValueAlign::Left => format!("{:<width$}", value, width = negative_width),};- segment.push_styled(formatted, config.negative_style);+ segment.push_styled(formatted, config.display.negative_style);} else {segment.push_raw(" ".repeat(negative_width));}@@ -468,8 +473,8 @@ pub fn format_list_item_line(#[cfg(test)]mod tests {use super::*;- use crate::commands::list::layout::DiffDigits;- use worktrunk::styling::StyledLine;+ use crate::commands::list::layout::{DiffDigits, DiffDisplayConfig};+ use worktrunk::styling::{ADDITION, DELETION, StyledLine};#[test]fn test_format_diff_column_pads_to_total_width() {@@ -487,10 +492,12 @@ mod tests {DiffColumnConfig {digits,total_width: total,- variant: DiffVariant::Signs,- positive_style: ADDITION,- negative_style: DELETION,- always_show_zeros: false,+ display: DiffDisplayConfig {+ variant: DiffVariant::Signs,+ positive_style: ADDITION,+ negative_style: DELETION,+ always_show_zeros: false,+ },},);assert_eq!(@@ -511,10 +518,12 @@ mod tests {DiffColumnConfig {digits,total_width: total,- variant: DiffVariant::Signs,- positive_style: ADDITION,- negative_style: DELETION,- always_show_zeros: false,+ display: DiffDisplayConfig {+ variant: DiffVariant::Signs,+ positive_style: ADDITION,+ negative_style: DELETION,+ always_show_zeros: false,+ },},);assert_eq!(@@ -535,10 +544,12 @@ mod tests {DiffColumnConfig {digits,total_width: total,- variant: DiffVariant::Signs,- positive_style: ADDITION,- negative_style: DELETION,- always_show_zeros: false,+ display: DiffDisplayConfig {+ variant: DiffVariant::Signs,+ positive_style: ADDITION,+ negative_style: DELETION,+ always_show_zeros: false,+ },},);assert_eq!(@@ -559,10 +570,12 @@ mod tests {DiffColumnConfig {digits,total_width: total,- variant: DiffVariant::Signs,- positive_style: ADDITION,- negative_style: DELETION,- always_show_zeros: false,+ display: DiffDisplayConfig {+ variant: DiffVariant::Signs,+ positive_style: ADDITION,+ negative_style: DELETION,+ always_show_zeros: false,+ },},);assert_eq!(result.width(), total, "Empty diff should be 6 spaces");@@ -585,10 +598,12 @@ mod tests {DiffColumnConfig {digits,total_width: total,- variant: DiffVariant::Signs,- positive_style: ADDITION,- negative_style: DELETION,- always_show_zeros: false,+ display: DiffDisplayConfig {+ variant: DiffVariant::Signs,+ positive_style: ADDITION,+ negative_style: DELETION,+ always_show_zeros: false,+ },},);let rendered = result.render();@@ -731,10 +746,12 @@ mod tests {DiffColumnConfig {digits,total_width: total,- variant: DiffVariant::Arrows,- positive_style: ADDITION,- negative_style: dim_deletion,- always_show_zeros: false,+ display: DiffDisplayConfig {+ variant: DiffVariant::Arrows,+ positive_style: ADDITION,+ negative_style: dim_deletion,+ always_show_zeros: false,+ },},);assert_eq!(result.width(), total);@@ -760,10 +777,12 @@ mod tests {DiffColumnConfig {digits,total_width: total,- variant: DiffVariant::Arrows,- positive_style: ADDITION,- negative_style: dim_deletion,- always_show_zeros: false,+ display: DiffDisplayConfig {+ variant: DiffVariant::Arrows,+ positive_style: ADDITION,+ negative_style: dim_deletion,+ always_show_zeros: false,+ },},);assert_eq!(empty.width(), total);@@ -774,10 +793,12 @@ mod tests {DiffColumnConfig {digits,total_width: total,- variant: DiffVariant::Arrows,- positive_style: ADDITION,- negative_style: dim_deletion,- always_show_zeros: false,+ display: DiffDisplayConfig {+ variant: DiffVariant::Arrows,+ positive_style: ADDITION,+ negative_style: dim_deletion,+ always_show_zeros: false,+ },},);assert_eq!(behind_only.width(), total);@@ -803,10 +824,12 @@ mod tests {DiffColumnConfig {digits,total_width: total,- variant: DiffVariant::Arrows,- positive_style: ADDITION,- negative_style: dim_deletion,- always_show_zeros: false,+ display: DiffDisplayConfig {+ variant: DiffVariant::Arrows,+ positive_style: ADDITION,+ negative_style: dim_deletion,+ always_show_zeros: false,+ },},);assert_eq!(without.width(), total);@@ -822,10 +845,12 @@ mod tests {DiffColumnConfig {digits,total_width: total,- variant: DiffVariant::Arrows,- positive_style: ADDITION,- negative_style: dim_deletion,- always_show_zeros: true,+ display: DiffDisplayConfig {+ variant: DiffVariant::Arrows,+ positive_style: ADDITION,+ negative_style: dim_deletion,+ always_show_zeros: true,+ },},);assert_eq!(with.width(), total);