[][src]Struct anes::parser::Parser

pub struct Parser { /* fields omitted */ }

An ANSI escape sequence parser.

Parser implements the Iterator<Item = Sequence> trait, thus you can use the next() method to consume all valid sequences with known meaning.

Examples

Parse cursor position:

use anes::parser::{Parser, Sequence};

let mut parser = Parser::default();
parser.advance(b"\x1B[20;10R", false);

assert_eq!(Some(Sequence::CursorPosition(10, 20)), parser.next());
assert!(parser.next().is_none());

Parse keyboard event:

use anes::parser::{KeyCode, KeyModifiers, Parser, Sequence};

let mut parser = Parser::default();
parser.advance("𐌼a".as_bytes(), false);

assert_eq!(Some(Sequence::Key(KeyCode::Char('𐌼'), KeyModifiers::empty())), parser.next());
assert_eq!(Some(Sequence::Key(KeyCode::Char('a'), KeyModifiers::empty())), parser.next());
assert!(parser.next().is_none());

Methods

impl Parser[src]

pub fn advance(&mut self, buffer: &[u8], more: bool)[src]

Advances parser state machine with additional input data.

Arguments

  • buffer - input data (stdin in raw mode, etc.)
  • more - more input data available right now

It's crucial to provide correct more value in order to receive KeyCode::Esc events as soon as possible.

Examples

Esc key:

use anes::parser::{KeyCode, KeyModifiers, Parser, Sequence};

let mut parser = Parser::default();
// User pressed Esc key & nothing else which means that there's no additional input available
// aka no possible escape sequence = `KeyCode::Esc` dispatched.
parser.advance(&[0x1b], false);

assert_eq!(Some(Sequence::Key(KeyCode::Esc, KeyModifiers::empty())), parser.next());
assert!(parser.next().is_none());

Possible escape sequence:

use anes::parser::{KeyCode, KeyModifiers, Parser, Sequence};

let mut parser = Parser::default();
// User pressed F1 = b"\x1BOP"

// Every escape sequence starts with Esc (0x1b). There's more input available
// aka possible escape sequence = `KeyCode::Esc` isn't dispatched even when the parser
// doesn't know rest of the sequence.
parser.advance(&[0x1b], true);
assert!(parser.next().is_none());

// Advance parser with rest of the sequence
parser.advance(&[b'O', b'P'], false);
assert_eq!(Some(Sequence::Key(KeyCode::F(1), KeyModifiers::empty())), parser.next());
assert!(parser.next().is_none());

Trait Implementations

impl Iterator for Parser[src]

type Item = Sequence

The type of the elements being iterated over.

impl Default for Parser[src]

Auto Trait Implementations

impl Send for Parser

impl Sync for Parser

impl Unpin for Parser

impl UnwindSafe for Parser

impl RefUnwindSafe for Parser

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = !

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]