#ansi #color #rgb #no-std #terminal

no-std ansi_rgb

Colorful terminal text using ANSI escape sequences

6 releases

Uses new Rust 2021

0.3.2-alpha Jun 25, 2022
0.3.1 Jun 24, 2022
0.2.0 Jul 18, 2019
0.1.0 Jul 16, 2019

#96 in Command-line interface

Download history 688/week @ 2022-10-07 489/week @ 2022-10-14 616/week @ 2022-10-21 626/week @ 2022-10-28 812/week @ 2022-11-04 887/week @ 2022-11-11 1044/week @ 2022-11-18 845/week @ 2022-11-25 506/week @ 2022-12-02 570/week @ 2022-12-09 572/week @ 2022-12-16 322/week @ 2022-12-23 851/week @ 2022-12-30 1373/week @ 2023-01-06 724/week @ 2023-01-13 1500/week @ 2023-01-20

4,549 downloads per month
Used in 6 crates (2 directly)

MIT license

16KB
248 lines

ansi_rgb

Colorful terminal text using ANSI escape sequences.

  • Very simple API
  • 3-, 4-, 8-, and 24-bit colors
  • Colors all the formatting traits
  • Easy to add your own color types
  • no_std compliant

crates.io badge
docs.rs badge
Downloads badge

Full documentation:

https://docs.rs/ansi_rgb


lib.rs:

Foreground colors

use ansi_rgb::{ Colorable, red };

println!("{}", "Hello, world!".fg(red()));

Output:

Hello, world!

Background colors

use ansi_rgb::{ Colorable, red };

println!("{}", "Hello, world!".bg(red()));

Output:

Hello, world!

Nesting

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);
# assert_eq!(
#     "Hello, world! \u{1b}[48;2;0;255;0m\u{1b}[38;2;0;0;255mThis\u{1b}[39m is an interesting \u{1b}[38;2;255;0;0mday\u{1b}[39m\u{1b}[49m",
#     formatted
# )

Output:

Hello, world! This is an interesting day

Anything formattable

use ansi_rgb::*;

#[derive(Debug)]
struct Foo(i32, i32);

let foo = Foo(1, 2);
println!("{:?}", foo.fg(green()));

Output:

Foo(1, 2)

3-bit colors

use ansi_rgb::{ Colorable, Color3 };

println!("{}", "Hello, world!".fg(Color3::RED).bg(Color3::BLACK));

Output:

Hello, world!

4-bit colors

use ansi_rgb::{ Colorable, Color4 };

println!("{}", "Hello, world!".fg(Color4::BRIGHT_RED).bg(Color4::BLACK));

Output:

Hello, world!

8-bit colors

use ansi_rgb::{ Colorable, Color8 };

println!("{}", "Hello, world!".fg(Color8::new(160)).bg(Color8::new(0)));

Output:

Hello, world!

24-bit colors

Built-in support for the rgb crate.

# Cargo.toml
[dependencies]
rgb = { version = "0.8", default-features = false }
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

Extending to other color types

If you have your own color type and you know how to turn it into ANSI escape sequences then just implement FormatColor:

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)
);
# assert_eq!("The sky is \x1B[38;2;135;206;235mblue\x1B[39m", format!("The sky is {}", "blue".fg(FavoriteColors::SkyBlue)))

Output:

The sky is blue

Features

default includes 3-, 4-, 8-, and 24-bit colors and depends on the rgb crate, giving you the following things:

  • Dependency on rgb crate
  • Implementation of FormatColor for rgb::RGB8 type
  • Color3 enum and its implementation of FormatColor
  • Color4 struct and its implementation of FormatColor
  • Color8 struct and its implementation of FormatColor
  • Color functions (red(), orange(), etc)

Windows users

You need to set your console mode. Otherwise you'll get garbage like this:

[48;2;159;114;0m �[0m

Dependencies

~73KB