Class: RubyTUI::Input::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/rubytui/input/parser.rb

Overview

Decodes raw terminal input into KeyEvent and MouseEvent objects.

Recognised input: printable ASCII, Enter, Tab, Backspace, Ctrl+letter, multi-byte UTF-8 characters given as raw bytes (as read by Reader), ESC, Alt+character (ESC followed by one character), CSI and SS3 key sequences (arrows, Home/End, Insert/Delete, Page Up/Down, Back-tab, F1-F12, xterm modified arrows such as ESC [ 1 ; 5 A), and SGR and legacy mouse reports.

Examples:

Input::Parser.new.parse("\e[A")  # => KeyEvent(up)

Instance Method Summary collapse

Instance Method Details

#parse(bytes) ⇒ KeyEvent, ...

Parse raw bytes into an Event.

Parameters:

  • bytes (String, nil) —

    raw bytes of one key press or mouse report

Returns:

  • (KeyEvent, MouseEvent, nil) —

    the decoded event, or nil if bytes is nil, empty or not a recognised sequence



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rubytui/input/parser.rb', line 22

def parse(bytes)
  return nil if bytes.nil? || bytes.empty?

  # ESC sequence
  if bytes.start_with?("\e")
    parse_escape(bytes)
  elsif bytes.length == 1
    parse_single(bytes)
  else
    # Multi-byte UTF-8 character
    KeyEvent.new(code: Key::CHAR, char: bytes)
  end
end