Skip to content

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

Refactor: Simplify ListItem data access and calculations

Maximilian Roos0bc7ccf939c6
diff --git a/src/commands/list/layout.rs b/src/commands/list/layout.rs
index b825a1a0d..ccde3ead6 100644
--- a/src/commands/list/layout.rs
+++ b/src/commands/list/layout.rs
@@ -75,50 +75,66 @@ pub fn calculate_column_widths(items: &[ListItem]) -> ColumnWidths {
let mut max_br_deleted_digits = 0;
for item in items {
+ let (commit, counts, branch_diff, upstream, worktree_info) = match item {
+ ListItem::Worktree(info) => (
+ &info.commit,
+ &info.counts,
+ info.branch_diff.diff,
+ &info.upstream,
+ Some(info),
+ ),
+ ListItem::Branch(info) => (
+ &info.commit,
+ &info.counts,
+ info.branch_diff.diff,
+ &info.upstream,
+ None,
+ ),
+ };
+
// Branch name
max_branch = max_branch.max(item.branch_name().width());
// Time
- let time_str = crate::display::format_relative_time(item.timestamp());
+ let time_str = crate::display::format_relative_time(commit.timestamp);
max_time = max_time.max(time_str.width());
// Message (truncate to 50 chars max)
- let msg_len = item.commit_message().chars().take(50).count();
+ let msg_len = commit.commit_message.chars().take(50).count();
max_message = max_message.max(msg_len);
// Ahead/behind (only for non-primary items)
- if !item.is_primary() && (item.ahead() > 0 || item.behind() > 0) {
- let ahead_behind_len = format!("↑{} ↓{}", item.ahead(), item.behind()).width();
+ if !item.is_primary() && (counts.ahead > 0 || counts.behind > 0) {
+ let ahead_behind_len = format!("↑{} ↓{}", counts.ahead, counts.behind).width();
max_ahead_behind = max_ahead_behind.max(ahead_behind_len);
}
// Working tree diff (worktrees only) - track digits separately
- if let Some((wt_added, wt_deleted)) = item.working_tree_diff()
- && (wt_added > 0 || wt_deleted > 0)
+ if let Some(info) = worktree_info
+ && (info.working_tree_diff.0 > 0 || info.working_tree_diff.1 > 0)
{
- max_wt_added_digits = max_wt_added_digits.max(wt_added.to_string().len());
- max_wt_deleted_digits = max_wt_deleted_digits.max(wt_deleted.to_string().len());
+ max_wt_added_digits =
+ max_wt_added_digits.max(info.working_tree_diff.0.to_string().len());
+ max_wt_deleted_digits =
+ max_wt_deleted_digits.max(info.working_tree_diff.1.to_string().len());
}
// Branch diff (only for non-primary items) - track digits separately
- if !item.is_primary() {
- let (br_added, br_deleted) = item.branch_diff();
- if br_added > 0 || br_deleted > 0 {
- max_br_added_digits = max_br_added_digits.max(br_added.to_string().len());
- max_br_deleted_digits = max_br_deleted_digits.max(br_deleted.to_string().len());
- }
+ if !item.is_primary() && (branch_diff.0 > 0 || branch_diff.1 > 0) {
+ max_br_added_digits = max_br_added_digits.max(branch_diff.0.to_string().len());
+ max_br_deleted_digits = max_br_deleted_digits.max(branch_diff.1.to_string().len());
}
// Upstream tracking
- if let Some((remote_name, upstream_ahead, upstream_behind)) = item.upstream_info() {
+ if let Some((remote_name, upstream_ahead, upstream_behind)) = upstream.active() {
let upstream_len =
format!("{} ↑{} ↓{}", remote_name, upstream_ahead, upstream_behind).width();
max_upstream = max_upstream.max(upstream_len);
}
// States (worktrees only)
- if let Some(worktree_info) = item.worktree_info() {
- let states = super::render::format_all_states(worktree_info);
+ if let Some(info) = worktree_info {
+ let states = super::render::format_all_states(info);
if !states.is_empty() {
max_states = max_states.max(states.width());
}
@@ -167,9 +183,9 @@ pub fn calculate_responsive_layout(items: &[ListItem]) -> LayoutConfig {
let terminal_width = get_terminal_width();
let paths: Vec<&Path> = items
.iter()
- .filter_map(|item| {
- item.worktree_info()
- .map(|info| info.worktree.path.as_path())
+ .filter_map(|item| match item {
+ ListItem::Worktree(info) => Some(info.worktree.path.as_path()),
+ ListItem::Branch(_) => None,
})
.collect();
let common_prefix = find_common_prefix(&paths);
@@ -178,41 +194,59 @@ pub fn calculate_responsive_layout(items: &[ListItem]) -> LayoutConfig {
let non_primary_count = items.iter().filter(|item| !item.is_primary()).count();
let ahead_behind_count = items
.iter()
- .filter(|item| !item.is_primary() && (item.ahead() > 0 || item.behind() > 0))
+ .filter(|item| {
+ !item.is_primary() && {
+ let counts = match item {
+ ListItem::Worktree(info) => &info.counts,
+ ListItem::Branch(info) => &info.counts,
+ };
+ counts.ahead > 0 || counts.behind > 0
+ }
+ })
.count();
let working_diff_count = items
.iter()
- .filter(|item| {
- item.working_tree_diff()
- .map(|(added, deleted)| added > 0 || deleted > 0)
- .unwrap_or(false)
+ .filter(|item| match item {
+ ListItem::Worktree(info) => {
+ info.working_tree_diff.0 > 0 || info.working_tree_diff.1 > 0
+ }
+ ListItem::Branch(_) => false,
})
.count();
let branch_diff_count = items
.iter()
.filter(|item| {
!item.is_primary() && {
- let (added, deleted) = item.branch_diff();
- added > 0 || deleted > 0
+ let diff = match item {
+ ListItem::Worktree(info) => info.branch_diff.diff,
+ ListItem::Branch(info) => info.branch_diff.diff,
+ };
+ diff.0 > 0 || diff.1 > 0
}
})
.count();
let upstream_count = items
.iter()
- .filter(|item| item.upstream_info().is_some())
+ .filter(|item| {
+ match item {
+ ListItem::Worktree(info) => info.upstream.active(),
+ ListItem::Branch(info) => info.upstream.active(),
+ }
+ .is_some()
+ })
.count();
let states_count = items
.iter()
.filter(|item| {
- item.worktree_info()
- .map(|i| {
- i.worktree_state.is_some()
+ matches!(
+ item,
+ ListItem::Worktree(i)
+ if i.worktree_state.is_some()
|| (i.worktree.detached && i.worktree.branch.is_some())
|| i.worktree.bare
|| i.worktree.locked.is_some()
|| i.worktree.prunable.is_some()
- })
- .unwrap_or(false)
+ )
})
.count();
@@ -231,7 +265,10 @@ pub fn calculate_responsive_layout(items: &[ListItem]) -> LayoutConfig {
// Calculate actual maximum path width (after common prefix removal)
let max_path_width = items
.iter()
- .filter_map(|item| item.worktree_info())
+ .filter_map(|item| match item {
+ ListItem::Worktree(info) => Some(info),
+ ListItem::Branch(_) => None,
+ })
.map(|info| {
use crate::display::shorten_path;
use unicode_width::UnicodeWidthStr;
@@ -357,7 +394,9 @@ mod tests {
#[test]
fn test_column_width_calculation_with_unicode() {
- use crate::commands::list::WorktreeInfo;
+ use crate::commands::list::{
+ AheadBehind, BranchDiffTotals, CommitDetails, UpstreamStatus, WorktreeInfo,
+ };
let info1 = WorktreeInfo {
worktree: worktrunk::git::Worktree {
@@ -369,16 +408,22 @@ mod tests {
locked: None,
prunable: None,
},
- timestamp: 0,
- commit_message: "Test".to_string(),
- ahead: 3,
- behind: 2,
+ commit: CommitDetails {
+ timestamp: 0,
+ commit_message: "Test".to_string(),
+ },
+ counts: AheadBehind {
+ ahead: 3,
+ behind: 2,
+ },
working_tree_diff: (100, 50),
- branch_diff: (200, 30),
+ branch_diff: BranchDiffTotals { diff: (200, 30) },
is_primary: false,
- upstream_remote: Some("origin".to_string()),
- upstream_ahead: 4,
- upstream_behind: 0,
+ upstream: UpstreamStatus {
+ remote: Some("origin".to_string()),
+ ahead: 4,
+ behind: 0,
+ },
worktree_state: None,
};
diff --git a/src/commands/list/mod.rs b/src/commands/list/mod.rs
index 6088567cb..66a951508 100644
--- a/src/commands/list/mod.rs
+++ b/src/commands/list/mod.rs
@@ -14,16 +14,16 @@ use render::{format_header_line, format_list_item_line};
#[derive(serde::Serialize)]
pub struct WorktreeInfo {
pub worktree: worktrunk::git::Worktree,
- pub timestamp: i64,
- pub commit_message: String,
- pub ahead: usize,
- pub behind: usize,
+ #[serde(flatten)]
+ pub commit: CommitDetails,
+ #[serde(flatten)]
+ pub counts: AheadBehind,
pub working_tree_diff: (usize, usize),
- pub branch_diff: (usize, usize),
+ #[serde(flatten)]
+ pub branch_diff: BranchDiffTotals,
pub is_primary: bool,
- pub upstream_remote: Option<String>,
- pub upstream_ahead: usize,
- pub upstream_behind: usize,
+ #[serde(flatten)]
+ pub upstream: UpstreamStatus,
pub worktree_state: Option<String>,
}
@@ -31,119 +31,132 @@ pub struct WorktreeInfo {
pub struct BranchInfo {
pub name: String,
pub head: String,
+ #[serde(flatten)]
+ pub commit: CommitDetails,
+ #[serde(flatten)]
+ pub counts: AheadBehind,
+ #[serde(flatten)]
+ pub branch_diff: BranchDiffTotals,
+ #[serde(flatten)]
+ pub upstream: UpstreamStatus,
+}
+
+#[derive(serde::Serialize, Clone)]
+pub(crate) struct CommitDetails {
pub timestamp: i64,
pub commit_message: String,
+}
+
+impl CommitDetails {
+ fn gather(repo: &Repository, head: &str) -> Result<Self, GitError> {
+ Ok(Self {
+ timestamp: repo.commit_timestamp(head)?,
+ commit_message: repo.commit_message(head)?,
+ })
+ }
+}
+
+#[derive(serde::Serialize, Default, Clone)]
+pub(crate) struct AheadBehind {
pub ahead: usize,
pub behind: usize,
- pub branch_diff: (usize, usize),
- pub upstream_remote: Option<String>,
- pub upstream_ahead: usize,
- pub upstream_behind: usize,
}
-/// Unified type for displaying worktrees and branches in the same table
-#[derive(serde::Serialize)]
-#[serde(tag = "type", rename_all = "lowercase")]
-pub enum ListItem {
- Worktree(WorktreeInfo),
- Branch(BranchInfo),
-}
+impl AheadBehind {
+ fn compute(repo: &Repository, base: Option<&str>, head: &str) -> Result<Self, GitError> {
+ let Some(base) = base else {
+ return Ok(Self::default());
+ };
-impl ListItem {
- pub fn timestamp(&self) -> i64 {
- match self {
- ListItem::Worktree(wt) => wt.timestamp,
- ListItem::Branch(br) => br.timestamp,
- }
+ let (ahead, behind) = repo.ahead_behind(base, head)?;
+ Ok(Self { ahead, behind })
}
+}
- pub fn worktree_info(&self) -> Option<&WorktreeInfo> {
- match self {
- ListItem::Worktree(wt) => Some(wt),
- ListItem::Branch(_) => None,
- }
- }
+#[derive(serde::Serialize, Default, Clone)]
+pub(crate) struct BranchDiffTotals {
+ #[serde(rename = "branch_diff")]
+ pub diff: (usize, usize),
+}
- pub fn branch_name(&self) -> &str {
- match self {
- ListItem::Worktree(wt) => wt.worktree.branch.as_deref().unwrap_or("(detached)"),
- ListItem::Branch(br) => &br.name,
- }
- }
+impl BranchDiffTotals {
+ fn compute(repo: &Repository, base: Option<&str>, head: &str) -> Result<Self, GitError> {
+ let Some(base) = base else {
+ return Ok(Self::default());
+ };
- pub fn commit_head(&self) -> &str {
- match self {
- ListItem::Worktree(wt) => &wt.worktree.head,
- ListItem::Branch(br) => &br.head,
- }
+ let diff = repo.branch_diff_stats(base, head)?;
+ Ok(Self { diff })
}
+}
- pub fn commit_message(&self) -> &str {
- match self {
- ListItem::Worktree(wt) => &wt.commit_message,
- ListItem::Branch(br) => &br.commit_message,
- }
- }
+#[derive(serde::Serialize, Default, Clone)]
+pub(crate) struct UpstreamStatus {
+ #[serde(rename = "upstream_remote")]
+ remote: Option<String>,
+ #[serde(rename = "upstream_ahead")]
+ ahead: usize,
+ #[serde(rename = "upstream_behind")]
+ behind: usize,
+}
- pub fn ahead(&self) -> usize {
- match self {
- ListItem::Worktree(wt) => wt.ahead,
- ListItem::Branch(br) => br.ahead,
- }
- }
+impl UpstreamStatus {
+ fn calculate(repo: &Repository, branch: Option<&str>, head: &str) -> Result<Self, GitError> {
+ let Some(branch) = branch else {
+ return Ok(Self::default());
+ };
- pub fn behind(&self) -> usize {
- match self {
- ListItem::Worktree(wt) => wt.behind,
- ListItem::Branch(br) => br.behind,
+ match repo.upstream_branch(branch) {
+ Ok(Some(upstream_branch)) => {
+ let remote = upstream_branch
+ .split_once('/')
+ .map(|(remote, _)| remote)
+ .unwrap_or("origin")
+ .to_string();
+ let (ahead, behind) = repo.ahead_behind(&upstream_branch, head)?;
+ Ok(Self {
+ remote: Some(remote),
+ ahead,
+ behind,
+ })
+ }
+ _ => Ok(Self::default()),
}
}
- pub fn is_primary(&self) -> bool {
- match self {
- ListItem::Worktree(wt) => wt.is_primary,
- ListItem::Branch(_) => false,
+ fn active(&self) -> Option<(&str, usize, usize)> {
+ if self.ahead == 0 && self.behind == 0 {
+ None
+ } else {
+ Some((
+ self.remote.as_deref().unwrap_or("origin"),
+ self.ahead,
+ self.behind,
+ ))
}
}
+}
- pub fn branch_diff(&self) -> (usize, usize) {
- match self {
- ListItem::Worktree(wt) => wt.branch_diff,
- ListItem::Branch(br) => br.branch_diff,
- }
- }
+/// Unified type for displaying worktrees and branches in the same table
+#[derive(serde::Serialize)]
+#[serde(tag = "type", rename_all = "lowercase")]
+pub enum ListItem {
+ Worktree(WorktreeInfo),
+ Branch(BranchInfo),
+}
- pub fn working_tree_diff(&self) -> Option<(usize, usize)> {
+impl ListItem {
+ pub fn branch_name(&self) -> &str {
match self {
- ListItem::Worktree(wt) => Some(wt.working_tree_diff),
- ListItem::Branch(_) => None,
+ ListItem::Worktree(wt) => wt.worktree.branch.as_deref().unwrap_or("(detached)"),
+ ListItem::Branch(br) => &br.name,
}
}
- pub fn upstream_info(&self) -> Option<(&str, usize, usize)> {
+ pub fn is_primary(&self) -> bool {
match self {
- ListItem::Worktree(wt) => {
- if wt.upstream_ahead > 0 || wt.upstream_behind > 0 {
- Some((
- wt.upstream_remote.as_deref().unwrap_or("origin"),
- wt.upstream_ahead,
- wt.upstream_behind,
- ))
- } else {
- None
- }
- }
- ListItem::Branch(br) => {
- if br.upstream_ahead > 0 || br.upstream_behind > 0 {
- Some((
- br.upstream_remote.as_deref().unwrap_or("origin"),
- br.upstream_ahead,
- br.upstream_behind,
- ))
- } else {
- None
- }
- }
+ ListItem::Worktree(wt) => wt.is_primary,
+ ListItem::Branch(_) => false,
}
}
}
@@ -158,52 +171,18 @@ impl BranchInfo {
// Get the commit SHA for this branch
let head = repo.run_command(&["rev-parse", branch])?.trim().to_string();
- // Get commit timestamp
- let timestamp = repo.commit_timestamp(&head)?;
-
- // Get commit message
- let commit_message = repo.commit_message(&head)?;
-
- // Calculate ahead/behind relative to primary branch
- let (ahead, behind) = if let Some(pb) = primary_branch {
- repo.ahead_behind(pb, &head)?
- } else {
- (0, 0)
- };
-
- // Get branch diff stats (line diff relative to primary)
- let branch_diff = if let Some(pb) = primary_branch {
- repo.branch_diff_stats(pb, &head)?
- } else {
- (0, 0)
- };
-
- // Get upstream tracking info
- let (upstream_remote, upstream_ahead, upstream_behind) =
- match repo.upstream_branch(branch).ok().flatten() {
- Some(upstream_branch) => {
- let remote = upstream_branch
- .split_once('/')
- .map(|(remote, _)| remote)
- .unwrap_or("origin")
- .to_string();
- let (ahead, behind) = repo.ahead_behind(&upstream_branch, &head)?;
- (Some(remote), ahead, behind)
- }
- None => (None, 0, 0),
- };
+ let commit = CommitDetails::gather(repo, &head)?;
+ let counts = AheadBehind::compute(repo, primary_branch, &head)?;
+ let branch_diff = BranchDiffTotals::compute(repo, primary_branch, &head)?;
+ let upstream = UpstreamStatus::calculate(repo, Some(branch), &head)?;
Ok(BranchInfo {
name: branch.to_string(),
head,
- timestamp,
- commit_message,
- ahead,
- behind,
+ commit,
+ counts,
branch_diff,
- upstream_remote,
- upstream_ahead,
- upstream_behind,
+ upstream,
})
}
}
@@ -217,65 +196,33 @@ impl WorktreeInfo {
let wt_repo = Repository::at(&wt.path);
let is_primary = wt.path == primary.path;
- // Get commit timestamp
- let timestamp = wt_repo.commit_timestamp(&wt.head)?;
+ let commit = CommitDetails::gather(&wt_repo, &wt.head)?;
- // Get commit message
- let commit_message = wt_repo.commit_message(&wt.head)?;
-
- // Calculate ahead/behind relative to primary branch (only if primary has a branch)
- let (ahead, behind) = if is_primary {
- (0, 0)
- } else if let Some(pb) = primary.branch.as_deref() {
- wt_repo.ahead_behind(pb, &wt.head)?
+ let counts = if is_primary {
+ AheadBehind::default()
} else {
- (0, 0)
+ AheadBehind::compute(&wt_repo, primary.branch.as_deref(), &wt.head)?
};
- let working_tree_diff = wt_repo.working_tree_diff_stats()?;
- // Get branch diff stats (downstream of primary, only if primary has a branch)
+ let working_tree_diff = wt_repo.working_tree_diff_stats()?;
let branch_diff = if is_primary {
- (0, 0)
- } else if let Some(pb) = primary.branch.as_deref() {
- wt_repo.branch_diff_stats(pb, &wt.head)?
+ BranchDiffTotals::default()
} else {
- (0, 0)
- };
-
- // Get upstream tracking info
- let (upstream_remote, upstream_ahead, upstream_behind) = match wt
- .branch
- .as_ref()
- .and_then(|b| wt_repo.upstream_branch(b).ok().flatten())
- {
- Some(upstream_branch) => {
- // Extract remote name from "origin/main" -> "origin"
- let remote = upstream_branch
- .split_once('/')
- .map(|(remote, _)| remote)
- .unwrap_or("origin")
- .to_string();
- let (ahead, behind) = wt_repo.ahead_behind(&upstream_branch, &wt.head)?;
- (Some(remote), ahead, behind)
- }
- None => (None, 0, 0),
+ BranchDiffTotals::compute(&wt_repo, primary.branch.as_deref(), &wt.head)?
};
+ let upstream = UpstreamStatus::calculate(&wt_repo, wt.branch.as_deref(), &wt.head)?;
// Get worktree state (merge/rebase/etc)
let worktree_state = wt_repo.worktree_state()?;
Ok(WorktreeInfo {
worktree: wt.clone(),
- timestamp,
- commit_message,
- ahead,
- behind,
+ commit,
+ counts,
working_tree_diff,
branch_diff,
is_primary,
- upstream_remote,
- upstream_ahead,
- upstream_behind,
+ upstream,
worktree_state,
})
}
@@ -333,7 +280,13 @@ pub fn handle_list(format: crate::OutputFormat, show_branches: bool) -> Result<(
}
// Sort by most recent commit (descending)
- items.sort_by_key(|b| std::cmp::Reverse(b.timestamp()));
+ items.sort_by_key(|item| {
+ let timestamp = match item {
+ ListItem::Worktree(info) => info.commit.timestamp,
+ ListItem::Branch(info) => info.commit.timestamp,
+ };
+ std::cmp::Reverse(timestamp)
+ });
match format {
crate::OutputFormat::Json => {
diff --git a/src/commands/list/render.rs b/src/commands/list/render.rs
index 64c44d0c7..f8c9d5bd0 100644
--- a/src/commands/list/render.rs
+++ b/src/commands/list/render.rs
@@ -122,14 +122,32 @@ fn format_item_line(
) {
let widths = &layout.widths;
- let short_head = &item.commit_head()[..8.min(item.commit_head().len())];
+ let (head, commit, counts, branch_diff, upstream, worktree_info) = match item {
+ ListItem::Worktree(info) => (
+ &info.worktree.head,
+ &info.commit,
+ &info.counts,
+ info.branch_diff.diff,
+ &info.upstream,
+ Some(info),
+ ),
+ ListItem::Branch(info) => (
+ &info.head,
+ &info.commit,
+ &info.counts,
+ info.branch_diff.diff,
+ &info.upstream,
+ None,
+ ),
+ };
+ let short_head = &head[..8.min(head.len())];
// Determine styling (worktree-specific)
- let text_style = item.worktree_info().and_then(|info| {
+ let text_style = worktree_info.and_then(|info| {
let is_current = current_worktree_path
.map(|p| p == &info.worktree.path)
.unwrap_or(false);
- match (is_current, item.is_primary()) {
+ match (is_current, info.is_primary) {
(true, _) => Some(CURRENT),
(_, true) => Some(Style::new().fg_color(Some(Color::Ansi(AnsiColor::Cyan)))),
_ => None,
@@ -152,7 +170,7 @@ fn format_item_line(
if widths.time > 0 {
let time_str = format!(
"{:width$}",
- format_relative_time(item.timestamp()),
+ format_relative_time(commit.timestamp),
width = widths.time
);
line.push_styled(time_str, Style::new().dimmed());
@@ -161,32 +179,37 @@ fn format_item_line(
// Ahead/behind (commits difference)
if widths.ahead_behind > 0 {
- if !item.is_primary() && (item.ahead() > 0 || item.behind() > 0) {
- let ahead_behind_text = format!(
- "{:width$}",
- format!("↑{} ↓{}", item.ahead(), item.behind()),
- width = widths.ahead_behind
- );
- line.push_styled(
- ahead_behind_text,
- Style::new().fg_color(Some(Color::Ansi(AnsiColor::Yellow))),
- );
+ if !item.is_primary() {
+ if counts.ahead > 0 || counts.behind > 0 {
+ let ahead_behind_text = format!(
+ "{:width$}",
+ format!("↑{} ↓{}", counts.ahead, counts.behind),
+ width = widths.ahead_behind
+ );
+ line.push_styled(
+ ahead_behind_text,
+ Style::new().fg_color(Some(Color::Ansi(AnsiColor::Yellow))),
+ );
+ line.push_raw(" ");
+ } else {
+ line.push_raw(" ".repeat(widths.ahead_behind));
+ line.push_raw(" ");
+ }
} else {
line.push_raw(" ".repeat(widths.ahead_behind));
+ line.push_raw(" ");
}
- line.push_raw(" ");
}
// Branch diff (line diff in commits)
if widths.branch_diff.total > 0 {
if !item.is_primary() {
- let (br_added, br_deleted) = item.branch_diff();
- if br_added > 0 || br_deleted > 0 {
+ if branch_diff.0 > 0 || branch_diff.1 > 0 {
// Right-align numbers within their fields: "+{num:width$} -{num:width$}"
let formatted = format!(
"+{:width_add$} -{:width_del$}",
- br_added,
- br_deleted,
+ branch_diff.0,
+ branch_diff.1,
width_add = widths.branch_diff.added_digits,
width_del = widths.branch_diff.deleted_digits
);
@@ -209,7 +232,8 @@ fn format_item_line(
// Working tree diff (worktrees only)
if widths.working_diff.total > 0 {
- if let Some((wt_added, wt_deleted)) = item.working_tree_diff() {
+ if let ListItem::Worktree(info) = item {
+ let (wt_added, wt_deleted) = info.working_tree_diff;
if wt_added > 0 || wt_deleted > 0 {
// Right-align numbers within their fields: "+{num:width$} -{num:width$}"
let formatted = format!(
@@ -238,7 +262,7 @@ fn format_item_line(
// Upstream tracking
if widths.upstream > 0 {
- if let Some((remote_name, upstream_ahead, upstream_behind)) = item.upstream_info() {
+ if let Some((remote_name, upstream_ahead, upstream_behind)) = upstream.active() {
let mut upstream_segment = StyledLine::new();
upstream_segment.push_styled(remote_name, Style::new().dimmed());
upstream_segment.push_raw(" ");
@@ -267,7 +291,7 @@ fn format_item_line(
if widths.message > 0 {
let msg = format!(
"{:width$}",
- truncate_at_word_boundary(item.commit_message(), layout.max_message_len),
+ truncate_at_word_boundary(&commit.commit_message, layout.max_message_len),
width = widths.message
);
line.push_styled(msg, Style::new().dimmed());
@@ -276,8 +300,8 @@ fn format_item_line(
// States (worktrees only)
if widths.states > 0 {
- if let Some(worktree_info) = item.worktree_info() {
- let states = format_all_states(worktree_info);
+ if let Some(info) = worktree_info {
+ let states = format_all_states(info);
if !states.is_empty() {
let states_text = format!("{:width$}", states, width = widths.states);
line.push_raw(states_text);
@@ -291,8 +315,8 @@ fn format_item_line(
}
// Path (worktrees only)
- if let Some(worktree_info) = item.worktree_info() {
- let path_str = shorten_path(&worktree_info.worktree.path, &layout.common_prefix);
+ if let Some(info) = worktree_info {
+ let path_str = shorten_path(&info.worktree.path, &layout.common_prefix);
if let Some(style) = text_style {
line.push_styled(path_str, style);
} else {
@@ -305,8 +329,10 @@ fn format_item_line(
#[cfg(test)]
mod tests {
+ use super::super::{
+ AheadBehind, BranchDiffTotals, CommitDetails, UpstreamStatus, WorktreeInfo,
+ };
use super::*;
- use crate::commands::list::WorktreeInfo;
use crate::commands::list::layout::{ColumnWidths, LayoutConfig};
use crate::display::shorten_path;
use std::path::PathBuf;
@@ -325,16 +351,22 @@ mod tests {
locked: Some("test lck".to_string()), // "(locked: test lck)" = 18 chars
prunable: None,
},
- timestamp: 0,
- commit_message: "Test message".to_string(),
- ahead: 3,
- behind: 2,
+ commit: CommitDetails {
+ timestamp: 0,
+ commit_message: "Test message".to_string(),
+ },
+ counts: AheadBehind {
+ ahead: 3,
+ behind: 2,
+ },
working_tree_diff: (100, 50),
- branch_diff: (200, 30),
+ branch_diff: BranchDiffTotals { diff: (200, 30) },
is_primary: false,
- upstream_remote: Some("origin".to_string()),
- upstream_ahead: 4,
- upstream_behind: 0,
+ upstream: UpstreamStatus {
+ remote: Some("origin".to_string()),
+ ahead: 4,
+ behind: 0,
+ },
worktree_state: None,
};