diff --git a/Cargo.lock b/Cargo.lock index e6959a2c..9ac3f9b2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1357,6 +1357,7 @@ version = "0.5.4" dependencies = [ "ansi_term 0.12.1", "anyhow", + "atty", "casual", "clap", "fs2", diff --git a/Cargo.toml b/Cargo.toml index da25fc24..e07b3783 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ include = ["src/**/*", "LICENSE-*", "README.md", "build.rs"] [dependencies] ansi_term = "0.12.1" anyhow = "1.0.32" +atty = "0.2.14" casual = "0.1.2" clap = { version = "3.0.0-beta.2", features = ["derive"] } fs2 = "0.4.3" diff --git a/src/cli.rs b/src/cli.rs index 6a0b0511..7788f8e1 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -23,6 +23,8 @@ use crate::log::{Output, Verbosity}; pub enum ColorChoice { /// Force color output. Always, + /// Intelligently guess whether to use color output. + Auto, /// Force disable color output. Never, } @@ -156,7 +158,7 @@ struct RawOpt { #[clap(long, short)] verbose: bool, - /// Output coloring: always or never. + /// Output coloring: always, auto, or never. #[clap(long, value_name = "WHEN", default_value)] color: ColorChoice, @@ -223,8 +225,7 @@ pub struct Opt { impl Default for ColorChoice { fn default() -> Self { - // FIXME: default to `Self::Auto` and detect if stdout and stderr is a TTY. - Self::Always + Self::Auto } } @@ -232,6 +233,7 @@ impl fmt::Display for ColorChoice { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Self::Always => f.write_str("always"), + Self::Auto => f.write_str("auto"), Self::Never => f.write_str("never"), } } @@ -247,6 +249,7 @@ impl FromStr for ColorChoice { fn from_str(s: &str) -> result::Result { match s { "always" => Ok(Self::Always), + "auto" => Ok(Self::Auto), "never" => Ok(Self::Never), s => Err(ParseColorChoiceError(s.to_string())), } @@ -257,6 +260,7 @@ impl ColorChoice { fn is_no_color(self) -> bool { match self { Self::Always => false, + Self::Auto => !atty::is(atty::Stream::Stderr), Self::Never => true, } } @@ -520,7 +524,7 @@ FLAGS: -V, --version Prints version information OPTIONS: - --color Output coloring: always or never [default: always] + --color Output coloring: always, auto, or never [default: auto] --config-dir The configuration directory [env: SHELDON_CONFIG_DIR=] --data-dir The data directory [env: SHELDON_DATA_DIR=] --config-file The config file [env: SHELDON_CONFIG_FILE=] diff --git a/tests/lib.rs b/tests/lib.rs index 11e12130..31a6b054 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -86,9 +86,7 @@ impl TestCommand { .env_remove("SHELDON_CLONE_DIR") .env_remove("SHELDON_DOWNLOAD_DIR") .args(¶ms) - .arg("--verbose") - .arg("--color") - .arg("never"); + .arg("--verbose"); Self { command,