Class: Charming::Internal::Terminal::KeyNormalizer

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

Overview

KeyNormalizer turns raw keypress strings (from tty-reader) into normalized KeyEvent objects with a semantic key symbol and the printable character (when applicable). Handles ctrl-modifier naming, special keys (return, tab, space), and the back-tab (Shift+Tab) variant.

Constant Summary collapse

CTRL_KEY_PATTERN =

Matches key names like "ctrl_a" → captures "a" so the modifier can be split out.

/\Actrl_(?<key>.+)\z/

Instance Method Summary collapse

Constructor Details

#initialize(reader) ⇒ KeyNormalizer

reader is a TTY::Reader used to look up canonical key names from raw keypresses.



15
16
17
# File 'lib/charming/internal/terminal/key_normalizer.rb', line 15

def initialize(reader)
  @reader = reader
end

Instance Method Details

#normalize(keypress) ⇒ Object

Converts a raw keypress string into a KeyEvent. Returns nil when keypress is nil. Modified CSI sequences (shift/alt/ctrl arrows, function keys) and ESC-prefixed alt chords are decoded first; everything else goes through tty-reader's key table.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/charming/internal/terminal/key_normalizer.rb', line 22

def normalize(keypress)
  return nil unless keypress

  modified = ModifiedKeyParser.parse(keypress)
  return modified if modified

  alt = alt_prefixed_event(keypress)
  return alt if alt

  key_name = @reader.console.keys[keypress]
  return character_event(keypress) unless key_name

  named_event(key_name)
end