3

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

Shepmaster
  • 326,504
  • 69
  • 892
  • 1,159
Narigo
  • 2,719
  • 3
  • 18
  • 29
  • 8
    To rephrase this: you want to check whether `stdout` is a TTY. You might want to check the `isatty` function or a crate wrapping it for compatibility, eg. https://crates.io/crates/atty. – SirDarius Jan 02 '19 at 22:15

1 Answers1

7

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.

Nickolay
  • 29,618
  • 12
  • 101
  • 174