18 releases

0.9.1 May 19, 2024
0.8.0 Apr 25, 2021
0.7.0 Oct 5, 2020
0.6.5 May 10, 2019

#230 in Parser implementations

Download history 1255/week @ 2024-07-19 1410/week @ 2024-07-26 1226/week @ 2024-08-02 1324/week @ 2024-08-09 1330/week @ 2024-08-16 1167/week @ 2024-08-23 1410/week @ 2024-08-30 1189/week @ 2024-09-06 1000/week @ 2024-09-13 928/week @ 2024-09-20 1223/week @ 2024-09-27 1040/week @ 2024-10-04 925/week @ 2024-10-11 989/week @ 2024-10-18 965/week @ 2024-10-25 1147/week @ 2024-11-01

4,120 downloads per month
Used in 23 crates (16 directly)

MPL-2.0 license

26KB
588 lines

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.

Dependencies

~1.5MB
~28K SLoC