Expand description

Enable ANSI code support on Windows 10 and above.

This crate provides one function, enable_ansi_support, which allows ANSI escape codes to work on Windows 10 and above.

Call enable_ansi_support once, early on in main(), to enable ANSI escape codes generated by crates like ansi_term or owo-colors to work on Windows just like they do on Unix platforms.

Examples

fn main() {
    match enable_ansi_support::enable_ansi_support() {
        Ok(()) => {
            // ANSI escape codes were successfully enabled, or this is a non-Windows platform.
            println!("\x1b[31mHello, world\x1b[0m");
        }
        Err(_) => {
            // The operation was unsuccessful, typically because it's running on an older
            // version of Windows. The program may choose to disable ANSI color code output in
            // this case.
        }
    }

    // Use your terminal color library of choice here.
}

How it works

enable_ansi_support uses Windows API calls to alter the properties of the console that the program is running in. See the Windows documentation for more information.

On non-Windows platforms, enable_ansi_support is a no-op.

Functions

Enables ANSI code support on Windows 10.