Class: Fatty::Curses::KeyDecoder

Inherits:
Object
  • Object
show all
Defined in:
lib/fatty/curses/key_decoder.rb

Overview

The KeyDecoder class is responsible for converting raw key events returned by the curses library into KeyEvent's to be used by Fatty. This allows Fatty to deal with keyboard actions using friendly names, like :home. :page_down, :f5, and so forth. It also sets the modifiers, :shift, :ctrl, and :meta, in the KeyEvent so that the KeyMap can assign different actions to the modified keys.

Constant Summary collapse

SS3_TO_EVENT =
{
  "j" => KeyEvent.new(key: :keypad_multiply),
  "k" => KeyEvent.new(key: :keypad_plus),
  "l" => KeyEvent.new(key: :keypad_comma),
  "m" => KeyEvent.new(key: :keypad_minus),
  "n" => KeyEvent.new(key: :keypad_decimal),
  "o" => KeyEvent.new(key: :keypad_divide),
  "p" => KeyEvent.new(key: :keypad_0),
  "q" => KeyEvent.new(key: :keypad_1),
  "r" => KeyEvent.new(key: :keypad_2),
  "s" => KeyEvent.new(key: :keypad_3),
  "t" => KeyEvent.new(key: :keypad_4),
  "u" => KeyEvent.new(key: :keypad_5),
  "v" => KeyEvent.new(key: :keypad_6),
  "w" => KeyEvent.new(key: :keypad_7),
  "x" => KeyEvent.new(key: :keypad_8),
  "y" => KeyEvent.new(key: :keypad_9),
  "M" => KeyEvent.new(key: :keypad_enter),
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env: Env.detect) ⇒ KeyDecoder

Returns a new instance of KeyDecoder.



34
35
36
37
38
39
# File 'lib/fatty/curses/key_decoder.rb', line 34

def initialize(env: Env.detect)
  @env = env
  @map = {}
  load_builtin_map
  load_user_config
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



32
33
34
# File 'lib/fatty/curses/key_decoder.rb', line 32

def env
  @env
end

#mapObject (readonly)

Returns the value of attribute map.



32
33
34
# File 'lib/fatty/curses/key_decoder.rb', line 32

def map
  @map
end

Instance Method Details

#decode(raw) ⇒ Object

Return a KeyEvent object based on the raw keyboard input as returned by Screen#read_raw.



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

def decode(raw)
  return unless raw

  result =
    case raw
    when Array
      if raw.length == 3 && ss3_sequence?(raw)
        decode_ss3(raw)
      else
        decode_meta(raw)
      end
    else
      decode_single(raw)
    end
  Fatty.debug("#{self.class}#decode(raw: #{raw}) -> #{result}", tag: :keycode)
  result
end