Expand description
ansi: ANSI/VT terminal output as data, on the Rust standard library alone. Zero dependencies.
Three tightly-related capabilities, all pure bytes in / bytes out:
- Build styled output:
Style+Colorrender to SGR escape sequences, absolutely (Style::sgr) or as a minimal transition from a previous style (Style::transition_to). - Parse a terminal byte stream incrementally:
Parser::feedaccepts chunks split at any byte boundary (mid-escape, mid-UTF-8) and yieldsTokens (text runs, C0 controls, CSI/ESC/OSC sequences). - Render by diff:
Screenis a grid of styledCells;Screen::diffemits the minimal cursor-move/SGR/text bytes that turn one screen into another, andScreen::render_fullrepaints from scratch.
use ansi::{Color, Style};
let style = Style { bold: true, fg: Color::Indexed(2), ..Style::default() };
assert_eq!(style.sgr(), "\x1b[0;1;32m");
let mut p = ansi::Parser::new();
let tokens = p.feed(b"hi\x1b[1;31mred");
assert_eq!(tokens.len(), 3); // Text("hi"), Csi(SGR 1;31), Text("red")There is no I/O here: no raw mode, no stdin/stdout, no PTY. Those are the
rawterm and pty crates’ concerns; this crate turns bytes into meaning
and meaning into bytes.
Structs§
- Cell
- One character cell: a
char, itsStyle, and its displaywidthin terminal columns. The default cell is a single-width space in the default style. - Parser
- The incremental parser. Create one per stream and call
feedwith each chunk as it arrives. - Screen
- A
rows x colsgrid ofCells plus a cursor position, both 0-based. - Style
- A complete character style: foreground, background, and attributes.