hwatch-ansi-parser

Crates.iohwatch-ansi-parser
lib.rshwatch-ansi-parser
version0.9.0
sourcesrc
created_at2024-03-04 19:29:52.980647
updated_at2024-03-04 19:29:52.980647
descriptionA library using nom for parsing ANSI Escape Codes
homepagehttps://gitlab.com/blacknon/ansi-parser
repository
max_upload_size
id1162190
size42,587
(blacknon)

documentation

README

pipeline status docs deps license downloads

Ansi Escape Sequence Parser

For a complete list of implemented sequences, see the documentation.

This is done through a pulldown type parser, where an iterator is exposed. This essentially turns all of the ANSI sequences into enums and splits the string at every location that there was an ANSI Sequence.

Example:

use ansi_parser::{Output, AnsiParser};
use ansi_parser::AnsiSequence;

fn main() {
    //Parse the first two blocks in the list
    //By parsing it this way, it allows you to iterate over the
    //elements returned.
    //
    //The parser only every holds a reference to the data,
    //so there is no allocation.
    let parsed: Vec<Output> = "This is \u{1b}[3Asome text!"
        .ansi_parse()
        .take(2)
        .collect();

    assert_eq!(
        vec![
            Output::TextBlock("This is "),
            Output::Escape(AnsiSequence::CursorUp(3))
        ],
        parsed
    );

    for block in parsed.into_iter() {
        match block {
            Output::TextBlock(text) => println!("{}", text),
            Output::Escape(seq)     => println!("{}", seq)
        }
    }
}

no_std support

no_std is supported via disabling the std feature in your Cargo.toml.

Note

Register as hwatch-ansi-parser in crate until the main unit version is updated.

Commit count: 0

cargo fmt