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.



20
21
22
23
24
25
26
27
# File 'lib/charming/internal/terminal/key_normalizer.rb', line 20

def normalize(keypress)
  return nil unless keypress

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

  named_event(key_name)
end