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.
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) |
§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§
- Ansi
Escape Stream - 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