Skip to content

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

Move ideal column calculation to ColumnKind

Maximilian Roos8aa85e5d643c
diff --git a/src/commands/list/ci_status.rs b/src/commands/list/ci_status.rs
index 9f56b486a..b48a84e7f 100644
--- a/src/commands/list/ci_status.rs
+++ b/src/commands/list/ci_status.rs
@@ -371,7 +371,7 @@ impl PrStatus {
}
// Parse JSON output
- let pipelines: Vec<GitLabPipelineList> = match serde_json::from_slice(&output.stdout) {
+ let pipelines: Vec<GitLabPipeline> = match serde_json::from_slice(&output.stdout) {
Ok(pipelines) => pipelines,
Err(e) => {
log::warn!(
@@ -501,49 +501,25 @@ impl GitLabMrInfo {
#[derive(Debug, Deserialize)]
struct GitLabPipeline {
status: Option<String>,
-}
-
-/// GitLab pipeline from `glab ci list --output json`
-#[derive(Debug, Deserialize)]
-struct GitLabPipelineList {
- status: Option<String>,
+ /// Only present in `glab ci list` output, not in MR view embedded pipeline
+ #[serde(default)]
sha: 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,
- }
+fn parse_gitlab_status(status: Option<&str>) -> CiStatus {
+ match status {
+ 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,
}
}
-impl GitLabPipelineList {
+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,
- }
+ parse_gitlab_status(self.status.as_deref())
}
}
diff --git a/src/commands/list/layout.rs b/src/commands/list/layout.rs
index 29808cb70..fa60b3ad7 100644
--- a/src/commands/list/layout.rs
+++ b/src/commands/list/layout.rs
@@ -132,7 +132,7 @@
//! for candidate in candidates {
//! if candidate.spec.kind == ColumnKind::Message {
//! // Special handling: flexible width (min 20, preferred 50)
-//! } else if let Some(ideal) = ideal_for_column(candidate.spec, ...) {
+//! } else if let Some(ideal) = candidate.spec.kind.ideal(...) {
//! if let allocated = try_allocate(&mut remaining, ideal.width, ...) {
//! pending.push(PendingColumn { spec: candidate.spec, width: allocated, format: ideal.format });
//! }
@@ -321,6 +321,32 @@ impl ColumnKind {
ColumnKind::Message => true,
}
}
+
+ fn ideal(
+ self,
+ widths: &ColumnWidths,
+ max_path_width: usize,
+ commit_width: usize,
+ ) -> Option<ColumnIdeal> {
+ match self {
+ ColumnKind::Gutter => ColumnIdeal::text(2), // Fixed width: symbol (1 char) + space (1 char)
+ ColumnKind::Branch => ColumnIdeal::text(widths.branch),
+ ColumnKind::Status => ColumnIdeal::text(widths.status),
+ ColumnKind::Path => ColumnIdeal::text(max_path_width),
+ ColumnKind::Time => ColumnIdeal::text(widths.time),
+ ColumnKind::CiStatus => ColumnIdeal::text(widths.ci_status),
+ ColumnKind::Commit => ColumnIdeal::text(commit_width),
+ ColumnKind::Message => None,
+ 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),
+ }
+ }
}
#[derive(Clone, Copy, Debug)]
@@ -404,28 +430,6 @@ struct PendingColumn<'a> {
format: ColumnFormat,
}
-fn ideal_for_column(
- spec: &ColumnSpec,
- widths: &ColumnWidths,
- max_path_width: usize,
- commit_width: usize,
-) -> Option<ColumnIdeal> {
- match spec.kind {
- ColumnKind::Gutter => ColumnIdeal::text(2), // Fixed width: symbol (1 char) + space (1 char)
- ColumnKind::Branch => ColumnIdeal::text(widths.branch),
- ColumnKind::Status => ColumnIdeal::text(widths.status),
- ColumnKind::Path => ColumnIdeal::text(max_path_width),
- ColumnKind::Time => ColumnIdeal::text(widths.time),
- ColumnKind::CiStatus => ColumnIdeal::text(widths.ci_status),
- ColumnKind::Commit => ColumnIdeal::text(commit_width),
- ColumnKind::Message => None,
- 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),
- }
-}
-
/// Build pre-allocated column width estimates.
///
/// Uses generous fixed allocations for expensive-to-compute columns (status, diffs, time, CI)
@@ -574,7 +578,9 @@ fn allocate_columns_with_priority(
}
// For non-message columns
- let Some(ideal) = ideal_for_column(spec, &metadata.widths, max_path_width, commit_width)
+ let Some(ideal) = spec
+ .kind
+ .ideal(&metadata.widths, max_path_width, commit_width)
else {
continue;
};
diff --git a/src/styling/format.rs b/src/styling/format.rs
index f4a6534aa..8ed9eb03c 100644
--- a/src/styling/format.rs
+++ b/src/styling/format.rs
@@ -138,6 +138,36 @@ pub fn format_with_gutter(content: &str, left_margin: &str, max_width: Option<us
output
}
+/// Wrap content lines for gutter display
+fn wrap_content_for_gutter(content: &str, left_margin: &str) -> Vec<String> {
+ let term_width = get_terminal_width();
+ let left_margin_width = left_margin.width();
+ let available_width = term_width.saturating_sub(3 + left_margin_width);
+
+ let mut wrapped_lines = Vec::new();
+ for line in content.lines() {
+ let wrapped = wrap_text_at_width(line, available_width);
+ wrapped_lines.extend(wrapped);
+ }
+ wrapped_lines
+}
+
+/// Format wrapped lines with plain gutter (no syntax highlighting)
+#[cfg(not(feature = "syntax-highlighting"))]
+fn format_wrapped_lines_plain(wrapped_lines: &[String], left_margin: &str) -> String {
+ let gutter = super::GUTTER;
+ let mut output = String::new();
+
+ for line in wrapped_lines {
+ output.push_str(&format!("{left_margin}{gutter} {gutter:#} "));
+ output.push_str(line);
+ output.push_str(&format!("{}", anstyle::Reset));
+ output.push('\n');
+ }
+
+ output
+}
+
/// Formats bash/shell commands with syntax highlighting and gutter
///
/// Similar to `format_with_gutter` but applies bash syntax highlighting using tree-sitter.
@@ -154,17 +184,7 @@ pub fn format_bash_with_gutter(content: &str, left_margin: &str) -> String {
let gutter = super::GUTTER;
let mut output = String::new();
- // Calculate available width for wrapping
- let term_width = get_terminal_width();
- let left_margin_width = left_margin.width();
- let available_width = term_width.saturating_sub(3 + left_margin_width);
-
- // Wrap lines at word boundaries
- let mut wrapped_lines = Vec::new();
- for line in content.lines() {
- let wrapped = wrap_text_at_width(line, available_width);
- wrapped_lines.extend(wrapped);
- }
+ let wrapped_lines = wrap_content_for_gutter(content, left_margin);
// Set up tree-sitter bash highlighting
let highlight_names = vec![
@@ -257,29 +277,6 @@ pub fn format_bash_with_gutter(content: &str, left_margin: &str) -> String {
/// It provides the same gutter formatting without tree-sitter dependencies.
#[cfg(not(feature = "syntax-highlighting"))]
pub fn format_bash_with_gutter(content: &str, left_margin: &str) -> String {
- let gutter = super::GUTTER;
- let mut output = String::new();
-
- // Calculate available width for wrapping
- let term_width = get_terminal_width();
- let left_margin_width = left_margin.width();
- let available_width = term_width.saturating_sub(3 + left_margin_width);
-
- // Wrap lines at word boundaries
- let mut wrapped_lines = Vec::new();
- for line in content.lines() {
- let wrapped = wrap_text_at_width(line, available_width);
- wrapped_lines.extend(wrapped);
- }
-
- // Process each wrapped line with plain gutter (no syntax highlighting)
- for line in &wrapped_lines {
- output.push_str(&format!("{left_margin}{gutter} {gutter:#} "));
- output.push_str(line);
- // Ensure all styles are reset at end of line to prevent leaking into child process output
- output.push_str(&format!("{}", anstyle::Reset));
- output.push('\n');
- }
-
- output
+ let wrapped_lines = wrap_content_for_gutter(content, left_margin);
+ format_wrapped_lines_plain(&wrapped_lines, left_margin)
}