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

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
}))
}