| Crates.io | ansi_escapers |
| lib.rs | ansi_escapers |
| version | 0.2.0 |
| created_at | 2025-08-26 02:29:24.58029+00 |
| updated_at | 2025-08-26 03:59:38.577446+00 |
| description | A Rust library for ANSI escape code parsing and manipulation. |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1810423 |
| size | 73,037 |
ANSIEscapeRS is a Rust library for generating, parsing, and working with ANSI escape codes. It provides a type-safe, extensible API for producing and interpreting ANSI codes for text formatting, color, cursor movement, and terminal control, with a focus on making invalid states unrepresentable.
ansi_creator (accessed via crate root)AnsiEnvironment: Detects terminal support for ANSI, truecolor, and 8-bit color.AnsiCreator: Main struct for formatting text, generating SGR (Select Graphic Rendition) codes, cursor movement, erase, and device control codes.use ansi_escapers::{creator, SgrAttribute, Color};
let creator = creator::AnsiCreator::new();
let bold_red = creator.format_text(
"Hello",
&[SgrAttribute::Bold, SgrAttribute::Foreground(Color::Red)]
);
println!("{}", bold_red);
interpreter (accessed via ansi_escapers::interpreter)AnsiSpan: Represents a span of text affected by an ANSI code.AnsiPoint: Represents a point event (e.g., cursor move).AnsiParseResult: Contains cleaned text, spans, and points.AnsiParser: State machine for parsing ANSI codes.use ansi_escapers::interpreter::AnsiParser;
let mut parser = AnsiParser::new("\x1b[31mRed\x1b[0m Normal");
let result = parser.parse_annotated();
println!("{:?}", result.spans);
ansi_types (accessed via crate root)SgrAttribute: Bold, Italic, Underline, Foreground/Background/UnderlineColor, etc.Color: Standard, bright, 8-bit, and 24-bit RGB colors.CursorMove, Erase, EraseMode, DeviceControl, AnsiEscape: All major ANSI command types.Add to your Cargo.toml:
[dependencies]
ansi_escapers = "0.1.0"
Import and use in your Rust code (all main types are available from the crate root):
use ansi_escapers::{creator, SgrAttribute, Color};
let creator = creator::AnsiCreator::new();
let styled = creator.format_text(
"Hello, world!",
&[SgrAttribute::Bold, SgrAttribute::Foreground(Color::Blue)]
);
println!("{}", styled);
The library can detect terminal capabilities:
use ansi_escapers::AnsiEnvironment;
let env = AnsiEnvironment::detect();
println!(
"ANSI: {}, Truecolor: {}, 8-bit: {}",
env.supports_ansi, env.supports_truecolor, env.supports_8bit_color
);
Run the tests with:
cargo test
This project is licensed under the MIT License.
Contributions, issues, and feature requests are welcome! Please open an issue or submit a pull request.