Class: Mapper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handler) ⇒ Mapper

Returns a new instance of Mapper.



12
13
14
15
16
17
18
19
20
# File 'lib/rbcurse/mapper.rb', line 12

def initialize handler
  #@handler = handler
  @view = handler       # caller program
  @keys = {}
  @mode = nil  # used when defining
  @pendingkeys = nil
  @prevkey = nil   # in case of a key sequence such as C-x C-c, will have C-x
  @arg = nil # regex matched this key.
end

Instance Attribute Details

#keymapObject (readonly)

Returns the value of attribute keymap.



8
9
10
# File 'lib/rbcurse/mapper.rb', line 8

def keymap
  @keymap
end

#keysObject (readonly)

Returns the value of attribute keys.



11
12
13
# File 'lib/rbcurse/mapper.rb', line 11

def keys
  @keys
end

#modeObject

Returns the value of attribute mode.



10
11
12
# File 'lib/rbcurse/mapper.rb', line 10

def mode
  @mode
end

#viewObject (readonly)

Returns the value of attribute view.



9
10
11
# File 'lib/rbcurse/mapper.rb', line 9

def view
  @view
end

Instance Method Details

#let(mode, &block) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/rbcurse/mapper.rb', line 21

def let mode, &block
  h = OrderedHash.new
  @keys[mode] = h
  @mode = mode
  instance_eval(&block)
  $log.debug("KEYS: #{@keys[mode].inspect}")
end

#map(*args, &block) ⇒ Object

mapping keys to blocks or symbols 2010-02-23 19:17 added check for string, so no required to use getbyte everywhere, esp for existing app However, this means that we can’t map an actual string, which we may want to for commands which cuold be passed outside keys, or as aliases.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rbcurse/mapper.rb', line 33

def map(*args, &block)
  if block_given?

    # We check for cases like C-x C-c etc. Only 2 levels.
    #args = arg.split(/ +/)
    arg0 = args[0]
    arg0 = args[0].getbyte(0) if args[0].class.to_s == 'String'
    if args.length == 2
      arg1 = args[1]
      arg1 = args[1].getbyte(0) if args[1].class.to_s == 'String'
      $log.debug "  KEY2 #{args[0]} , #{args[0].class},  #{args[1]} , #{args[1].class} "
      @keys[@mode][arg0] ||= OrderedHash.new
      @keys[@mode][arg0][arg1]=block
    else
      # single key or control key
      $log.debug "  KEY #{args[0]} , #{args[0].class} "
      @keys[@mode][arg0]=block
    end
  else
     #no block, last arg shold be a symbol
     symb = args.pop
     raise "If block not passed, last arg should be a method symbol" if !symb.is_a? Symbol
     arg0 = args[0]
     arg0 = args[0].getbyte(0) if args[0].class.to_s == 'String'
     if args.length == 2
       arg1 = args[1]
       arg1 = args[1].getbyte(0) if args[1].class.to_s == 'String'
       @keys[@mode][arg0] ||= OrderedHash.new
       @keys[@mode][arg0][arg1]=symb
     else
       # single key or control key
       @keys[@mode][arg0]=symb
     end
  end
end

#match(key) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rbcurse/mapper.rb', line 105

def match key
     #$log.debug "MATCH #{key} "
    #blk = @keys[@view.mode][key]
  @keys[@view.mode].each_pair do |k,v|
   #$log.debug "LOOP #{k.class}, #{k}, #{v} "
    case k.class.to_s
    when "String"
      return v if k == key
    when "Fixnum" # for keyboard2
   #$log.debug "FIXNUM LOOP #{k.class}, #{k}, #{v} "
      return v if k == key
    when "Regexp"
#       $log.debug "REGEX #key , #k, #{k.match(key)}"
      key1 = key.chr if key.is_a? Fixnum
      if !k.match(key1).nil?
        @arg = key1
        return v 
      end
    else
      $log.error "MATCH: Unhandled class #{k.class} "
    end
  end
     $log.debug "MATCH #{key} ret nil "
  return nil
end

#press(key) ⇒ Object

manages key pressing takes care of multiple key combos too



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rbcurse/mapper.rb', line 71

def press key
  #$log.debug("press Got: #{key}")
  # for a double key combination such as C-x C-c this has the set of pending keys to check against
  if @pendingkeys != nil
    blk = @pendingkeys[key]
  else
    # this is the regular single key mode
    #blk = @keys[@view.mode][key]
    blk = match(key)
  end
  # this means this key expects more keys to follow such as C-x could
  if blk.is_a? OrderedHash
    @pendingkeys = blk
    @prevkey = key
    return
  end
  if blk.nil? # this should go up XXX
    if !@pendingkeys.nil?
      # this error message to be modified if using numeric keys -- need to convert to char
      view.info("%p not valid in %p. Try: #{@pendingkeys.keys.join(', ')}" % [key, @prevkey]) # XXX
    else
      view.info("%p not valid in %p. " % [key, @view.mode]) 
    end
    return
  end
  # call the block or symbol - our user defined key mappings use symbols
  if blk.is_a? Symbol
    @view.send(blk)
  else
    blk.call
  end
  @prevkey = nil
  @pendingkeys = nil
end