Class: Charming::Internal::Terminal::ModifiedKeyParser

Inherits:
Object
  • Object
show all
Defined in:
lib/charming/internal/terminal/modified_key_parser.rb

Overview

ModifiedKeyParser decodes xterm CSI sequences that carry a modifier parameter — "\e[1;2A" (shift+up), "\e[3;5~" (ctrl+delete) — which tty-reader's key table does not cover. The modifier parameter encodes 1 + (1 shift | 2 alt | 4 ctrl | 8 meta); meta is treated as alt.

Constant Summary collapse

LETTER_PATTERN =

"\e[1;" — arrows, home/end, and the SS3-style F1-F4 finals.

/\A\e\[1;(\d+)([A-DFHPQRS])\z/
TILDE_PATTERN =

"\e[;~" — insert/delete, page keys, home/end, F5-F12.

/\A\e\[(\d+);(\d+)~\z/
LETTER_KEYS =
{
  "A" => :up, "B" => :down, "C" => :right, "D" => :left,
  "F" => :end, "H" => :home,
  "P" => :f1, "Q" => :f2, "R" => :f3, "S" => :f4
}.freeze
TILDE_KEYS =
{
  1 => :home, 2 => :insert, 3 => :delete, 4 => :end,
  5 => :page_up, 6 => :page_down, 7 => :home, 8 => :end,
  15 => :f5, 17 => :f6, 18 => :f7, 19 => :f8,
  20 => :f9, 21 => :f10, 23 => :f11, 24 => :f12
}.freeze
SHIFT_BIT =
1
ALT_BIT =
2
CTRL_BIT =
4
META_BIT =
8

Class Method Summary collapse

  • .parse(raw) ⇒ Object

    Parses raw into a KeyEvent with modifier flags, or nil when it is not a modified CSI sequence.

Class Method Details

.parse(raw) ⇒ Object

Parses raw into a KeyEvent with modifier flags, or nil when it is not a modified CSI sequence.



37
38
39
40
41
42
43
44
45
# File 'lib/charming/internal/terminal/modified_key_parser.rb', line 37

def self.parse(raw)
  return nil unless raw.is_a?(String)

  if (match = raw.match(LETTER_PATTERN))
    build_event(LETTER_KEYS[match[2]], match[1].to_i)
  elsif (match = raw.match(TILDE_PATTERN))
    build_event(TILDE_KEYS[match[1].to_i], match[2].to_i)
  end
end