|
1 | 1 | # ansi_rgb
|
2 |
| -ANSI escape code colors for `no_std` environments. |
| 2 | +Colorful console text using [ANSI escape sequences](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters). |
| 3 | + |
| 4 | + * Very simple API |
| 5 | + * Full color (using the [`rgb` crate](https://crates.io/crates/rgb)) |
| 6 | + * Colors all the [formatting traits](https://doc.rust-lang.org/std/fmt/#formatting-traits) |
| 7 | + * `no_std` compliant |
| 8 | + |
| 9 | +[](https://crates.io/crates/ansi_rgb)<br/> |
| 10 | +[](https://docs.rs/ansi_rgb)<br/> |
| 11 | +[](https://crates.io/crates/ansi_rgb) |
| 12 | + |
| 13 | +Cargo.toml: |
| 14 | +```toml |
| 15 | +ansi_rgb = "0.2.0" |
| 16 | +``` |
| 17 | + |
| 18 | +# Foreground colors |
| 19 | + |
| 20 | +```rust |
| 21 | +use ansi_rgb::{ Foreground, red }; |
| 22 | + |
| 23 | +println!("{}", "Hello, world!".fg(red())); |
| 24 | +``` |
| 25 | + |
| 26 | +Output: |
| 27 | + |
| 28 | +<code style="color: red">Hello, world!</code> |
| 29 | + |
| 30 | +# Background colors |
| 31 | + |
| 32 | +```rust |
| 33 | +use ansi_rgb::{ Background, red }; |
| 34 | + |
| 35 | +println!("{}", "Hello, world!".bg(red())); |
| 36 | +``` |
| 37 | + |
| 38 | +Output: |
| 39 | + |
| 40 | +<code style="background: red">Hello, world!</code> |
| 41 | + |
| 42 | +# Mix and match |
| 43 | + |
| 44 | +```toml |
| 45 | +# Cargo.toml |
| 46 | +[dependencies] |
| 47 | +rbg = "0.8" |
| 48 | +``` |
| 49 | + |
| 50 | +```rust |
| 51 | +use ansi_rgb::{ Foreground, Background }; |
| 52 | +use rgb::RGB8; |
| 53 | + |
| 54 | +let fg = RGB8::new(123, 231, 111); |
| 55 | +let bg = RGB8::new(10, 100, 20); |
| 56 | +println!("{}", "Yuck".fg(fg).bg(bg)); |
| 57 | +``` |
| 58 | + |
| 59 | +Output: |
| 60 | + |
| 61 | +<code style="color: #7BE76F; background: #0A6414">Yuck</code> |
| 62 | + |
| 63 | +# Anything formattable |
3 | 64 |
|
4 | 65 | ```rust
|
5 |
| -use ansi_rgb::*; |
6 |
| - |
7 |
| -fn main() { |
8 |
| - println!("{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}", |
9 |
| - "Red".fg(red()), |
10 |
| - "Orange".fg(orange()), |
11 |
| - "Yellow".fg(yellow()), |
12 |
| - "Yellow green".fg(yellow_green()), |
13 |
| - "Green".fg(green()), |
14 |
| - "Green cyan".fg(green_cyan()), |
15 |
| - "Cyan".fg(cyan()), |
16 |
| - "Cyan blue".fg(white()).bg(cyan_blue()), |
17 |
| - "Blue".fg(white()).bg(blue()), |
18 |
| - "Blue magenta".fg(white()).bg(blue_magenta()), |
19 |
| - "Magenta".fg(magenta()), |
20 |
| - "Magenta pink".fg(magenta_pink()), |
21 |
| - "Custom color".fg(Rgb::new(123, 231, 111)).bg(Rgb::new(10, 100, 20)) |
22 |
| - ); |
23 |
| -} |
| 66 | +#[derive(Debug)] |
| 67 | +struct Foo(i32, i32); |
| 68 | + |
| 69 | +let foo = Foo(1, 2); |
| 70 | +println!("{:?}", foo.fg(green())); |
24 | 71 | ```
|
25 | 72 |
|
26 |
| - |
| 73 | +Output: |
| 74 | + |
| 75 | +<code style="color: #00FF00">Foo(1, 2)</code> |
| 76 | + |
| 77 | +# Windows users |
| 78 | + |
| 79 | +You need to [set your console mode](https://docs.microsoft.com/en-us/windows/console/console-modes). Otherwise you'll get garbage like this: |
27 | 80 |
|
28 |
| -Context here: https://github.com/phil-opp/blog_os/issues/603 |
| 81 | +`�[48;2;159;114;0m �[0m` |
0 commit comments