I'd like to disable colors when the output is piped somewhere else than a terminal.
I'd like to disable colors when the output is piped somewhere else than a terminal.
Translated into the POSIX language, your question would be: "is stdout not a TTY", so the answer on *nix can be obtained by !isatty(STDOUT_FILENO). The libc crate can be used to call this from Rust.
On Windows, it's complicated, so you're better off using the atty crate.
[edit] You can use the atty crate on Linux as well, making it a convenient solution for cross-platform programs.