rustpipetty

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


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


Solution

  • As of Rust 1.70, std::io::IsTerminal is available:

    use std::io::IsTerminal;
    
    fn main() {
        dbg!(std::io::stdout().is_terminal());
    }
    

    Alternatively, crossterm has a is_tty() method, and there's also the is-terminal crate (a maintained fork of atty).