From ed5a82543de03790ee79142674b2e1c7a33d1d00 Mon Sep 17 00:00:00 2001 From: Peter Grayson Date: Sun, 27 Nov 2022 22:38:27 -0500 Subject: [PATCH] refactor: Use is-terminal instead of atty This follows suit with clap, which also changed to is-terminal, to eliminate a redundant dependency and perhaps have a better maintained library. Refs: https://github.com/clap-rs/clap/pull/4249 --- Cargo.lock | 24 ++---------------------- Cargo.toml | 2 +- src/color.rs | 7 ++++--- src/main.rs | 3 ++- 4 files changed, 9 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bbf891de..c76a360e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,17 +23,6 @@ version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6" -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi 0.1.19", - "libc", - "winapi", -] - [[package]] name = "autocfg" version = "1.1.0" @@ -331,15 +320,6 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" -[[package]] -name = "hermit-abi" -version = "0.1.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", -] - [[package]] name = "hermit-abi" version = "0.2.6" @@ -424,7 +404,7 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aae5bc6e2eb41c9def29a3e0f1306382807764b9b53112030eff57435667352d" dependencies = [ - "hermit-abi 0.2.6", + "hermit-abi", "io-lifetimes 1.0.2", "rustix 0.36.3", "windows-sys 0.42.0", @@ -741,7 +721,6 @@ name = "stgit" version = "2.0.3" dependencies = [ "anyhow", - "atty", "bstr", "bzip2", "chrono", @@ -752,6 +731,7 @@ dependencies = [ "flate2", "git2", "indexmap", + "is-terminal", "serde", "serde_json", "strsim", diff --git a/Cargo.toml b/Cargo.toml index 144e00ed..070c04ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,6 @@ name = "stg" [dependencies] anyhow = "1.0" -atty = "0.2" bstr = { version = "1.0", default-features = false, features = ["std"] } chrono = { version = "0.4", default-features = false, features = ["clock"] } clap = { version = "4.0", default-features = false, features = [ @@ -34,6 +33,7 @@ ctrlc = "3.2" encoding_rs = "0.8" git2 = { version = "0.15", default-features = false } indexmap = "1.8" +is-terminal = "0.4" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" strsim = "0.10" diff --git a/src/color.rs b/src/color.rs index 322e36c6..c7dc9e57 100644 --- a/src/color.rs +++ b/src/color.rs @@ -5,6 +5,7 @@ use std::ffi::OsString; use clap::{Arg, ArgMatches}; +use is_terminal::IsTerminal; use termcolor::StandardStream; pub(crate) fn get_color_arg() -> Arg { @@ -56,7 +57,7 @@ pub(crate) fn termcolor_choice_to_clap(color_choice: termcolor::ColorChoice) -> /// Get [`termcolor::StandardStream`] for stdout based on `--color` option. pub(crate) fn get_color_stdout(matches: &ArgMatches) -> StandardStream { let mut choice = get_color_choice(Some(matches)); - if choice == termcolor::ColorChoice::Auto && atty::isnt(atty::Stream::Stdout) { + if choice == termcolor::ColorChoice::Auto && !std::io::stdout().is_terminal() { choice = termcolor::ColorChoice::Never; } StandardStream::stdout(choice) @@ -65,7 +66,7 @@ pub(crate) fn get_color_stdout(matches: &ArgMatches) -> StandardStream { /// Get [`termcolor::StandardStream`] for stderr based on `--color` option. pub(crate) fn get_color_stderr(matches: &ArgMatches) -> StandardStream { let mut choice = get_color_choice(Some(matches)); - if choice == termcolor::ColorChoice::Auto && atty::isnt(atty::Stream::Stderr) { + if choice == termcolor::ColorChoice::Auto && !std::io::stderr().is_terminal() { choice = termcolor::ColorChoice::Never; } StandardStream::stderr(choice) @@ -85,7 +86,7 @@ pub(crate) fn get_color_choice(maybe_matches: Option<&ArgMatches>) -> termcolor: pub(crate) fn use_color(matches: &ArgMatches) -> bool { match crate::color::get_color_choice(Some(matches)) { termcolor::ColorChoice::Always | termcolor::ColorChoice::AlwaysAnsi => true, - termcolor::ColorChoice::Auto => atty::is(atty::Stream::Stdout), + termcolor::ColorChoice::Auto => std::io::stdout().is_terminal(), termcolor::ColorChoice::Never => false, } } diff --git a/src/main.rs b/src/main.rs index 5f5d8f62..176bc33b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -572,8 +572,9 @@ pub(crate) fn print_warning_message(matches: &ArgMatches, msg: &str) { /// Print user-facing error message to stderr. fn print_error_message(color_choice: Option, err: &anyhow::Error) { + use is_terminal::IsTerminal; let color_choice = color_choice.unwrap_or_else(|| { - if atty::is(atty::Stream::Stderr) { + if std::io::stderr().is_terminal() { termcolor::ColorChoice::Auto } else { termcolor::ColorChoice::Never