Module parser

Source
Expand description

§Parser for ansi-control-codes

This module contains a parser implementation that can be used to parse string-like types into a sequence of ansi-control-codes (represented by ControlFunctions) and strings that do not contain any ansi-control-codes.

To use the parser module, enable the feature parser in your Cargo.toml.

cargo add ansi-control-codes --features parser

§Example Usage

use ansi_control_codes::c1::NEL;
use ansi_control_codes::parser::{TokenStream, Token};

let to_be_parsed = format!("This text{}is spread across{}multiple lines.", NEL, NEL);
let parsed = TokenStream::from(&to_be_parsed);

let parts: Vec<Token> = parsed.collect();
println!("{:?}", parts);

assert_eq!(parts[0], Token::String("This text"));
assert_eq!(parts[1], Token::ControlFunction(NEL));
assert_eq!(parts[2], Token::String("is spread across"));
assert_eq!(parts[3], Token::ControlFunction(NEL));
assert_eq!(parts[4], Token::String("multiple lines."));

§Parse and Explain

You can combine the features parser and explain of this crate to parse text and explain the meaning of control functions. Also check the Explain-Module.

use ansi_control_codes::c1::NEL;
use ansi_control_codes::parser::{TokenStream, Token};
use ansi_control_codes::explain::Explain;

let example = "\x1b[0u\x1b[62c\x1b[23;6H";
let result = TokenStream::from(&example).collect::<Vec<Token>>();

for (i, part) in result.iter().enumerate() {
    match part {
        Token::String(string) => println!("{}. Normal String: {}", i, string),
        Token::ControlFunction(control_function) => {
            println!(
                "{}. Control Function: {} ({})",
                i,
                control_function.short_name().unwrap_or_default(),
                control_function.long_name()
            );
            println!(
                "Short description: {}",
                control_function.short_description()
            );
            println!("Long description: {}", control_function.long_description());
        }
    }
    if i < (result.len() - 1) {
        println!("---------------------");
    }
}

This will produce the following output

0. Control Function:  (Private Use / Experimental Use)
Short description: Reserved for private use / not standardized.
Long description: Reserved for private use / not standardized.
---------------------
1. Control Function: DA (Device Attributes)
Short description: The device sending this identifies as device with code 62.
Long description: The device sending this identifies as device with code 62.
---------------------
2. Control Function: CUP (Cursor Position)
Short description: Move the active position to line 23 and character 6.
Long description: Causes the active presentation position to be moved in the presentation component to the 23rd line
position according to the line progression, and to the 6 character position according to the character path.

Structs§

TokenStream
A TokenStream is a stream of Tokens that were parsed from an input string. The TokenStream implements the Iterator interface, which can be used to extract the result of a parse operation.

Enums§

Token
A Token contains a part of the parsed string. Each part is either a String that does not contain any ansi-control-codes (represented by Token::String), or a ansi-control-code (represented by Token::ControlFunction).