Colorful terminal text using ANSI escape sequences.
no_std compliant```rust use ansi_rgb::{ Colorable, red };
println!("{}", "Hello, world!".fg(red())); ```
Output:
Hello, world!
```rust use ansi_rgb::{ Colorable, red };
println!("{}", "Hello, world!".bg(red())); ```
Output:
Hello, world!
```rust use ansi_rgb::{ Colorable, blue, green, red };
let formatted = format!( "Hello, world! {}", format!( "{} is an interesting {}", "This".fg(blue()), "day".fg(red()) ).bg(green()) );
println!("{}", formatted);
```
Output:
Hello, world! This is an interesting day
```rust use ansi_rgb::*;
struct Foo(i32, i32);
let foo = Foo(1, 2); println!("{:?}", foo.fg(green())); ```
Output:
Foo(1, 2)
```rust use ansi_rgb::{ Colorable, Color3 };
println!("{}", "Hello, world!".fg(Color3::RED).bg(Color3::BLACK)); ```
Output:
Hello, world!
```rust use ansi_rgb::{ Colorable, Color4 };
println!("{}", "Hello, world!".fg(Color4::BRIGHT_RED).bg(Color4::BLACK)); ```
Output:
Hello, world!
```rust use ansi_rgb::{ Colorable, Color8 };
println!("{}", "Hello, world!".fg(Color8::new(160)).bg(Color8::new(0))); ```
Output:
Hello, world!
Built-in support for the rgb crate.
```toml
[dependencies] rgb = { version = "0.8", default-features = false } ```
```rust use ansi_rgb::{ Colorable }; use rgb::RGB8;
let fg = RGB8::new(123, 231, 111); let bg = RGB8::new(10, 100, 20); println!("{}", "Yuck".fg(fg).bg(bg)); ```
Output:
Yuck
If you have your own color type and you know how to turn it into ANSI escape
sequences then just implement FormatColor:
```rust use ansi_rgb::{ Canvas, Colorable, FormatColor }; use core::fmt;
enum FavoriteColors { SkyBlue, RocketPlumeYellow, TitaniumGray }
impl FormatColor for FavoriteColors { fn prelude(&self, f: &mut fmt::Formatter, canvas: Canvas) -> fmt::Result { let (r, g, b) = match self { FavoriteColors::SkyBlue => (135, 206, 235), FavoriteColors::RocketPlumeYellow => (255, 255, 0), FavoriteColors::TitaniumGray => (86, 95, 107) }; write!( f, "\x1B[{};2;{};{};{}m", match canvas { Canvas::Foreground => 38, Canvas::Background => 48 }, r, g, b ) } }
println!( "The sky is {}", "blue".fg(FavoriteColors::SkyBlue) );
```
Output:
The sky is blue
default includes 3-, 4-, 8-, and 24-bit colors and depends on the rgb crate,
giving you the following things:
rgb crateFormatColor for rgb::RGB8 typeColor3 enum and its implementation of FormatColorColor4 struct and its implementation of FormatColorColor8 struct and its implementation of FormatColorred(), orange(), etc)You need to set your console mode. Otherwise you'll get garbage like this:
�[48;2;159;114;0m �[0m