Class: Rereline::KeyStroke

Inherits:
Object
  • Object
show all
Defined in:
lib/rereline/key_stroke.rb

Defined Under Namespace

Modules: Ex

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapping) ⇒ KeyStroke

Returns a new instance of KeyStroke.



37
38
39
# File 'lib/rereline/key_stroke.rb', line 37

def initialize(mapping)
  @mapping = mapping
end

Instance Attribute Details

#mappingObject (readonly)

Returns the value of attribute mapping.



35
36
37
# File 'lib/rereline/key_stroke.rb', line 35

def mapping
  @mapping
end

Instance Method Details

#expand(input, wait_match: true) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rereline/key_stroke.rb', line 41

def expand(input, wait_match: true)
  if input.empty?
    [[], []]
  elsif wait_match && matching?(input)
    [[], input]
  elsif matched?(input)
    lhs, rhs = mapping.filter { |lhs, rhs| lhs.matched? input }.max_by { |lhs, rhs| lhs.size }
    if (after_input = input.drop(lhs.size)).size > 0
      # NOTE: 再帰的にマッピングされているキーを展開する時に後続のキーが存在している場合は待ち処理はせずに展開する
      join(expand(rhs, wait_match: false), expand(after_input, wait_match: wait_match))
    else
      expand(rhs, wait_match: wait_match)
    end
  else
    join([input.take(1), []], expand(input.drop(1), wait_match: wait_match))
  end
end