Improve terminal width detection

Ensure the value is always non-zero, and consider $COLUMNS if
crossterm does not succeed.

Fixes #707
pull/708/merge
Wilfred Hughes 2024-05-10 16:26:48 +07:00
parent 9d2574dbd1
commit edb839c803
2 changed files with 24 additions and 1 deletions

@ -29,6 +29,14 @@ Difftastic now has a man page, see the `difft.1` file.
Fixed a memory leak and substantially improved performance in some
cases (up to 2x in testing).
### Command Line Interface
Fixed a crash when difftastic could not detect the terminal width,
such as inside eshell.
Difftastic now also considers $COLUMNS when detecting the terminal
width.
## 0.57 (released 1st April 2024)
### Parsing

@ -864,7 +864,22 @@ pub(crate) fn parse_args() -> Mode {
/// to a sensible default value.
fn detect_terminal_width() -> usize {
if let Ok((columns, _rows)) = crossterm::terminal::size() {
return columns.into();
if columns > 0 {
return columns.into();
}
}
// If crossterm couldn't detect the terminal width, use the
// shell variable COLUMNS if it's set. This helps with terminals like eshell.
//
// https://github.com/Wilfred/difftastic/issues/707
// https://stackoverflow.com/a/48016366
if let Ok(columns_env_val) = std::env::var("COLUMNS") {
if let Ok(columns) = columns_env_val.parse::<usize>() {
if columns > 0 {
return columns;
}
}
}
DEFAULT_TERMINAL_WIDTH