use 'atty' to detect when colors can be used

This commit is contained in:
Dustin J. Mitchell 2020-12-20 19:54:38 -05:00
parent 7d17740ca8
commit 8ba6277cce
3 changed files with 12 additions and 1 deletions

1
Cargo.lock generated
View file

@ -2264,6 +2264,7 @@ name = "taskchampion-cli"
version = "0.2.0"
dependencies = [
"assert_cmd",
"atty",
"config",
"dirs 3.0.1",
"env_logger",

View file

@ -13,6 +13,7 @@ nom = "*"
prettytable-rs = "^0.8.0"
textwrap = "0.12.1"
termcolor = "1.1.2"
atty = "0.2.14"
[dependencies.config]
default-features = false

View file

@ -18,7 +18,7 @@ pub(crate) fn invoke(command: Command, settings: Config) -> Fallible<()> {
log::debug!("command: {:?}", command);
log::debug!("settings: {:?}", settings);
let mut w = StandardStream::stdout(ColorChoice::Auto);
let mut w = get_writer()?;
// This function examines the command and breaks out the necessary bits to call one of the
// `execute` functions in a submodule of `cmd`.
@ -110,3 +110,12 @@ fn get_server(settings: &Config) -> Fallible<Box<dyn server::Server>> {
client_id,
})?)
}
/// Get a WriteColor implementation based on whether the output is a tty.
fn get_writer() -> Fallible<StandardStream> {
Ok(StandardStream::stdout(if atty::is(atty::Stream::Stdout) {
ColorChoice::Auto
} else {
ColorChoice::Never
}))
}