forked from crate-ci/typos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This supports - Basic capability detection - NO_COLOR env variable - tty detection - CLI overrides This does not yet support CLICOLOR. I'll be trying to upstream all of this into `yansi` and get it taken care of there. This only supports Windows Anniversary edition and later which I think is a fine compromise due to the ergonomic difference between `yansi` and `termcolor`. Fixes #30
- Loading branch information
Ed Page
committed
May 12, 2021
1 parent
b9d1f9d
commit fa38ad4
Showing
6 changed files
with
270 additions
and
60 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use structopt::StructOpt; | ||
|
||
#[derive(Debug, StructOpt)] | ||
#[structopt(rename_all = "kebab-case")] | ||
pub struct ColorArgs { | ||
/// "Specify when to use colored output. The automatic mode | ||
/// only enables colors if an interactive terminal is detected - | ||
/// colors are automatically disabled if the output goes to a pipe. | ||
/// | ||
/// Possible values: *auto*, never, always. | ||
#[structopt( | ||
long, | ||
value_name="when", | ||
possible_values(&ColorValue::variants()), | ||
case_insensitive(true), | ||
default_value("auto"), | ||
hide_possible_values(true), | ||
hide_default_value(true), | ||
help="When to use colors (*auto*, never, always).")] | ||
color: ColorValue, | ||
} | ||
|
||
impl ColorArgs { | ||
pub fn colored(&self) -> Option<bool> { | ||
self.color.colored() | ||
} | ||
} | ||
|
||
arg_enum! { | ||
#[derive(Debug, Copy, Clone, PartialEq, Eq)] | ||
pub enum ColorValue { | ||
Always, | ||
Never, | ||
Auto, | ||
} | ||
} | ||
|
||
impl ColorValue { | ||
fn colored(self) -> Option<bool> { | ||
match self { | ||
ColorValue::Always => Some(true), | ||
ColorValue::Never => Some(false), | ||
ColorValue::Auto => None, | ||
} | ||
} | ||
} | ||
|
||
impl Default for ColorValue { | ||
fn default() -> Self { | ||
ColorValue::Auto | ||
} | ||
} | ||
|
||
pub fn colored_stdout() -> Option<bool> { | ||
if atty::is(atty::Stream::Stdout) { | ||
None | ||
} else { | ||
Some(false) | ||
} | ||
} | ||
|
||
pub fn colored_stderr() -> Option<bool> { | ||
if atty::is(atty::Stream::Stderr) { | ||
None | ||
} else { | ||
Some(false) | ||
} | ||
} | ||
|
||
pub fn colored_env() -> Option<bool> { | ||
match std::env::var_os("TERM") { | ||
None => noterm_colored(), | ||
Some(k) => { | ||
if k == "dumb" { | ||
Some(false) | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
.or_else(|| { | ||
// See https://no-color.org/ | ||
std::env::var_os("NO_COLOR").map(|_| true) | ||
}) | ||
} | ||
|
||
#[cfg(not(windows))] | ||
fn noterm_colored() -> Option<bool> { | ||
// If TERM isn't set, then we are in a weird environment that | ||
// probably doesn't support colors. | ||
Some(false) | ||
} | ||
|
||
#[cfg(windows)] | ||
fn noterm_colored() -> Option<bool> { | ||
// On Windows, if TERM isn't set, then we shouldn't automatically | ||
// assume that colors aren't allowed. This is unlike Unix environments | ||
// where TERM is more rigorously set. | ||
None | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.