summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilfred Hughes <me@wilfred.me.uk>2024-05-10 16:26:48 -0700
committerWilfred Hughes <me@wilfred.me.uk>2024-05-10 16:26:48 -0700
commitedb839c803f4b0b991ec8d24a77d48bc5514029d (patch)
tree97855ad5881ed2d85314fd5c08d53c51ebca0ebc
parent9d2574dbd1ae949f106ac8d254925963cf4e9a55 (diff)
Improve terminal width detection
Ensure the value is always non-zero, and consider $COLUMNS if crossterm does not succeed. Fixes #707
-rw-r--r--CHANGELOG.md8
-rw-r--r--src/options.rs17
2 files changed, 24 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9c6374211..4ffdb5794 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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
diff --git a/src/options.rs b/src/options.rs
index daa156ee0..774da3586 100644
--- a/src/options.rs
+++ b/src/options.rs
@@ -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