Class: MiniTerm::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_term/common/mapper.rb

Overview

Translate raw input to mapped commands.

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Mapper

Set up the keystroke mapper.

Yields:

  • (_self)

Yield Parameters:



10
11
12
13
# File 'lib/mini_term/common/mapper.rb', line 10

def initialize
  @map = Hash.new {|_h, key| [:unmapped, key + MiniTerm.flush]}
  yield self
end

Instance Method Details

#[]=(indexes, value) ⇒ Object

Add a map entry



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mini_term/common/mapper.rb', line 16

def []=(indexes, value)
  indexes = [indexes] unless indexes.is_a?(Range)

  indexes.each do |index|
    process_non_terminals(index)

    if @map.has_key?(index)
      fail MiniTermKME, "Duplicate entry #{index.inspect}"
    end

    @map[index] = [value, index]
  end
end

#get_mapped_charObject

Get a mapped input sequence.



46
47
48
49
50
51
52
53
54
55
# File 'lib/mini_term/common/mapper.rb', line 46

def get_mapped_char
  key_seq, key_cmd = "", nil

  begin
    key_seq << yield
    key_cmd = @map[key_seq]
  end until key_cmd

  key_cmd
end

#process_non_terminals(index) ⇒ Object

Handle the preamble characters in the command sequence.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mini_term/common/mapper.rb', line 31

def process_non_terminals(index)
  seq = ""

  index.chop.chars.each do |char|
    seq << char

    if @map.has_key?(seq) && @map[seq]
      fail MiniTermKME, "Ambiguous entry #{index.inspect}"
    end

    @map[seq] = false
  end
end