diff --git a/CHANGELOG.md b/CHANGELOG.md index 08c502a5..c57dd731 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ - Syntax highlighting for Python files using uv as script runner in shebang #3689 (@janlarres) ## Bugfixes +- Fix `capacity overflow` panic when printing a snip separator at `--terminal-width=1` with multiple line ranges. Closes #3803, see #3804 (@leeewee) - 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) - Quote filenames before substituting them into `$LESSOPEN` / `$LESSCLOSE` templates, preventing shell injection when a filename contains shell metacharacters, see #3726 (@curious-rabbit) - 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) diff --git a/src/printer.rs b/src/printer.rs index 17f7aa30..fc8a70ba 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -603,11 +603,23 @@ impl Printer for InteractivePrinter<'_> { let title = "8<"; let title_count = title.chars().count(); - let snip_left = "─ ".repeat((self.config.term_width - panel_count - (title_count / 2)) / 4); + let snip_left = "─ ".repeat( + self.config + .term_width + .saturating_sub(panel_count) + .saturating_sub(title_count / 2) + / 4, + ); let snip_left_count = snip_left.chars().count(); // Can't use .len() with Unicode. - let snip_right = - " ─".repeat((self.config.term_width - panel_count - snip_left_count - title_count) / 2); + let snip_right = " ─".repeat( + self.config + .term_width + .saturating_sub(panel_count) + .saturating_sub(snip_left_count) + .saturating_sub(title_count) + / 2, + ); writeln!( handle, diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 432faed8..2f2dcce8 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -305,6 +305,19 @@ fn line_range_multiple() { .stdout("line 1\nline 2\nline 4\n"); } +#[test] +fn snip_at_terminal_width_one_does_not_panic() { + bat() + .arg("multiline.txt") + .arg("--style=snip") + .arg("--color=always") + .arg("--terminal-width=1") + .arg("--line-range=1:2") + .arg("--line-range=4:4") + .assert() + .success(); +} + #[test] fn line_range_multiple_with_context() { bat()