Class: Fatty::KeyGesture

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

Overview

This struct is used as a key into the KeyMap. It represents only those parts of the KeyEvent that are relevant for resolving bindings. It also normalizes the use of uppercase letters, like 'G' to convert them to a canonical :g with shift: true. That way the user can bind either to mean the same thing. KeyGesture = Struct.new(:key, :ctrl, :meta, :shift, keyword_init: true) do

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, ctrl:, meta:, shift:) ⇒ KeyGesture

Returns a new instance of KeyGesture.



15
16
17
18
19
20
# File 'lib/fatty/key_map.rb', line 15

def initialize(key:, ctrl:, meta:, shift:)
  @key = key
  @ctrl = ctrl
  @meta = meta
  @shift = shift
end

Instance Attribute Details

#ctrlObject (readonly)

Returns the value of attribute ctrl.



13
14
15
# File 'lib/fatty/key_map.rb', line 13

def ctrl
  @ctrl
end

#keyObject (readonly)

Returns the value of attribute key.



13
14
15
# File 'lib/fatty/key_map.rb', line 13

def key
  @key
end

#metaObject (readonly)

Returns the value of attribute meta.



13
14
15
# File 'lib/fatty/key_map.rb', line 13

def meta
  @meta
end

#shiftObject (readonly)

Returns the value of attribute shift.



13
14
15
# File 'lib/fatty/key_map.rb', line 13

def shift
  @shift
end

Class Method Details

.from_event(ev) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fatty/key_map.rb', line 22

def self.from_event(ev)
  k = ev&.key
  ctrl = !!ev&.ctrl
  meta = !!ev&.meta
  shift = !!ev&.shift

  if k.is_a?(Symbol)
    s = k.to_s
    if s.length == 1
      ch = s[0]
      if ('A'..'Z').cover?(ch)
        # Decoder (or config) used :G. Canonicalize to :g + shift.
        k = ch.downcase.to_sym
        shift = true
      elsif ('a'..'z').cover?(ch)
        # Decoder used :g. Keep it, but allow explicit shift to mean uppercase.
        k = ch.to_sym
      end
    end
  end

  new(key: k, ctrl: ctrl, meta: meta, shift: shift)
end

Instance Method Details

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



46
47
48
49
50
51
52
# File 'lib/fatty/key_map.rb', line 46

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

#hashObject



55
56
57
# File 'lib/fatty/key_map.rb', line 55

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

#inspectObject



59
60
61
# File 'lib/fatty/key_map.rb', line 59

def inspect
  "#<#{self.class} key=#{key.inspect} ctrl=#{ctrl} meta=#{meta} shift=#{shift}>"
end