Skip to main content

Parser

Struct Parser 

Source
pub struct Parser { /* private fields */ }
Expand description

An incremental parser for terminal input.

Feed it bytes as they arrive with Parser::feed, then pull events out with Parser::next_event. A sequence split across two reads is handled: the parser waits for the rest. Unknown or malformed sequences are skipped and never panic or block later input.

Implementations§

Source§

impl Parser

Source

pub fn new() -> Parser

Creates an empty parser.

Source

pub fn feed(&mut self, bytes: &[u8])

Adds input bytes to the parser.

Source

pub fn next_event(&mut self) -> Option<Event>

The next complete event, or None if more bytes are needed.

Source

pub fn expect_cursor_position(&mut self)

Tells the parser that a cursor position report (ESC [ row ; col R) is on its way, in answer to a ESC [ 6 n query. The next such sequence is then kept for Parser::take_cursor_position instead of being read as a key, because it is otherwise indistinguishable from Ctrl+F3.

Source

pub fn take_cursor_position(&mut self) -> Option<(u32, u32)>

The (row, column) of the last cursor position report, counted from 1, or None if none has arrived since this was last called.

Source

pub fn has_pending(&self) -> bool

Whether bytes are waiting for the rest of an escape sequence. After Parser::next_event returns None, this is true if more input could still complete an event, so the caller should wait a moment and then call Parser::flush.

Source

pub fn flush(&mut self) -> Option<Event>

Call when no more bytes are coming for a moment (for example after a short read timeout). A lone Escape byte could be the Escape key or the start of a longer sequence; this settles it as the Escape key.

Trait Implementations§

Source§

impl Debug for Parser

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Parser

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.