Skip to content

Commit

Permalink
Improve terminal width detection
Browse files Browse the repository at this point in the history
Ensure the value is always non-zero, and consider $COLUMNS if
crossterm does not succeed.

Fixes #707
  • Loading branch information
Wilfred committed May 10, 2024
1 parent 9d2574d commit edb839c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit edb839c

Please sign in to comment.