ANSIEscapeRS
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.
Features
- Type-safe ANSI code generation: Use enums and builder patterns to create valid ANSI escape sequences.
- Parsing and interpretation: Efficiently parse strings containing ANSI codes into structured representations.
- Environment detection: Query terminal capabilities (ANSI support, truecolor, 8-bit color, etc.).
- Comprehensive color support: Standard, 8-bit, and 24-bit (truecolor) modes.
- Cursor and device control: Move the cursor, clear the screen, and more.
- Tested: Includes extensive unit tests for formatting and parsing.
Modules Overview
ansi_creator (accessed via crate root)
- Purpose: API for producing ANSI escape codes and querying environment capabilities.
- Key Types:
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.
- Example:
use ; let creator = new; let bold_red = creator.format_text; println!;
interpreter (accessed via ansi_escapers::interpreter)
- Purpose: Efficient parser for interpreting ANSI escape codes in strings.
- Key Types:
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.
- Example:
use AnsiParser; let mut parser = new; let result = parser.parse_annotated; println!;
ansi_types (accessed via crate root)
- Purpose: Core enums representing ANSI escape code capabilities.
- Key Types:
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.
Usage
Add to your Cargo.toml:
[]
= "0.1.0"
Import and use in your Rust code (all main types are available from the crate root):
use ;
let creator = new;
let styled = creator.format_text;
println!;
Environment Detection
The library can detect terminal capabilities:
use AnsiEnvironment;
let env = detect;
println!;
Testing
Run the tests with:
License
This project is licensed under the MIT License.
Contributing
Contributions, issues, and feature requests are welcome! Please open an issue or submit a pull request.