Is there a cross-platform way to check if stdout is being piped into another program in Rust?

Viewed 1409

I'd like to disable colors when the output is piped somewhere else than a terminal.

1 Answers

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.

Related