Skip to content

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

Extract width override for testable bash formatting

Maximilian Roos21172c7c4ef6
diff --git a/src/styling/format.rs b/src/styling/format.rs
index 6ae5d3244..364fa538d 100644
--- a/src/styling/format.rs
+++ b/src/styling/format.rs
@@ -162,18 +162,12 @@ pub(super) fn wrap_styled_text(styled: &str, max_width: usize) -> Vec<String> {
cleaned.lines().map(|s| s.to_owned()).collect()
}
-/// Formats bash/shell commands with syntax highlighting and gutter
-///
-/// Processes each line separately for highlighting (required for multi-line commands
-/// with `&&` at line ends), then applies template syntax detection to avoid
-/// misinterpreting `}}` as a command when it appears at line start.
-///
-/// # Example
-/// ```ignore
-/// print!("{}", format_bash_with_gutter("npm install --frozen-lockfile", ""));
-/// ```
#[cfg(feature = "syntax-highlighting")]
-pub fn format_bash_with_gutter(content: &str, left_margin: &str) -> String {
+fn format_bash_with_gutter_impl(
+ content: &str,
+ left_margin: &str,
+ width_override: Option<usize>,
+) -> String {
use tree_sitter_highlight::{HighlightConfiguration, HighlightEvent, Highlighter};
let gutter = super::GUTTER;
@@ -182,7 +176,7 @@ pub fn format_bash_with_gutter(content: &str, left_margin: &str) -> String {
let mut output = String::new();
// Calculate available width for content
- let term_width = get_terminal_width();
+ let term_width = width_override.unwrap_or_else(get_terminal_width);
let left_margin_width = left_margin.width();
let available_width = term_width.saturating_sub(3 + left_margin_width);
@@ -300,6 +294,33 @@ pub fn format_bash_with_gutter(content: &str, left_margin: &str) -> String {
output
}
+/// Formats bash/shell commands with syntax highlighting and gutter
+///
+/// Processes each line separately for highlighting (required for multi-line commands
+/// with `&&` at line ends), then applies template syntax detection to avoid
+/// misinterpreting `}}` as a command when it appears at line start.
+///
+/// # Example
+/// ```ignore
+/// print!("{}", format_bash_with_gutter("npm install --frozen-lockfile", ""));
+/// ```
+#[cfg(feature = "syntax-highlighting")]
+pub fn format_bash_with_gutter(content: &str, left_margin: &str) -> String {
+ format_bash_with_gutter_impl(content, left_margin, None)
+}
+
+/// Test-only helper to force a specific terminal width for deterministic output.
+///
+/// This avoids env var mutation which is unsafe in parallel tests.
+#[cfg(all(test, feature = "syntax-highlighting"))]
+pub(crate) fn format_bash_with_gutter_at_width(
+ content: &str,
+ left_margin: &str,
+ width: usize,
+) -> String {
+ format_bash_with_gutter_impl(content, left_margin, Some(width))
+}
+
/// Format bash commands with gutter (fallback without syntax highlighting)
///
/// This version is used when the `syntax-highlighting` feature is disabled.
diff --git a/src/styling/mod.rs b/src/styling/mod.rs
index 18acb30a8..848acdf8c 100644
--- a/src/styling/mod.rs
+++ b/src/styling/mod.rs
@@ -26,6 +26,8 @@ pub use anstyle::Style as AnstyleStyle;
// Re-export our public types
pub use constants::*;
+#[cfg(all(test, feature = "syntax-highlighting"))]
+pub(crate) use format::format_bash_with_gutter_at_width;
pub use format::{GUTTER_OVERHEAD, format_bash_with_gutter, format_with_gutter};
pub use highlighting::format_toml;
pub use line::{StyledLine, StyledString, truncate_visible};
@@ -549,8 +551,8 @@ command = "npm install"
let command = "cp -cR {{ repo_root }}/target/debug/.fingerprint {{ repo_root }}/target/debug/build {{ worktree }}/target/debug/";
- // Get the result (will use actual terminal width, but we can still check structure)
- let result = format_bash_with_gutter(command, "");
+ // Use explicit width for deterministic output (avoids env var mutation in parallel tests)
+ let result = format_bash_with_gutter_at_width(command, "", 80);
// Snapshot the raw output to verify ANSI codes are consistent
insta::assert_snapshot!(result);
@@ -579,7 +581,8 @@ cp -c {{ repo_root }}/target/debug/deps/*.rlib {{ repo_root }}/target/debug/deps
cp -cR {{ repo_root }}/target/debug/.fingerprint {{ repo_root }}/target/debug/build {{ worktree
}}/target/debug/"#;
- let result = format_bash_with_gutter(multiline_command, "");
+ // Use explicit width for deterministic output (avoids env var mutation in parallel tests)
+ let result = format_bash_with_gutter_at_width(multiline_command, "", 80);
// Snapshot the output - each line should have consistent dim styling
insta::assert_snapshot!(result);