TermUI.Parser (TermUI v0.2.0)

View Source

Escape sequence parser for terminal input.

Transforms raw terminal input bytes into structured events (key presses, mouse actions, paste content, focus changes).

Summary

Functions

Flushes any pending escape sequence as an ESC key event.

Creates a new parser state.

Parses input bytes into events.

Resets parser state while preserving configuration.

Types

event()

state()

@type state() :: %{
  mode: atom(),
  buffer: binary(),
  params: [integer()],
  paste_buffer: binary()
}

Functions

flush_escape(state)

@spec flush_escape(state()) :: {[event()], state()}

Flushes any pending escape sequence as an ESC key event.

Call this after a timeout when parser is in :escape state.

new()

@spec new() :: state()

Creates a new parser state.

parse(input, state)

@spec parse(binary(), state()) :: {[event()], binary(), state()}

Parses input bytes into events.

Returns {events, remaining_bytes, new_state} where:

  • events - List of parsed events
  • remaining_bytes - Bytes that couldn't be parsed yet (incomplete sequences)
  • new_state - Parser state for next call

Examples

iex> {events, "", _state} = TermUI.Parser.parse("a", TermUI.Parser.new())
iex> [%TermUI.Parser.Events.KeyEvent{key: "a"}] = events

reset(state)

@spec reset(state()) :: state()

Resets parser state while preserving configuration.