class MiniTerm::Mapper

Translate raw input to mapped commands.

Public Class Methods

new() { |self| ... } click to toggle source

Set up the keystroke mapper.

# File lib/mini_term/common/mapper.rb, line 10
def initialize
  @map = Hash.new {|_h, key| [:unmapped, key + MiniTerm.flush]}
  yield self
end

Public Instance Methods

[]=(indexes, value) click to toggle source

Add a map entry

# 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_char() { || ... } click to toggle source

Get a mapped input sequence.

# 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) click to toggle source

Handle the preamble characters in the command sequence.

# 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