Class: Fatty::KeyEvent

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

Overview

The KeyEvent class is a simple class to store a key with its modifiers. KeyMap will map a KeyEvent to an action (for a given context), and the KeyDecoder class is responsible for turning raw key inputs from curses into a KeyEvent. The CURSES_TO_EVENT constant is an initial map from known curses key codes to KeyEvents. These can be overriden for different terminals in Fatty::Config.keydefs.

Constant Summary collapse

CTRL_PUNCT =
{
  27 => :'[',   # ESC is also C-[
  28 => :'\\',  # C-\
  29 => :']',   # C-]
  30 => :'^',   # C-^
  31 => :'/',   # C-_
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, text: nil, raw: nil, ctrl: false, meta: false, shift: false) ⇒ KeyEvent

Returns a new instance of KeyEvent.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fatty/key_event.rb', line 28

def initialize(key:, text: nil, raw: nil, ctrl: false, meta: false, shift: false)
  # If the decoder gives us a control character as an integer (1..26),
  # canonicalize it to ctrl+letter so keymaps and display behave nicely.
  if key.is_a?(Integer)
    if CTRL_CODE_TO_LETTER.key?(key)
      key = CTRL_CODE_TO_LETTER[key]
      ctrl = true
    elsif CTRL_PUNCT.key?(key)
      key = CTRL_PUNCT[key]
      ctrl = true
    end
  end
  @key = key           # Symbol or named key
  @raw = raw           # the key (or keys for escape sequences) as returned by Curses
  @ctrl = ctrl
  @meta = meta
  @shift = shift
  # invariant: chorded keys do not self-insert
  @text = ctrl || meta ? nil : text
end

Instance Attribute Details

#ctrlObject (readonly)

Returns the value of attribute ctrl.



26
27
28
# File 'lib/fatty/key_event.rb', line 26

def ctrl
  @ctrl
end

#keyObject (readonly)

Returns the value of attribute key.



26
27
28
# File 'lib/fatty/key_event.rb', line 26

def key
  @key
end

#metaObject (readonly)

Returns the value of attribute meta.



26
27
28
# File 'lib/fatty/key_event.rb', line 26

def meta
  @meta
end

#rawObject (readonly)

Returns the value of attribute raw.



26
27
28
# File 'lib/fatty/key_event.rb', line 26

def raw
  @raw
end

#shiftObject (readonly)

Returns the value of attribute shift.



26
27
28
# File 'lib/fatty/key_event.rb', line 26

def shift
  @shift
end

#textObject (readonly)

Returns the value of attribute text.



26
27
28
# File 'lib/fatty/key_event.rb', line 26

def text
  @text
end

Class Method Details

.key_to_str(key: "<?>", ctrl: false, meta: false, shift: false) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/fatty/key_event.rb', line 49

def self.key_to_str(key: "<?>", ctrl: false, meta: false, shift: false)
  mods = []
  mods << "C" if ctrl
  mods << "M" if meta
  mods << "S" if shift
  key_str = key.to_s
  mods.empty? ? key_str : "#{mods.join('-')}-#{key_str}"
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



62
63
64
65
66
67
68
# File 'lib/fatty/key_event.rb', line 62

def ==(other)
  other.is_a?(KeyEvent) &&
    key == other.key &&
    ctrl == other.ctrl &&
    meta == other.meta &&
    shift == other.shift
end

#bindingsObject



148
149
150
# File 'lib/fatty/key_event.rb', line 148

def bindings
  Fatty::KeyMap.active.bindings_for(self) || {}
end

#codeObject



91
92
93
94
95
96
97
98
# File 'lib/fatty/key_event.rb', line 91

def code
  case raw
  when Integer
    raw
  when Array
    raw.reverse.find { |item| item.is_a?(Integer) }
  end
end

#coded?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/fatty/key_event.rb', line 79

def coded?
  key.is_a?(Symbol)
end

#colorObject



104
105
106
107
108
109
110
111
112
# File 'lib/fatty/key_event.rb', line 104

def color
  if uncoded?
    :error
  elsif unbound?
    :warn
  else
    :good
  end
end

#ctrl?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/fatty/key_event.rb', line 152

def ctrl?
  @ctrl
end

#hashObject



71
72
73
# File 'lib/fatty/key_event.rb', line 71

def hash
  [key, ctrl, meta, shift].hash
end

#key_nameObject



144
145
146
# File 'lib/fatty/key_event.rb', line 144

def key_name
  coded? ? to_s : "(none)"
end

#meta?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/fatty/key_event.rb', line 156

def meta?
  @meta
end

#printable?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/fatty/key_event.rb', line 75

def printable?
  text && !text.empty? && text != "\n" && text != "\r"
end

#raw_bytesObject



100
101
102
# File 'lib/fatty/key_event.rb', line 100

def raw_bytes
  bytes_from_raw(raw)
end

#reportObject



124
125
126
127
128
129
130
131
132
# File 'lib/fatty/key_event.rb', line 124

def report
  if uncoded?
    report_for_uncoded
  elsif unbound?
    report_for_unbound
  else
    report_for_bound
  end
end

#shift?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/fatty/key_event.rb', line 160

def shift?
  @shift
end

#status_stringObject



114
115
116
117
118
119
120
121
122
# File 'lib/fatty/key_event.rb', line 114

def status_string
  if uncoded?
    "(UNCODED)"
  elsif unbound?
    "(UNBOUND)"
  else
    "(BOUND)"
  end
end

#suggested_snippet(terminal_name) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/fatty/key_event.rb', line 134

def suggested_snippet(terminal_name)
  if uncoded?
    suggested_keydef(terminal_name)
  elsif unbound?
    suggested_keybinding
  else
    ''
  end
end

#to_sObject



58
59
60
# File 'lib/fatty/key_event.rb', line 58

def to_s
  self.class.key_to_str(key:, ctrl:, meta:, shift:)
end

#unbound?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/fatty/key_event.rb', line 87

def unbound?
  coded? && bindings.empty?
end

#uncoded?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/fatty/key_event.rb', line 83

def uncoded?
  !key.is_a?(Symbol)
end