๐Ÿ“ฆ jgardona / ansistream

Write blazingly fast, free allocation ansi escape codes to a buffer.

โ˜… 1 stars โ‘‚ 0 forks ๐Ÿ‘ 1 watching โš–๏ธ MIT License
ansiansi-colorsansi-escape-codesbufferstream
๐Ÿ“ฅ Clone https://github.com/jgardona/ansistream.git
HTTPS git clone https://github.com/jgardona/ansistream.git
SSH git clone git@github.com:jgardona/ansistream.git
CLI gh repo clone jgardona/ansistream
jgardona jgardona Merge commit '69eb2d6b4987751fae386b673dc6efe06f93518b' e23b42a 2 years ago ๐Ÿ“ History
๐Ÿ“‚ main View all commits โ†’
๐Ÿ“ .github
๐Ÿ“ examples
๐Ÿ“ images
๐Ÿ“ src
๐Ÿ“ tests
๐Ÿ“„ .gitignore
๐Ÿ“„ Cargo.toml
๐Ÿ“„ LICENSE
๐Ÿ“„ README.md
๐Ÿ“„ README.md

AnsiStream

Crates.io Crates.io Downloads Rust Stable License GitHub Actions Workflow Status

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.

ParameterEffect
0reset all SGR effects to their default
1bold or increased intensity
2faint or decreased insensity
4singly underlined
5slow blink
30-37foreground color (3/4 bit)
38;5;xforeground color (256 colors, non-standard)
38;2;r;g;bforeground color (RGB, non-standard)
40-47background color (8 colors)
48;5;xbackground color (256 colors, non-standard)
48;2;r;g;bbackground color (RGB, non-standard)
90-97bright foreground color (non-standard)
100-107bright background color (non-standard)
  • Below example will print a red underlined text.
sgi

Usage

  • Add the AnsiStream crate to your Cargo.toml
$ cargo add ansistream

  • Initialize a buffer and write a simple string in it
// 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

  • Write the stream in stdout
let stdout = io::stdout().lock();

let mut astream = ansistream::AnsiScapeStream::new(stdout);
astream.write_string("simple text")?;

astream.flush()?;

  • Writing a green foreground text to stream
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()
);

  • Write formatted color output
let mut astream = AnsiEscapeStream::new(writer);

  for i in 100..=107 {
      astream.write_text_color_fmt(FC_LIGHT_GRAY, i, format_args!("{i:>5} "))?;
  }

Examples

  • 16color example
256color

$ 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

  • 256color example
256color

$ 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

  • truecolor example
truecolor

$ 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

Status

### Escape codes available

FinnishedType
xColor and Style Escape Codes
Screen and Cursor Escape Codes

References

[Ansi Escape Codes