Skip to content

Commit

Permalink
Auto-detect whether to use colored output
Browse files Browse the repository at this point in the history
This adds a new `--color auto` option. By default Sheldon will now check
whether stderr is a TTY before using colored output. You can still force
Sheldon to always use color or never use color with the `--color always`
option or `--color never`.
  • Loading branch information
rossmacarthur committed Oct 15, 2020
1 parent 8e7bdef commit 2be1da7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 8 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,

Expand Down Expand Up @@ -223,15 +225,15 @@ 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
}
}

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"),
}
}
Expand All @@ -247,6 +249,7 @@ impl FromStr for ColorChoice {
fn from_str(s: &str) -> result::Result<Self, Self::Err> {
match s {
"always" => Ok(Self::Always),
"auto" => Ok(Self::Auto),
"never" => Ok(Self::Never),
s => Err(ParseColorChoiceError(s.to_string())),
}
Expand All @@ -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,
}
}
Expand Down Expand Up @@ -520,7 +524,7 @@ FLAGS:
-V, --version Prints version information
OPTIONS:
--color <WHEN> Output coloring: always or never [default: always]
--color <WHEN> Output coloring: always, auto, or never [default: auto]
--config-dir <PATH> The configuration directory [env: SHELDON_CONFIG_DIR=]
--data-dir <PATH> The data directory [env: SHELDON_DATA_DIR=]
--config-file <PATH> The config file [env: SHELDON_CONFIG_FILE=]
Expand Down
4 changes: 1 addition & 3 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ impl TestCommand {
.env_remove("SHELDON_CLONE_DIR")
.env_remove("SHELDON_DOWNLOAD_DIR")
.args(&params)
.arg("--verbose")
.arg("--color")
.arg("never");
.arg("--verbose");

Self {
command,
Expand Down

0 comments on commit 2be1da7

Please sign in to comment.