Expand description
Convert ANSI color and style codes into Ratatui Text.
This crate parses bytes containing ANSI SGR escape sequences (like \x1b[31m).
It produces a Ratatui Text with equivalent foreground/background Color and
Modifier settings via Style.
Unknown or malformed escape sequences are ignored, so you can feed it real terminal output without having to pre-clean it.
§Features
- UTF-8 decoding via
String::from_utf8(default) orsimdutf8(simdfeature). - SGR styles such as bold, italic, underline, and strikethrough.
- Colors: named (3/4-bit, 8/16-color), indexed (8-bit, 256-color), and truecolor (24-bit RGB).
- Optional
zero-copyAPI that borrows from the input.
§Supported Color Codes
| Color Mode | Supported | SGR Example | Ratatui Color Example |
|---|---|---|---|
| Named (3/4-bit, 8/16-color) | ✓ | \x1b[30..37;40..47m | Color::Blue |
| Indexed (8-bit, 256-color) | ✓ | \x1b[38;5;<N>m | Color::Indexed(1) |
| Truecolor (24-bit RGB) | ✓ | \x1b[38;2;<R>;<G>;<B>m | Color::Rgb(255, 0, 0) |
The SGR examples above set the foreground color (38). For background colors, replace 38
with 48 (for example, \x1b[48;5;<N>m and \x1b[48;2;<R>;<G>;<B>m).
§Example
The input type implements AsRef<[u8]>, so it is not consumed.
use ansi_to_tui::IntoText as _;
let bytes = b"\x1b[38;2;225;192;203mAAAAA\x1b[0m".to_vec();
let text = bytes.into_text()?;Parsing from a file.
use ansi_to_tui::IntoText as _;
let buffer = std::fs::read("ascii/text.ascii")?;
let text = buffer.into_text()?;Enums§
- Error
- Errors returned by this crate.