Skip to main content

Use std::io::IsTerminal to detect if you’re running in the terminal in Rust

This allows me to suppress ANSI escape codes if the output is going somewhere other than the terminal.

In my dominant_colours tool, I use ANSI escape sequences to print coloured text to the console. I to detect if my code was running in the terminal, or if it was running in a non-terminal environment where those escape codes would be unhelpful (e.g. being redirected to a file).

I found a Stack Overflow answer that suggested using std::io::IsTerminal, which worked perfectly. Here’s a simple example:

use std::io::IsTerminal;

fn main() {
    if std::io::stdout().is_terminal() {
        println!("This text is \x1b[0;31mred\x1b[0m and \x1b[0;34mblue\x1b[0m");
    } else {
        println!("This text is not coloured");
    }
}

I added this to dominant_colours in pull request #65.