diff --git a/CHANGELOG.md b/CHANGELOG.md index 9049dca1..1e5b161e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ - Fixed syntax tests path, see #3610 (@foxfromworld) - Fix zsh tab completion word-splitting language names containing spaces (e.g. `HTML (Jinja2)`, `Apache Conf`), see #3693 (@YoshKoz) - Fix zsh tab completion offering invalid `-l` arguments (file globs, paths, hidden filenames) sourced from the second column of `--list-languages`. Closes #3735, see #3737 (@truffle-dev) +- Fix `usize` underflow in `--list-languages` when `--terminal-width` is smaller than the longest language name, see #3812 (@greymoth-jp) ## Other - Use git version of cross. See #3533 (@OctopusET) diff --git a/src/bin/bat/main.rs b/src/bin/bat/main.rs index 1d89f8ec..c8bbd368 100644 --- a/src/bin/bat/main.rs +++ b/src/bin/bat/main.rs @@ -152,7 +152,14 @@ pub fn get_languages(config: &Config, cache_dir: &Path) -> Result { let comma_separator = ", "; let separator = " "; // 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 { Green.normal()