Skip to content

Commit d5e0565

Browse files
committed
Styled zero lines fix
With `interactive.diffFilter = delta --color-only` delta is called by e.g. `git add -p`, but in this mode git hides the terminal from the pager. Plus/minus lines correctly use ANSI sequences to paint up to to the end of the line, but zero lines always use spaces. This needs the terminal width, but it is not available for diffFilter. So the fallback of 80 is used, and zero styles did not extend to the full terminal width. Since zero lines are only rarely styled (e.g. via `--zero-style='syntax "#1d1f21" dim'`), this was never noticed. This also crashed delta when a zero line was longer than 80.
1 parent 686f19a commit d5e0565

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/paint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl<'p> Painter<'p> {
204204
self.config,
205205
&mut self.line_numbers_data.as_mut(),
206206
None,
207-
BgShouldFill::With(BgFillMethod::Spaces),
207+
BgShouldFill::default(),
208208
);
209209
}
210210
}

src/tests/test_example_diffs.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,6 +2137,36 @@ src/align.rs:71: impl<'a> Alignment<'a> { │
21372137
"###);
21382138
}
21392139

2140+
#[test]
2141+
fn test_color_only_diff_filter_zero_style_bg() {
2142+
let result =
2143+
DeltaTest::with_args(&["--color-only", r##"--zero-style='syntax "#1d1f21" dim'"##])
2144+
.set_config(|c| c.available_terminal_width = 80)
2145+
.explain_ansi()
2146+
.with_input(GIT_DIFF_OF_WIDTH_81);
2147+
2148+
assert_snapshot!(result.output, @r###"
2149+
(normal)
2150+
--- a.rs
2151+
+++ b.rs
2152+
@@ -1,3 +1,3 @@
2153+
(normal 22)+(231) (203)if(231) (81)let(231) (149)Ok(231)(foo) { (242)// Works fine with plus hunk lines which are longer 81(normal)
2154+
(normal 52)-// and it works fine with minus lines, however 81(normal)
2155+
(dim normal 234) (242)// styled(!) zero lines can only be as long as the width fallback of 80(normal)
2156+
(dim normal 234) (231)panic!(); (242)/* if no tty can be queried, and delta crashes on longer lines: 81(normal)
2157+
"###);
2158+
}
2159+
2160+
const GIT_DIFF_OF_WIDTH_81: &str = r#"
2161+
--- a.rs
2162+
+++ b.rs
2163+
@@ -1,3 +1,3 @@
2164+
+ if let Ok(foo) { // Works fine with plus hunk lines which are longer 81
2165+
-// and it works fine with minus lines, however 81
2166+
// styled(!) zero lines can only be as long as the width fallback of 80
2167+
panic!(); /* if no tty can be queried, and delta crashes on longer lines: 81
2168+
"#;
2169+
21402170
const GIT_DIFF_SINGLE_HUNK: &str = "\
21412171
commit 94907c0f136f46dc46ffae2dc92dca9af7eb7c2e
21422172
Author: Dan Davison <[email protected]>

0 commit comments

Comments
 (0)