Class: Fatty::KeyEvent
- Inherits:
-
Object
- Object
- Fatty::KeyEvent
- 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
-
#ctrl ⇒ Object
readonly
Returns the value of attribute ctrl.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#shift ⇒ Object
readonly
Returns the value of attribute shift.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #bindings ⇒ Object
- #code ⇒ Object
- #coded? ⇒ Boolean
- #color ⇒ Object
- #ctrl? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(key:, text: nil, raw: nil, ctrl: false, meta: false, shift: false) ⇒ KeyEvent
constructor
A new instance of KeyEvent.
- #key_name ⇒ Object
- #meta? ⇒ Boolean
- #printable? ⇒ Boolean
- #raw_bytes ⇒ Object
- #report ⇒ Object
- #shift? ⇒ Boolean
- #status_string ⇒ Object
- #suggested_snippet(terminal_name) ⇒ Object
- #to_s ⇒ Object
- #unbound? ⇒ Boolean
- #uncoded? ⇒ Boolean
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 = @shift = shift # invariant: chorded keys do not self-insert @text = ctrl || ? nil : text end |
Instance Attribute Details
#ctrl ⇒ Object (readonly)
Returns the value of attribute ctrl.
26 27 28 |
# File 'lib/fatty/key_event.rb', line 26 def ctrl @ctrl end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
26 27 28 |
# File 'lib/fatty/key_event.rb', line 26 def key @key end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
26 27 28 |
# File 'lib/fatty/key_event.rb', line 26 def @meta end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
26 27 28 |
# File 'lib/fatty/key_event.rb', line 26 def raw @raw end |
#shift ⇒ Object (readonly)
Returns the value of attribute shift.
26 27 28 |
# File 'lib/fatty/key_event.rb', line 26 def shift @shift end |
#text ⇒ Object (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 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 && == other. && shift == other.shift end |
#bindings ⇒ Object
148 149 150 |
# File 'lib/fatty/key_event.rb', line 148 def bindings Fatty::KeyMap.active.bindings_for(self) || {} end |
#code ⇒ Object
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
79 80 81 |
# File 'lib/fatty/key_event.rb', line 79 def coded? key.is_a?(Symbol) end |
#color ⇒ Object
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
152 153 154 |
# File 'lib/fatty/key_event.rb', line 152 def ctrl? @ctrl end |
#hash ⇒ Object
71 72 73 |
# File 'lib/fatty/key_event.rb', line 71 def hash [key, ctrl, , shift].hash end |
#key_name ⇒ Object
144 145 146 |
# File 'lib/fatty/key_event.rb', line 144 def key_name coded? ? to_s : "(none)" end |
#meta? ⇒ Boolean
156 157 158 |
# File 'lib/fatty/key_event.rb', line 156 def @meta end |
#printable? ⇒ Boolean
75 76 77 |
# File 'lib/fatty/key_event.rb', line 75 def printable? text && !text.empty? && text != "\n" && text != "\r" end |
#raw_bytes ⇒ Object
100 101 102 |
# File 'lib/fatty/key_event.rb', line 100 def raw_bytes bytes_from_raw(raw) end |
#report ⇒ Object
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
160 161 162 |
# File 'lib/fatty/key_event.rb', line 160 def shift? @shift end |
#status_string ⇒ Object
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_s ⇒ Object
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
87 88 89 |
# File 'lib/fatty/key_event.rb', line 87 def unbound? coded? && bindings.empty? end |
#uncoded? ⇒ Boolean
83 84 85 |
# File 'lib/fatty/key_event.rb', line 83 def uncoded? !key.is_a?(Symbol) end |