Skip to content

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

Move CI status and diff formatting to methods

Maximilian Roos9ee3f923223e
diff --git a/src/commands/list/ci_status.rs b/src/commands/list/ci_status.rs
index c82823192..8df5d7793 100644
--- a/src/commands/list/ci_status.rs
+++ b/src/commands/list/ci_status.rs
@@ -178,7 +178,7 @@ impl PrStatus {
let ci_status = if pr_info.merge_state_status.as_deref() == Some("DIRTY") {
CiStatus::Conflicts
} else {
- Self::analyze_github_checks(&pr_info.status_check_rollup)
+ pr_info.ci_status()
};
let is_stale = pr_info
@@ -193,38 +193,6 @@ impl PrStatus {
})
}
- fn analyze_github_checks(checks: &Option<Vec<GitHubCheck>>) -> CiStatus {
- let Some(checks) = checks else {
- return CiStatus::NoCI;
- };
-
- if checks.is_empty() {
- return CiStatus::NoCI;
- }
-
- let has_pending = checks.iter().any(|c| {
- matches!(
- c.status.as_deref(),
- Some("IN_PROGRESS" | "QUEUED" | "PENDING" | "EXPECTED")
- )
- });
-
- let has_failure = checks.iter().any(|c| {
- matches!(
- c.conclusion.as_deref(),
- Some("FAILURE" | "ERROR" | "CANCELLED")
- )
- });
-
- if has_pending {
- CiStatus::Running
- } else if has_failure {
- CiStatus::Failed
- } else {
- CiStatus::Passed
- }
- }
-
fn detect_gitlab(branch: &str, local_head: &str) -> Option<Self> {
// Check if glab is available
if !Command::new("glab")
@@ -263,7 +231,7 @@ impl PrStatus {
} else if mr_info.detailed_merge_status.as_deref() == Some("ci_must_pass") {
CiStatus::Failed
} else {
- Self::analyze_gitlab_pipeline(mr_info.pipeline_status())
+ mr_info.ci_status()
};
let is_stale = mr_info.sha != local_head;
@@ -274,23 +242,6 @@ impl PrStatus {
})
}
- fn analyze_gitlab_pipeline(pipeline_status: Option<&String>) -> CiStatus {
- match pipeline_status.map(|s| s.as_str()) {
- Some(
- "running"
- | "pending"
- | "preparing"
- | "waiting_for_resource"
- | "created"
- | "scheduled",
- ) => CiStatus::Running,
- Some("failed" | "canceled") => CiStatus::Failed,
- Some("success") => CiStatus::Passed,
- Some("skipped" | "manual") | None => CiStatus::NoCI,
- _ => CiStatus::NoCI,
- }
- }
-
fn detect_github_workflow(
branch: &str,
_local_head: &str,
@@ -346,7 +297,7 @@ impl PrStatus {
let run = runs.first()?;
// Analyze workflow run status
- let ci_status = Self::analyze_github_workflow_run(run);
+ let ci_status = run.ci_status();
// Workflow runs don't have staleness concept (no PR to compare against)
Some(PrStatus {
@@ -355,19 +306,6 @@ impl PrStatus {
})
}
- fn analyze_github_workflow_run(run: &GitHubWorkflowRun) -> CiStatus {
- match run.status.as_deref() {
- Some("in_progress" | "queued" | "pending" | "waiting") => CiStatus::Running,
- Some("completed") => match run.conclusion.as_deref() {
- Some("success") => CiStatus::Passed,
- Some("failure" | "cancelled" | "timed_out" | "action_required") => CiStatus::Failed,
- Some("skipped" | "neutral") | None => CiStatus::NoCI,
- _ => CiStatus::NoCI,
- },
- _ => CiStatus::NoCI,
- }
- }
-
fn detect_gitlab_pipeline(branch: &str, _local_head: &str) -> Option<Self> {
// Check if glab is available
if !Command::new("glab")
@@ -397,9 +335,11 @@ impl PrStatus {
// Extract status from format like "• (running) #12345"
let status_start = first_line.find('(')?;
let status_end = first_line.find(')')?;
- let status = &first_line[status_start + 1..status_end];
-
- let ci_status = Self::analyze_gitlab_pipeline(Some(&status.to_string()));
+ let status = first_line[status_start + 1..status_end].to_string();
+ let ci_status = GitLabPipeline {
+ status: Some(status),
+ }
+ .ci_status();
Some(PrStatus {
ci_status,
@@ -431,6 +371,55 @@ struct GitHubWorkflowRun {
conclusion: Option<String>,
}
+impl GitHubPrInfo {
+ fn ci_status(&self) -> CiStatus {
+ let Some(checks) = &self.status_check_rollup else {
+ return CiStatus::NoCI;
+ };
+
+ if checks.is_empty() {
+ return CiStatus::NoCI;
+ }
+
+ let has_pending = checks.iter().any(|c| {
+ matches!(
+ c.status.as_deref(),
+ Some("IN_PROGRESS" | "QUEUED" | "PENDING" | "EXPECTED")
+ )
+ });
+
+ let has_failure = checks.iter().any(|c| {
+ matches!(
+ c.conclusion.as_deref(),
+ Some("FAILURE" | "ERROR" | "CANCELLED")
+ )
+ });
+
+ if has_pending {
+ CiStatus::Running
+ } else if has_failure {
+ CiStatus::Failed
+ } else {
+ CiStatus::Passed
+ }
+ }
+}
+
+impl GitHubWorkflowRun {
+ fn ci_status(&self) -> CiStatus {
+ match self.status.as_deref() {
+ Some("in_progress" | "queued" | "pending" | "waiting") => CiStatus::Running,
+ Some("completed") => match self.conclusion.as_deref() {
+ Some("success") => CiStatus::Passed,
+ Some("failure" | "cancelled" | "timed_out" | "action_required") => CiStatus::Failed,
+ Some("skipped" | "neutral") | None => CiStatus::NoCI,
+ _ => CiStatus::NoCI,
+ },
+ _ => CiStatus::NoCI,
+ }
+ }
+}
+
#[derive(Debug, Deserialize)]
struct GitLabMrInfo {
state: String,
@@ -442,11 +431,12 @@ struct GitLabMrInfo {
}
impl GitLabMrInfo {
- fn pipeline_status(&self) -> Option<&String> {
+ fn ci_status(&self) -> CiStatus {
self.head_pipeline
.as_ref()
.or(self.pipeline.as_ref())
- .and_then(|p| p.status.as_ref())
+ .map(GitLabPipeline::ci_status)
+ .unwrap_or(CiStatus::NoCI)
}
}
@@ -454,3 +444,22 @@ impl GitLabMrInfo {
struct GitLabPipeline {
status: Option<String>,
}
+
+impl GitLabPipeline {
+ fn ci_status(&self) -> CiStatus {
+ match self.status.as_deref() {
+ Some(
+ "running"
+ | "pending"
+ | "preparing"
+ | "waiting_for_resource"
+ | "created"
+ | "scheduled",
+ ) => CiStatus::Running,
+ Some("failed" | "canceled" | "manual") => CiStatus::Failed,
+ Some("success") => CiStatus::Passed,
+ Some("skipped") | None => CiStatus::NoCI,
+ _ => CiStatus::NoCI,
+ }
+ }
+}
diff --git a/src/commands/list/mod.rs b/src/commands/list/mod.rs
index 5aec0047c..e36f0f173 100644
--- a/src/commands/list/mod.rs
+++ b/src/commands/list/mod.rs
@@ -13,7 +13,6 @@ use super::repository_ext::RepositoryCliExt;
use columns::ColumnKind;
use layout::calculate_responsive_layout;
use model::{ListData, ListItem};
-use render::format_diff_plain;
use worktrunk::git::{GitError, Repository};
use worktrunk::styling::{INFO_EMOJI, println};
@@ -24,15 +23,15 @@ fn enrich_common_fields(
upstream: &model::UpstreamStatus,
pr_status: &Option<ci_status::PrStatus>,
) -> model::DisplayFields {
- let commits_display = format_diff_plain(ColumnKind::AheadBehind, counts.ahead, counts.behind);
+ let commits_display = ColumnKind::AheadBehind.format_diff_plain(counts.ahead, counts.behind);
let (added, deleted) = branch_diff.diff;
- let branch_diff_display = format_diff_plain(ColumnKind::BranchDiff, added, deleted);
+ let branch_diff_display = ColumnKind::BranchDiff.format_diff_plain(added, deleted);
let upstream_display = upstream
.active()
.and_then(|(_, upstream_ahead, upstream_behind)| {
- format_diff_plain(ColumnKind::Upstream, upstream_ahead, upstream_behind)
+ ColumnKind::Upstream.format_diff_plain(upstream_ahead, upstream_behind)
});
let ci_status_display = pr_status.as_ref().map(ci_status::PrStatus::format_plain);
@@ -46,39 +45,6 @@ fn enrich_common_fields(
}
}
-/// Enrich a ListItem with display fields for json-pretty format
-fn enrich_with_display_fields(mut item: ListItem) -> ListItem {
- match &mut item {
- ListItem::Worktree(info) => {
- let mut display = enrich_common_fields(
- &info.counts,
- &info.branch_diff,
- &info.upstream,
- &info.pr_status,
- );
- // Preserve status_display that was set in constructor
- display.status_display = info.display.status_display.clone();
- info.display = display;
-
- // Working tree specific field
- let (added, deleted) = info.working_tree_diff;
- info.working_diff_display = format_diff_plain(ColumnKind::WorkingDiff, added, deleted);
- }
- ListItem::Branch(info) => {
- let mut display = enrich_common_fields(
- &info.counts,
- &info.branch_diff,
- &info.upstream,
- &info.pr_status,
- );
- // Preserve status_display that was set in constructor
- display.status_display = info.display.status_display.clone();
- info.display = display;
- }
- }
- item
-}
-
pub fn handle_list(
format: crate::OutputFormat,
show_branches: bool,
@@ -95,8 +61,10 @@ pub fn handle_list(
match format {
crate::OutputFormat::Json => {
- let enriched_items: Vec<_> =
- items.into_iter().map(enrich_with_display_fields).collect();
+ let enriched_items: Vec<_> = items
+ .into_iter()
+ .map(ListItem::with_display_fields)
+ .collect();
let json = serde_json::to_string_pretty(&enriched_items).map_err(|e| {
GitError::CommandFailed(format!("Failed to serialize to JSON: {}", e))
@@ -198,3 +166,39 @@ impl SummaryMetrics {
}
}
}
+
+impl ListItem {
+ /// Enrich a ListItem with display fields for json-pretty format.
+ fn with_display_fields(mut self) -> Self {
+ match &mut self {
+ ListItem::Worktree(info) => {
+ let mut display = enrich_common_fields(
+ &info.counts,
+ &info.branch_diff,
+ &info.upstream,
+ &info.pr_status,
+ );
+ // Preserve status_display that was set in constructor
+ display.status_display = info.display.status_display.clone();
+ info.display = display;
+
+ // Working tree specific field
+ let (added, deleted) = info.working_tree_diff;
+ info.working_diff_display =
+ ColumnKind::WorkingDiff.format_diff_plain(added, deleted);
+ }
+ ListItem::Branch(info) => {
+ let mut display = enrich_common_fields(
+ &info.counts,
+ &info.branch_diff,
+ &info.upstream,
+ &info.pr_status,
+ );
+ // Preserve status_display that was set in constructor
+ display.status_display = info.display.status_display.clone();
+ info.display = display;
+ }
+ }
+ self
+ }
+}
diff --git a/src/commands/list/render.rs b/src/commands/list/render.rs
index 9c04e92b5..006b1c198 100644
--- a/src/commands/list/render.rs
+++ b/src/commands/list/render.rs
@@ -55,11 +55,13 @@ fn format_plain_diff_from_config(
}
}
-/// 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()?;
+impl ColumnKind {
+ /// Format diff-style values as plain text with ANSI colors (for json-pretty).
+ pub(crate) fn format_diff_plain(self, positive: usize, negative: usize) -> Option<String> {
+ let config = self.diff_display_config()?;
- format_plain_diff_from_config(positive, negative, config)
+ format_plain_diff_from_config(positive, negative, config)
+ }
}
impl PrStatus {