Skip to content

Commit

Permalink
refactor: Use is-terminal instead of atty
Browse files Browse the repository at this point in the history
This follows suit with clap, which also changed to is-terminal, to
eliminate a redundant dependency and perhaps have a better maintained
library.

Refs: clap-rs/clap#4249
  • Loading branch information
jpgrayson committed Nov 28, 2022
1 parent ac99566 commit ed5a825
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 27 deletions.
24 changes: 2 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -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 = [
Expand All @@ -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"
Expand Down
7 changes: 4 additions & 3 deletions src/color.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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,
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Expand Up @@ -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<termcolor::ColorChoice>, 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
Expand Down

0 comments on commit ed5a825

Please sign in to comment.