Write blazingly fast, free allocation ansi escape codes to a buffer.
https://github.com/jgardona/ansistream.git
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.
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) |
$ cargo add ansistream
// initialize a ansi stream
let output = Cursor::new(Vec::<u8>::new());
let mut astream = ansistream::AnsiScapeStream::new(output);
// write a simple string in buffer
astream.write_string("the quick brown fox jumps over the lazy dog")?;
// data will be flushed when astream drop or gets flushed
let stdout = io::stdout().lock();
let mut astream = ansistream::AnsiScapeStream::new(stdout);
astream.write_string("simple text")?;
astream.flush()?;
let mut astream = AnsiEscapeStream::new(writer);
astream.write_text_fc_fmt(FCGREEN, format_args!("123")).unwrap();
// asserts that fcgreen was writed and also reseted with fcdefault
assert_eq!(
&[0x1b, 0x5b, 0x33, 0x32, 0x6d, 0x31, 0x32, 0x33, 0x1b, 0x5b, 0x33, 0x39, 0x6d],
astream.buffer()
);
let mut astream = AnsiEscapeStream::new(writer);
for i in 100..=107 {
astream.write_text_color_fmt(FC_LIGHT_GRAY, i, format_args!("{i:>5} "))?;
}
$ hyperfine --warmup 100 '16color'
Benchmark 1: 16color
Time (mean ยฑ ฯ): 10.9 ms ยฑ 0.4 ms [User: 5.7 ms, System: 9.3 ms]
Range (min โฆ max): 10.3 ms โฆ 12.2 ms 133 runs
$ hyperfine --warmup 100 '256color'
Benchmark 1: 256color
Time (mean ยฑ ฯ): 11.3 ms ยฑ 0.4 ms [User: 4.9 ms, System: 9.3 ms]
Range (min โฆ max): 10.7 ms โฆ 12.7 ms 130 runs
$ hyperfine --warmup 100 'truecolor'
Benchmark 1: truecolor
Time (mean ยฑ ฯ): 11.2 ms ยฑ 0.5 ms [User: 5.4 ms, System: 9.2 ms]
Range (min โฆ max): 10.4 ms โฆ 13.0 ms 131 runs
### Escape codes available
| Finnished | Type |
|---|---|
| x | Color and Style Escape Codes |
| Screen and Cursor Escape Codes |