Skip to content

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

Fix dim attribute not preserved on wrapped continuation lines (#357)

Maximilian Roos9986f7e1c603
diff --git a/CLAUDE.md b/CLAUDE.md
index c8b005fac..e564c824f 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -170,10 +170,27 @@ Users can clean up old logs manually or use a git hook. No automatic cleanup is
## Coverage
+We maintain high code coverage. The `codecov/patch` CI check enforces coverage on changed lines — respond to failures by writing tests, not by ignoring them.
+
+### Running Coverage Locally
+
- Install once: `cargo install cargo-llvm-cov`.
-- Run: `./dev/coverage.sh` — runs tests once, then generates HTML (`target/llvm-cov/html/index.html`) and LCOV (`target/llvm-cov/lcov.info`). Set `COVERAGE_OPEN=0` to skip opening the HTML report.
+- Run: `./dev/coverage.sh` — runs tests once, then generates HTML (`target/llvm-cov/html/index.html`) and LCOV (`target/llvm-cov/lcov.info`).
- Pass extra args to the test run (for example to filter tests): `./dev/coverage.sh -- --test test_name`.
+### Investigating codecov/patch Failures
+
+When `codecov/patch` fails, find which changed lines lack coverage:
+
+```bash
+# 1. Run coverage and show uncovered lines
+./dev/coverage.sh
+cargo llvm-cov report --show-missing-lines | grep <file>
+
+# 2. Compare against your diff (use three-dot diff for PR changes)
+git diff main...HEAD -- path/to/file.rs
+```
+
## Benchmarks
For detailed benchmark documentation, see `benches/CLAUDE.md`.
diff --git a/dev/coverage.sh b/dev/coverage.sh
index 6819b3ee3..882089c8f 100755
--- a/dev/coverage.sh
+++ b/dev/coverage.sh
@@ -32,13 +32,8 @@ export NEXTEST_NO_INPUT_HANDLER=1
# Run tests once with instrumentation, without generating a report yet.
"${CARGO_BIN[@]}" llvm-cov --locked --workspace --no-report --features shell-integration-tests "$@"
-# Generate HTML (optionally open) and LCOV reports from the recorded data without rerunning tests.
-OPEN_FLAG=()
-if [ "${COVERAGE_OPEN:-1}" != "0" ]; then
- OPEN_FLAG=(--open)
-fi
-
-"${CARGO_BIN[@]}" llvm-cov report --html --output-dir "${COVERAGE_DIR}/html" "${OPEN_FLAG[@]}"
+# Generate HTML and LCOV reports from the recorded data without rerunning tests.
+"${CARGO_BIN[@]}" llvm-cov report --html --output-dir "${COVERAGE_DIR}/html"
"${CARGO_BIN[@]}" llvm-cov report --lcov --output-path "${LCOV_PATH}"
echo "HTML report: ${COVERAGE_DIR}/html/index.html"
diff --git a/src/styling/format.rs b/src/styling/format.rs
index adaa6aa02..6aa90d972 100644
--- a/src/styling/format.rs
+++ b/src/styling/format.rs
@@ -142,6 +142,10 @@ pub fn format_with_gutter(content: &str, max_width: Option<usize>) -> String {
///
/// Additionally, wrap_ansi may split between styled content and its reset code,
/// leaving [0m at the start of continuation lines. We move these to line ends.
+///
+/// IMPORTANT: wrap_ansi only restores foreground colors on continuation lines,
+/// not text attributes like dim. We detect this and prepend dim (\x1b[2m) to
+/// continuation lines that start with a color code, ensuring consistent dimming.
pub fn wrap_styled_text(styled: &str, max_width: usize) -> Vec<String> {
if max_width == 0 {
return vec![styled.to_string()];
@@ -168,13 +172,32 @@ pub fn wrap_styled_text(styled: &str, max_width: usize) -> Vec<String> {
// Fix reset codes that got separated from their content by wrapping.
// When wrap happens between styled text and its [0m reset, the reset
// ends up at the start of the next line. Strip leading resets.
- cleaned
- .lines()
- .map(|line| {
- let trimmed = line.strip_prefix("\x1b[0m").unwrap_or(line);
- trimmed.to_owned()
- })
- .collect()
+ //
+ // Also fix missing dim on continuation lines: wrap_ansi restores colors
+ // but not text attributes like dim. If a line starts with a color code
+ // (e.g., \x1b[32m) but no dim (\x1b[2m), prepend dim to maintain consistency.
+ let lines: Vec<_> = cleaned.lines().collect();
+ let mut result = Vec::with_capacity(lines.len());
+
+ for (i, line) in lines.iter().enumerate() {
+ let trimmed = line.strip_prefix("\x1b[0m").unwrap_or(line);
+
+ // For continuation lines (not first), check if we need to restore dim.
+ // wrap_ansi restores foreground colors (\x1b[3Xm where X is 0-7 or 8;...)
+ // but drops text attributes like dim (\x1b[2m).
+ //
+ // We restore dim for lines that start with a color code. This is safe
+ // because format_bash_with_gutter_impl always starts lines with dim,
+ // so any wrapped continuation should also be dimmed.
+ if i > 0 && trimmed.starts_with("\x1b[3") {
+ // Prepend dim before the color code
+ result.push(format!("\x1b[2m{trimmed}"));
+ } else {
+ result.push(trimmed.to_owned());
+ }
+ }
+
+ result
}
#[cfg(feature = "syntax-highlighting")]
@@ -478,6 +501,32 @@ mod tests {
assert!(!result[0].contains("\u{1b}[49m"));
}
+ #[test]
+ fn test_wrap_styled_text_restores_dim_on_continuation() {
+ // When wrap_ansi wraps dim+color text, it restores the color but not dim.
+ // We fix this by prepending dim to continuation lines that start with a color.
+ let dim = "\x1b[2m";
+ let green = "\x1b[32m";
+ let reset = "\x1b[0m";
+
+ // Simulate what format_bash_with_gutter_impl produces for a string token
+ let styled = format!(
+ "{dim}{green}This is a very long string that definitely needs to wrap across multiple lines{reset}"
+ );
+
+ // Force wrapping at 30 chars - should produce multiple lines
+ let result = wrap_styled_text(&styled, 30);
+ assert!(result.len() > 1);
+
+ // First line should have dim+green (as input)
+ assert!(result[0].starts_with("\x1b[2m\x1b[32m"));
+
+ // Continuation lines should ALSO have dim before the color (restored by our fix)
+ for line in result.iter().skip(1) {
+ assert!(line.starts_with("\x1b[2m\x1b[32m") || line.starts_with("\x1b[2m"));
+ }
+ }
+
#[test]
#[cfg(feature = "syntax-highlighting")]
fn test_format_bash_with_gutter_at_width_basic() {