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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env:) ⇒ KeyDecoder

Returns a new instance of KeyDecoder.



14
15
16
17
18
19
# File 'lib/fatty/curses/key_decoder.rb', line 14

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

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



12
13
14
# File 'lib/fatty/curses/key_decoder.rb', line 12

def env
  @env
end

#mapObject (readonly)

Returns the value of attribute map.



12
13
14
# File 'lib/fatty/curses/key_decoder.rb', line 12

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.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fatty/curses/key_decoder.rb', line 23

def decode(raw)
  return unless raw

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