From fe68f43e93fefdc6d5d962804ba35a910ac9893c Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Mon, 16 Jan 2023 23:55:03 -0800 Subject: [PATCH] Use crossterm for is_tty and terminal width This is activly maintained, handles stdout being directed, and seems to be tested more on Windows too (potentially improving #363). --- Cargo.lock | 194 ++++++++++++++++++++++++++++++------------------- Cargo.toml | 4 +- src/options.rs | 19 +---- 3 files changed, 126 insertions(+), 91 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7653dc917..9ec3652c2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -80,7 +80,7 @@ dependencies = [ "lazy_static", "strsim", "termcolor", - "terminal_size 0.1.17", + "terminal_size", "textwrap", ] @@ -158,6 +158,31 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "crossterm" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e64e6c0fbe2c17357405f7c758c1ef960fce08bdfb2c03d88d2a18d7e09c4b67" +dependencies = [ + "bitflags", + "crossterm_winapi", + "libc", + "mio", + "parking_lot", + "signal-hook", + "signal-hook-mio", + "winapi", +] + +[[package]] +name = "crossterm_winapi" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ae1b35a484aa10e07fe0638d02301c5ad24de82d310ccbd2f3693da5f09bf1c" +dependencies = [ + "winapi", +] + [[package]] name = "ctor" version = "0.1.22" @@ -178,11 +203,11 @@ checksum = "0e25ea47919b1560c4e3b7fe0aaab9becf5b84a10325ddf7db0f0ba5e1026499" name = "difftastic" version = "0.43.0" dependencies = [ - "atty", "bumpalo", "cc", "clap", "const_format", + "crossterm", "itertools", "lazy_static", "libc", @@ -197,8 +222,6 @@ dependencies = [ "regex", "rustc-hash", "strsim", - "term_size", - "terminal_size 0.2.3", "tree-sitter", "tree_magic_mini", "typed-arena", @@ -227,27 +250,6 @@ dependencies = [ "termcolor", ] -[[package]] -name = "errno" -version = "0.2.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1" -dependencies = [ - "errno-dragonfly", - "libc", - "winapi", -] - -[[package]] -name = "errno-dragonfly" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" -dependencies = [ - "cc", - "libc", -] - [[package]] name = "fixedbitset" version = "0.4.1" @@ -294,16 +296,6 @@ dependencies = [ "hashbrown", ] -[[package]] -name = "io-lifetimes" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7d6c6f8c91b4b9ed43484ad1a938e393caf35960fce7f82a040497207bd8e9e" -dependencies = [ - "libc", - "windows-sys", -] - [[package]] name = "itertools" version = "0.10.3" @@ -335,10 +327,14 @@ dependencies = [ ] [[package]] -name = "linux-raw-sys" -version = "0.1.4" +name = "lock_api" +version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" +checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" +dependencies = [ + "autocfg", + "scopeguard", +] [[package]] name = "log" @@ -379,6 +375,18 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" +[[package]] +name = "mio" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5d732bc30207a6423068df043e3d02e0735b155ad7ce1a6f76fe2baa5b158de" +dependencies = [ + "libc", + "log", + "wasi", + "windows-sys", +] + [[package]] name = "nom" version = "7.1.1" @@ -426,6 +434,29 @@ version = "3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "decf7381921fea4dcb2549c5667eda59b3ec297ab7e2b5fc33eac69d2e7da87b" +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba1ef8814b5c993410bb3adfad7a5ed269563e4a2f90c41f5d85be7fb47133bf" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-sys", +] + [[package]] name = "petgraph" version = "0.6.1" @@ -510,6 +541,15 @@ dependencies = [ "num_cpus", ] +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + [[package]] name = "regex" version = "1.5.6" @@ -533,20 +573,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" -[[package]] -name = "rustix" -version = "0.36.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4feacf7db682c6c329c4ede12649cd36ecab0f3be5b7d74e6a20304725db4549" -dependencies = [ - "bitflags", - "errno", - "io-lifetimes", - "libc", - "linux-raw-sys", - "windows-sys", -] - [[package]] name = "same-file" version = "1.0.6" @@ -562,6 +588,42 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "signal-hook" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a253b5e89e2698464fc26b545c9edceb338e18a89effeeecfea192c3025be29d" +dependencies = [ + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-mio" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af" +dependencies = [ + "libc", + "mio", + "signal-hook", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0" +dependencies = [ + "libc", +] + +[[package]] +name = "smallvec" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" + [[package]] name = "strsim" version = "0.10.0" @@ -579,16 +641,6 @@ 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" @@ -608,23 +660,13 @@ dependencies = [ "winapi", ] -[[package]] -name = "terminal_size" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb20089a8ba2b69debd491f8d2d023761cbf196e999218c591fa1e7e15a21907" -dependencies = [ - "rustix", - "windows-sys", -] - [[package]] name = "textwrap" version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" dependencies = [ - "terminal_size 0.1.17", + "terminal_size", ] [[package]] @@ -691,6 +733,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 211525b06..657ead8f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,6 @@ typed-arena = "2.0.1" rustc-hash = "1.1.0" strsim = "0.10.0" lazy_static = "1.4.0" -atty = "0.2.14" tree-sitter = "0.20.9" libc = "0.2.108" log = "0.4.14" @@ -46,7 +45,6 @@ mimalloc = { version = "0.1.28", default-features = false } libmimalloc-sys = "=0.1.24" radix-heap = "0.4.2" walkdir = "2.3.2" -terminal_size = "0.2.3" const_format = "0.2.22" owo-colors = "3.3.0" wu-diff = "0.1.2" @@ -54,7 +52,7 @@ rayon = "1.6.1" tree_magic_mini = "3.0.2" bumpalo = "3.11.1" unicode-width = "0.1.9" -term_size = "0.3.2" +crossterm = { version = "0.25.0", features = [] } [dev-dependencies] pretty_assertions = "1.2.1" diff --git a/src/options.rs b/src/options.rs index 60ec9aa86..b5a8b3654 100644 --- a/src/options.rs +++ b/src/options.rs @@ -2,9 +2,9 @@ use std::{borrow::Borrow, env, ffi::OsStr, path::Path, path::PathBuf}; -use atty::Stream; use clap::{crate_authors, crate_description, crate_version, Arg, Command}; use const_format::formatcp; +use crossterm::tty::IsTty; use crate::{ display::style::BackgroundColor, exit_codes::EXIT_BAD_ARGUMENTS, parse::guess_language, @@ -533,19 +533,8 @@ 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 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; + if let Ok((columns, _rows)) = crossterm::terminal::size() { + return columns.into(); } 80 @@ -558,7 +547,7 @@ pub fn should_use_color(color_output: ColorOutput) -> bool { // Always enable colour if stdout is a TTY or if the git pager is active. // TODO: consider following the env parsing logic in git_config_bool // in config.c. - atty::is(Stream::Stdout) || env::var("GIT_PAGER_IN_USE").is_ok() + std::io::stdout().is_tty() || env::var("GIT_PAGER_IN_USE").is_ok() } ColorOutput::Never => false, }