Class: Thaum::EscapeParser

Inherits:
Object
  • Object
show all
Defined in:
lib/thaum/escape_parser.rb

Overview

Parses raw terminal input bytes into KeyEvent / PasteEvent objects.

The parser is stateful: a paste payload that spans multiple parse() calls (chunked reads from the InputReader) is accumulated across calls and emitted as a single PasteEvent once the closing e[201~ marker arrives. The class-level .parse helper makes a fresh instance for one-shot use.

Constant Summary collapse

PASTE_START =
"\e[200~"
PASTE_END =
"\e[201~"
CR =

Ground state byte codes

0x0d
LF =

Carriage return

0x0a
TAB =

Line feed

0x09
DEL =

Tab

0x7f
BS =

Delete

0x08
CTRL_START =

Backspace

0x01
CTRL_END =
0x1a
PRINTABLE_START =
0x20
PRINTABLE_END =
0x7e
ESC =

Escape sequence byte codes

0x1b
SGR_MOUSE_MARKER =

‘<’ for SGR mouse introducer

0x3c
SGR_MOUSE_PRESS =

‘M’

0x4d
SGR_MOUSE_RELEASE =

‘m’

0x6d
CSI_FINAL_START =
0x40
CSI_FINAL_END =
0x7e
SGR_DIGIT_START =

‘0’

0x30
SGR_DIGIT_END =

‘9’

0x39
SGR_SEP =

‘;’

0x3b

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEscapeParser

Returns a new instance of EscapeParser.



39
40
41
# File 'lib/thaum/escape_parser.rb', line 39

def initialize
  @paste_buf = nil
end

Class Method Details

.parse(input) ⇒ Object



37
# File 'lib/thaum/escape_parser.rb', line 37

def self.parse(input) = new.parse(input)

Instance Method Details

#parse(input) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/thaum/escape_parser.rb', line 43

def parse(input)
  events = []
  i = 0
  while i < input.bytesize
    if @paste_buf
      i = collect_paste(input: input, i: i, events: events)
    elsif input.getbyte(i) == ESC
      i = parse_at_escape(input: input, i: i, events: events)
    else
      event = parse_ground(input.getbyte(i))
      events << event if event
      i += 1
    end
  end
  events
end