mirror of
https://github.com/sharkdp/bat
synced 2026-08-09 19:51:48 +00:00
fix(list-languages): clamp desired_width to avoid usize underflow at tiny --terminal-width
`get_languages` computed `desired_width = term_width - longest - separator.len()`.
When `--terminal-width` is smaller than the longest language name (e.g. 1), this
underflows `usize`: in overflow-checked builds it panics ("attempt to subtract with
overflow"), and in release it wraps to ~usize::MAX, silently disabling the extension
line-wrapping so `--terminal-width` is ignored for `--list-languages`.
Use `saturating_sub`, mirroring the fix applied to `print_snip` in #3804 (the snip
separator had the same `term_width - ...` underflow pattern). Output at normal widths
is unchanged.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+8
-1
@@ -152,7 +152,14 @@ pub fn get_languages(config: &Config, cache_dir: &Path) -> Result<String> {
|
|||||||
let comma_separator = ", ";
|
let comma_separator = ", ";
|
||||||
let separator = " ";
|
let separator = " ";
|
||||||
// Line-wrapping for the possible file extension overflow.
|
// Line-wrapping for the possible file extension overflow.
|
||||||
let desired_width = config.term_width - longest - separator.len();
|
// Clamp instead of subtracting: a tiny `--terminal-width` (smaller than the
|
||||||
|
// longest language name) would otherwise underflow `usize` and wrap to a huge
|
||||||
|
// value, silently disabling wrapping (and panicking in debug/overflow-checked
|
||||||
|
// builds). Mirrors the `saturating_sub` fix applied to `print_snip` in #3804.
|
||||||
|
let desired_width = config
|
||||||
|
.term_width
|
||||||
|
.saturating_sub(longest)
|
||||||
|
.saturating_sub(separator.len());
|
||||||
|
|
||||||
let style = if config.colored_output {
|
let style = if config.colored_output {
|
||||||
Green.normal()
|
Green.normal()
|
||||||
|
|||||||
Reference in New Issue
Block a user