Skip to content

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

Fix: Correct diff stat width for gutter formatting

Maximilian Rooscac78c3c109e
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index 390b6eb92..32632256e 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -663,7 +663,7 @@ pub fn handle_push(
.trim()
.to_string();
- // Get formatted diff stat
+ // Get formatted diff stat (full terminal width - no gutter in error display)
let term_width = crate::display::get_terminal_width();
let files_formatted = repo
.run_command(&[
@@ -772,13 +772,14 @@ pub fn handle_push(
])?;
crate::output::gutter(format_with_gutter(&log_output, "", None))?;
- // Show diff statistics with color (use terminal width for proper formatting)
+ // Show diff statistics with color (use terminal width minus gutter overhead)
let term_width = crate::display::get_terminal_width();
+ let stat_width = term_width.saturating_sub(worktrunk::styling::GUTTER_OVERHEAD);
let diff_stat = repo.run_command(&[
"diff",
"--color=always",
"--stat",
- &format!("--stat-width={}", term_width),
+ &format!("--stat-width={}", stat_width),
&format!("{}..HEAD", target_branch),
])?;
diff --git a/src/styling/format.rs b/src/styling/format.rs
index be431c7bf..c0ac1efdf 100644
--- a/src/styling/format.rs
+++ b/src/styling/format.rs
@@ -11,6 +11,18 @@ use super::highlighting::bash_token_style;
/// Default terminal width fallback if detection fails
const DEFAULT_TERMINAL_WIDTH: usize = 80;
+/// Width overhead added by format_with_gutter()
+///
+/// The gutter formatting adds:
+/// - 1 column: colored space (gutter)
+/// - 2 columns: regular spaces for padding
+///
+/// Total: 3 columns
+///
+/// When passing widths to tools like git --stat-width, subtract this overhead
+/// so the final output (content + gutter) fits within the terminal width.
+pub const GUTTER_OVERHEAD: usize = 3;
+
/// Strip ANSI escape codes from a string using strip-ansi-escapes
fn strip_ansi_codes(s: &str) -> String {
strip_ansi_escapes::strip_str(s)
diff --git a/src/styling/mod.rs b/src/styling/mod.rs
index 37d2c951e..cc90e3f07 100644
--- a/src/styling/mod.rs
+++ b/src/styling/mod.rs
@@ -26,7 +26,7 @@ pub use anstyle::Style as AnstyleStyle;
// Re-export our public types
pub use constants::*;
-pub use format::{format_bash_with_gutter, format_with_gutter};
+pub use format::{GUTTER_OVERHEAD, format_bash_with_gutter, format_with_gutter};
pub use highlighting::format_toml;
pub use line::{StyledLine, StyledString};