Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Centralize diff column style configuration
Maximilian Roos04094fcc47cc
diff --git a/src/commands/list/layout.rs b/src/commands/list/layout.rsindex f28923b7e..519908ff0 100644--- a/src/commands/list/layout.rs+++ b/src/commands/list/layout.rs@@ -114,8 +114,10 @@//! - `try_allocate()`: Attempts to allocate space, returns 0 if insufficientuse crate::display::{find_common_prefix, get_terminal_width};+use anstyle::Style;use std::path::{Path, PathBuf};use unicode_width::UnicodeWidthStr;+use worktrunk::styling::{ADDITION, DELETION};use super::{columns::{COLUMN_SPECS, ColumnKind, ColumnSpec, DiffVariant},@@ -285,10 +287,17 @@ impl From<DiffWidths> for DiffDigits {#[derive(Clone, Copy, Debug)]pub enum ColumnFormat {Text,- Diff {- digits: DiffDigits,- variant: DiffVariant,- },+ Diff(DiffColumnConfig),+}++#[derive(Clone, Copy, Debug)]+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,}#[derive(Clone, Debug)]@@ -326,17 +335,58 @@ impl ColumnIdeal {}}- fn diff(widths: DiffWidths, variant: DiffVariant) -> Option<Self> {+ fn diff(widths: DiffWidths, variant: DiffVariant, kind: ColumnKind) -> Option<Self> {if widths.total == 0 {- None- } else {- Some(Self {- width: widths.total,- format: ColumnFormat::Diff {- digits: widths.into(),- variant,- },- })+ return None;+ }++ let styles = diff_styles_for_kind(kind);++ 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,+ }),+ })+ }+}++#[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,+ }}}}@@ -369,10 +419,24 @@ 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::AheadBehind => ColumnIdeal::diff(widths.ahead_behind, DiffVariant::Arrows),- ColumnKind::BranchDiff => ColumnIdeal::diff(widths.branch_diff, DiffVariant::Signs),- ColumnKind::Upstream => ColumnIdeal::diff(widths.upstream, DiffVariant::Arrows),+ 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)+ }}}diff --git a/src/commands/list/render.rs b/src/commands/list/render.rsindex f43f7ea29..c56e8b13f 100644--- a/src/commands/list/render.rs+++ b/src/commands/list/render.rs@@ -5,7 +5,7 @@ use worktrunk::styling::{ADDITION, CURRENT, DELETION, StyledLine, println};use super::ci_status::{CiStatus, PrStatus};use super::columns::{ColumnKind, DiffVariant};-use super::layout::{ColumnFormat, ColumnLayout, DiffDigits, LayoutConfig};+use super::layout::{ColumnFormat, ColumnLayout, DiffColumnConfig, LayoutConfig};use super::model::{AheadBehind, CommitDetails, ListItem, PositionMask, UpstreamStatus, WorktreeInfo,};@@ -117,15 +117,6 @@ fn diff_render_config(variant: DiffVariant) -> DiffRenderConfig {}}-struct DiffColumnConfig {- digits: DiffDigits,- total_width: usize,- variant: DiffVariant,- positive_style: Style,- negative_style: Style,- always_show_zeros: bool,-}-fn format_diff_like_column(positive: usize,negative: usize,@@ -364,7 +355,7 @@ fn render_list_cell(else {return StyledLine::new();};- render_diff_cell(column, added, deleted, ADDITION, DELETION, false)+ render_diff_cell(column, added, deleted)}ColumnKind::AheadBehind => {if ctx.item.is_primary() {@@ -375,21 +366,13 @@ fn render_list_cell(if ahead == 0 && behind == 0 {return StyledLine::new();}- let dim_deletion = DELETION.dimmed();- render_diff_cell(column, ahead, behind, ADDITION, dim_deletion, false)+ render_diff_cell(column, ahead, behind)}ColumnKind::BranchDiff => {if ctx.item.is_primary() {return StyledLine::new();}- render_diff_cell(- column,- ctx.branch_diff.0,- ctx.branch_diff.1,- ADDITION,- DELETION,- false,- )+ render_diff_cell(column, ctx.branch_diff.0, ctx.branch_diff.1)}ColumnKind::Path => {let Some(info) = ctx.worktree_info else {@@ -408,8 +391,7 @@ fn render_list_cell(let Some((_, ahead, behind)) = ctx.upstream.active() else {return StyledLine::new();};- let dim_deletion = DELETION.dimmed();- render_diff_cell(column, ahead, behind, ADDITION, dim_deletion, true)+ render_diff_cell(column, ahead, behind)}ColumnKind::Time => {let mut cell = StyledLine::new();@@ -437,30 +419,14 @@ fn render_list_cell(}}-fn render_diff_cell(- column: &ColumnLayout,- positive: usize,- negative: usize,- positive_style: Style,- negative_style: Style,- always_show_zeros: bool,-) -> StyledLine {- let ColumnFormat::Diff { digits, variant } = column.format else {+fn render_diff_cell(column: &ColumnLayout, positive: usize, negative: usize) -> StyledLine {+ let ColumnFormat::Diff(config) = column.format else {return StyledLine::new();};- format_diff_like_column(- positive,- negative,- DiffColumnConfig {- digits,- total_width: column.width,- variant,- positive_style,- negative_style,- always_show_zeros,- },- )+ debug_assert_eq!(config.total_width, column.width);++ format_diff_like_column(positive, negative, config)}/// Render a list item (worktree or branch) as a formatted line@@ -484,6 +450,7 @@ pub fn format_list_item_line(#[cfg(test)]mod tests {use super::*;+ use crate::commands::list::layout::DiffDigits;use worktrunk::styling::StyledLine;#[test]