Ansi Stream
Write blazingly fast, free allocation ansi escape codes to a buffer, and flushes them all to any output stream. Supports 8/16 colors, 256 colors, RGB color rendering output.
ANSI Escape Codes for Terminal Graphics
The ANSI escape code standard, formally adopted as ISO/IEC 6429, defines a series of control sequences. Each control sequence begins with a Control Sequence Introducer (CSI), defined as a scape character followed immediately by a bracket: ESC[. In particular, a CSI followed by a certain number of "parameter bytes" (ASCII 0-9:; <=>?) then the letter m forms a control sequence known as Select Graphic Rendition (SGR). If no parameter bytes are explicitly given, then it is assumed to be 0. SGR parameters can be chained together with a semicolon ; as delimiter.
Some common SGR parameters are shown below.
| Parameter | Effect |
|---|---|
| 0 | reset all SGR effects to their default |
| 1 | bold or increased intensity |
| 2 | faint or decreased insensity |
| 4 | singly underlined |
| 5 | slow blink |
| 30-37 | foreground color (3/4 bit) |
| 38;5;x | foreground color (256 colors, non-standard) |
| 38;2;r;g;b | foreground color (RGB, non-standard) |
| 40-47 | background color (8 colors) |
| 48;5;x | background color (256 colors, non-standard) |
| 48;2;r;g;b | background color (RGB, non-standard) |
| 90-97 | bright foreground color (non-standard) |
| 100-107 | bright background color (non-standard) |
- Below example will print a red underlined text.

Usage
- Add the AnsiStream crate to your Cargo.toml
$ cargo add ansistream
- Initialize a buffer and write a simple string in it
// initialize an internal buffer with 30 bytes
let mut astream = new;
// write a simple string in buffer
astream.write_string?;
- Write the stream in stdout
let mut stdout = stdout.lock;
astream.write_string?;
// AnsiScapeStream impl DerefMut
flush?;
- Writing a green foreground text to stream
let mut astream = default;
astream.write_text_fc.unwrap;
// asserts that fcred was writed and also reseted with fcdefault
assert_eq!;
Examples
- 16color example

❯ hyperfine.exe --warmup 100 '.\16color.exe'
Benchmark 1: .\16color.exe
Time (mean ± σ): 27.2 ms ± 1.0 ms [User: 10.7 ms, System: 16.3 ms]
Range (min … max): 26.1 ms … 31.9 ms 52 runs
- 256color example

❯ hyperfine.exe --warmup 100 '.\256color.exe'
Benchmark 1: .\256color.exe
Time (mean ± σ): 29.0 ms ± 2.7 ms [User: 8.4 ms, System: 17.2 ms]
Range (min … max): 25.8 ms … 40.6 ms 41 runs
- truecolor example

❯ hyperfine.exe --warmup 100 '.\truecolor.exe'
Benchmark 1: .\truecolor.exe
Time (mean ± σ): 28.5 ms ± 1.2 ms [User: 7.9 ms, System: 17.8 ms]
Range (min … max): 26.9 ms … 32.5 ms 51 runs