Crate ansistream

Source
Expand description

§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.

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)

§Examples

  • Write FC_RED attribute to stream
use ansistream::{FC_RED, AnsiEscapeStream};
use std::io::Cursor;
 
let buffer = Cursor::new(Vec::<u8>::new());
let mut astream = AnsiEscapeStream::new(buffer);
astream.write_attribute(FC_RED).unwrap();
// fcred escape code
assert_eq!(&[0x1b, 0x5b, 0x33, 0x31, 0x6d], astream.buffer());
  • Reset an attribute in stream
use ansistream::{FC_RED, AnsiEscapeStream};
use std::io::Cursor;

let buffer = Cursor::new(Vec::<u8>::new());
let mut astream = AnsiEscapeStream::new(buffer);
astream.reset_attribute(FC_RED).unwrap();
// fcred escape code
assert_eq!(&[0x1b, 0x5b, 0x33, 0x39, 0x6d], astream.buffer());
  • Write formatted foreground green color text to stream.
use ansistream::{FC_GREEN, AnsiEscapeStream};
use std::io::Cursor;

let buffer = Cursor::new(Vec::<u8>::new());
let mut astream = AnsiEscapeStream::new(buffer);
astream
    .write_text_fc_fmt(FC_GREEN, format_args!("123"))
    .unwrap();
// asserts that fcred was writed and also reseted with fcdefault
assert_eq!(
    &[0x1b, 0x5b, 0x33, 0x32, 0x6d, 0x31, 0x32, 0x33, 0x1b, 0x5b, 0x33, 0x39, 0x6d],
    astream.buffer()
);

Structs§

AnsiEscapeStream
Data structure used to do fast ansi escape write operations. It implements many methods and traits which makes easier to format text. An internal buffer can be preallocated, which avoids allocation using write operations.

Constants§

BC_BLACK
Background colors.
BC_BLUE
BC_BROWN
BC_CYAN
BC_DARK_GRAY
BC_DEFAULT
BC_GREEN
BC_LIGHT_BLUE
BC_LIGHT_CYAN
BC_LIGHT_GRAY
BC_LIGHT_GREEN
BC_LIGHT_MAGENTA
BC_LIGHT_RED
BC_MAGENTA
BC_RED
BC_RICH_COLORS
BC_WHITE
BC_YELLOW
FC_BLACK
Foreground colors.
FC_BLUE
FC_BROWN
FC_CYAN
FC_DARK_GRAY
FC_DEFAULT
FC_GREEN
FC_LIGHT_BLUE
FC_LIGHT_CYAN
FC_LIGHT_GRAY
FC_LIGHT_GREEN
FC_LIGHT_MAGENTA
FC_LIGHT_RED
FC_MAGENTA
FC_RED
FC_RICH_COLORS
FC_WHITE
FC_YELLOW
TS_BLINK
TS_BOLD
TS_DEFAULT
TS_DIM
TS_HIDDEN
TS_INVERT
TS_ITALIC
TS_NO_BLINK
TS_NO_BOLD
TS_NO_DIM
TS_NO_HIDDEN
TS_NO_INVERT
TS_NO_ITALIC
TS_NO_OVERLINE
TS_NO_STRIKE
TS_NO_UNDERLINE
TS_OVERLINE
TS_RESET_ALL
Text Styles
TS_STRIKE
TS_UNDERLINE