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.