Tela.Input (tela v0.1.0)

Copy Markdown View Source

Parses raw terminal byte sequences into Tela.Key structs.

OTP 28's raw terminal mode delivers keystrokes as raw bytes via :io.get_chars/2. This module is responsible for translating those bytes into structured Tela.Key values that applications can pattern-match on.

Usage

iex> Tela.Input.parse("a")
[%Tela.Key{key: {:char, "a"}, raw: "a"}]

iex> Tela.Input.parse("\e[A")
[%Tela.Key{key: :up, raw: "\e[A"}]

iex> Tela.Input.parse("\e[A\e[B")
[%Tela.Key{key: :up, raw: "\e[A"}, %Tela.Key{key: :down, raw: "\e[B"}]

Batched input

Multiple keystrokes may arrive in a single :io.get_chars/2 call — for example during rapid typing or paste. parse/1 handles this correctly by consuming the binary recursively and returning one Tela.Key per keystroke.

Unknown sequences

Any byte sequence that does not match a known pattern produces a Tela.Key with key: :unknown. The raw bytes are preserved in the :raw field for debugging.

Summary

Functions

Parses a binary of raw terminal bytes into a list of Tela.Key structs.

Functions

parse(binary)

@spec parse(binary()) :: [Tela.Key.t()]

Parses a binary of raw terminal bytes into a list of Tela.Key structs.

Returns an empty list for empty input. Unrecognised sequences produce a key with key: :unknown.

Examples

iex> Tela.Input.parse("")
[]

iex> Tela.Input.parse("q")
[%Tela.Key{key: {:char, "q"}, raw: "q"}]

iex> Tela.Input.parse("\r")
[%Tela.Key{key: :enter, raw: "\r"}]

iex> Tela.Input.parse("\e[A")
[%Tela.Key{key: :up, raw: "\e[A"}]