Fix terminal width detection

This was previously fixed in
cb900c3463 (see commit message), but
broken in #341.

Instead, use both term_size and terminal_size, to maximise our chances
that we can detect the width. Also comment the code with the relevant
terminal_size issue.

Fixes #346
add_libdifftastic
Wilfred Hughes 2022-09-02 11:07:48 +07:00
parent df5c787ec7
commit a04f867557
4 changed files with 33 additions and 1 deletions

@ -7,6 +7,11 @@ UTF-16-LE. Previously it required files to be UTF-8.
Added support for Makefiles.
### Command Line Interface
Fixed terminal width detection when only stderr is a TTY (e.g. when
using difftastic with git). This was broken in 0.34.
## 0.34 (released 27th August 2022)
### Build

11
Cargo.lock generated

@ -207,6 +207,7 @@ dependencies = [
"rpds",
"rustc-hash",
"strsim",
"term_size",
"terminal_size 0.2.1",
"tree-sitter",
"tree_magic_mini",
@ -601,6 +602,16 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "term_size"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e4129646ca0ed8f45d09b929036bafad5377103edd06e50bf574b353d2b08d9"
dependencies = [
"libc",
"winapi",
]
[[package]]
name = "termcolor"
version = "1.1.3"

@ -48,6 +48,7 @@ rayon = "1.5.2"
tree_magic_mini = "3.0.3"
bumpalo = "3.9.1"
unicode-width = "0.1.9"
term_size = "0.3.2"
[dev-dependencies]
pretty_assertions = "1.2.1"

@ -377,7 +377,22 @@ pub fn parse_args() -> Mode {
/// Choose the display width: try to autodetect, or fall back to a
/// sensible default.
fn detect_display_width() -> usize {
terminal_size::terminal_size().map_or(80, |(w, _)| w.0 as usize)
// terminal_size is actively maintained, but only considers
// stdout. This is a problem inside git, where stderr is a TTY
// with a size but stdout is piped to less.
//
// https://github.com/eminence/terminal-size/issues/23
if let Some(width) = terminal_size::terminal_size().map(|(w, _)| w.0 as usize) {
return width;
}
// term_size is no longer maintained, but it checks all of stdin,
// stdout and stderr, so gives better results in may cases.
if let Some(width) = term_size::dimensions().map(|(w, _)| w) {
return width;
}
80
}
pub fn should_use_color(color_output: ColorOutput) -> bool {