#ansi #parser #iterator #nom #string #escape

ansi-parser

A library using nom for parsing ANSI Escape Codes

14 releases (5 breaking)

✓ Uses Rust 2018 edition

0.6.5 May 10, 2019
0.6.4 May 10, 2019
0.5.3 May 4, 2019
0.4.0 Apr 27, 2019
0.1.0 Apr 27, 2019
Download history 28/week @ 2019-11-01 4/week @ 2019-11-08 44/week @ 2019-11-15 15/week @ 2019-11-22 29/week @ 2019-11-29 408/week @ 2019-12-06 1/week @ 2019-12-13 1/week @ 2019-12-20 18/week @ 2019-12-27 6/week @ 2020-01-03 30/week @ 2020-01-10 45/week @ 2020-01-17 2/week @ 2020-01-24 1/week @ 2020-01-31 19/week @ 2020-02-07

228 downloads per month

MPL-2.0 license

22KB
589 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)
        }
    }
}

Dependencies

~650KB
~13K SLoC