Skip to content

Commit 5bb35b4

Browse files
authored
Merge pull request #3804 from leeewee/fix-snip-terminal-width-1-panic
Fix capacity-overflow panic in snip separator at --terminal-width 1
2 parents af1f53d + d28aa4a commit 5bb35b4

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- Syntax highlighting for Python files using uv as script runner in shebang #3689 (@janlarres)
2323

2424
## Bugfixes
25+
- Fix `capacity overflow` panic when printing a snip separator at `--terminal-width=1` with multiple line ranges. Closes #3803, see #3804 (@leeewee)
2526
- Pass `--no-paging` to `bat` invocations inside the bash / zsh / fish / PowerShell shell completion scripts so that shell-level pager wiring (e.g. `LESSOPEN='|-bat -f -pp %s'`) cannot inject ANSI escape sequences into the completion candidates. Closes #3760 (@mvanhorn)
2627
- Quote filenames before substituting them into `$LESSOPEN` / `$LESSCLOSE` templates, preventing shell injection when a filename contains shell metacharacters, see #3726 (@curious-rabbit)
2728
- Fix `--list-themes` unconditionally probing the terminal via OSC 10/11 even when `--theme` was set to an explicit value, see #3700 (regression introduced in bc42149a). (@optimistiCli)

src/printer.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -603,11 +603,23 @@ impl Printer for InteractivePrinter<'_> {
603603
let title = "8<";
604604
let title_count = title.chars().count();
605605

606-
let snip_left = "─ ".repeat((self.config.term_width - panel_count - (title_count / 2)) / 4);
606+
let snip_left = "─ ".repeat(
607+
self.config
608+
.term_width
609+
.saturating_sub(panel_count)
610+
.saturating_sub(title_count / 2)
611+
/ 4,
612+
);
607613
let snip_left_count = snip_left.chars().count(); // Can't use .len() with Unicode.
608614

609-
let snip_right =
610-
" ─".repeat((self.config.term_width - panel_count - snip_left_count - title_count) / 2);
615+
let snip_right = " ─".repeat(
616+
self.config
617+
.term_width
618+
.saturating_sub(panel_count)
619+
.saturating_sub(snip_left_count)
620+
.saturating_sub(title_count)
621+
/ 2,
622+
);
611623

612624
writeln!(
613625
handle,

tests/integration_tests.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,19 @@ fn line_range_multiple() {
305305
.stdout("line 1\nline 2\nline 4\n");
306306
}
307307

308+
#[test]
309+
fn snip_at_terminal_width_one_does_not_panic() {
310+
bat()
311+
.arg("multiline.txt")
312+
.arg("--style=snip")
313+
.arg("--color=always")
314+
.arg("--terminal-width=1")
315+
.arg("--line-range=1:2")
316+
.arg("--line-range=4:4")
317+
.assert()
318+
.success();
319+
}
320+
308321
#[test]
309322
fn line_range_multiple_with_context() {
310323
bat()

0 commit comments

Comments
 (0)