Print colored text to the terminal

colored terminal ANSI

Using the colored crate it is super easy to print text on the terminal using colored text.

Dependency

examples/colored-terminal/Cargo.toml

[package]
name = "colored-terminal"
version = "0.1.0"
edition = "2021"

[dependencies]
colored = "2.1.0"

Code

examples/colored-terminal/src/main.rs

use colored::Colorize;

fn main() {
    let text = "Hello, world!";
    println!("{}", text);
    println!("{}", text.red());
    println!("{}", text.red().bold());
    println!("{}", text.blue().bold());
    println!("{}", text.truecolor(0, 136, 136));
    println!("{}", text.white().bold().on_black());
}

Output

Windows

In the source code of ruff I saw the following lines in the main function:

    // Enabled ANSI colors on Windows 10.
    #[cfg(windows)]
    assert!(colored::control::set_virtual_terminal(true).is_ok());

I only tried this crate on Ubuntu Linux.

Author

Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo