Class: Canis::PrefixCommand

Inherits:
Object show all
Defined in:
lib/canis/core/include/appmethods.rb

Overview

utils

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_symbol, calling, config = {}) {|_self| ... } ⇒ PrefixCommand

Returns a new instance of PrefixCommand.

Yields:

  • (_self)

Yield Parameters:



102
103
104
105
106
107
108
# File 'lib/canis/core/include/appmethods.rb', line 102

def initialize _symbol, calling, config={}, &block
  @object = calling
  @symbol = _symbol
  @descriptions = {}
  define_prefix_command _symbol
  yield self if block_given?
end

Instance Attribute Details

#objectObject

Returns the value of attribute object.



101
102
103
# File 'lib/canis/core/include/appmethods.rb', line 101

def object
  @object
end

Instance Method Details

#callObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/canis/core/include/appmethods.rb', line 138

def call
  h = @map
  ch = @object.window.getch # dicey.
    $log.debug "XXX:  CALLED #{ch} "
  if ch
    if ch == KEY_F1
      text =  ["Options are: "]
      h.keys.each { |e| c = keycode_tos(e); text << " #{c} #{@descriptions[e]} " }
      textdialog text, :title => " #{@symbol} key bindings "
      return
    end
    res =  h[ch]
    if res.is_a? Proc
      res.call
    elsif res.is_a? Symbol
      @object.send(res) if res
    else
      Ncurses.beep
      @object.window.ungetch(ch)

      :UNHANDLED
    end
  else
    raise "got nothing"
  end
end

#define_key(_keycode, *args, &blk) ⇒ Object Also known as: key

define a key within a prefix key map such as C-x Now that i am moving this from global, how will describe bindings get hold of the bindings and descriptions

Raises:

  • (ArgumentError)


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/canis/core/include/appmethods.rb', line 168

def define_key _keycode, *args, &blk
  _symbol = @symbol
  h = $rb_prefix_map[_symbol]
  raise ArgumentError, "No such keymap #{_symbol} defined. Use define_prefix_command." unless h
  _keycode = _keycode[0].getbyte(0) if _keycode[0].class == String
  arg = args.shift
  if arg.is_a? String
    desc = arg
    arg = args.shift
  elsif arg.is_a? Symbol
    # its a symbol
    desc = arg.to_s
  elsif arg.nil?
    desc = "unknown"
  else
    raise ArgumentError, "Don't know how to handle #{arg.class} in PrefixManager"
  end
  @descriptions[_keycode] = desc

  if !block_given?
    blk = arg
  end
  h[_keycode] = blk
end

#define_prefix_command(_name, config = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/canis/core/include/appmethods.rb', line 109

def define_prefix_command _name, config={}
  $rb_prefix_map ||= {}
  #h = {}
  #@map = h
  _name = _name.to_sym unless _name.is_a? Symbol
  # TODO it may already exist, so retrieve it
  $rb_prefix_map[_name] ||= {}
  @map = $rb_prefix_map[_name]
  # create a variable by name _name
  # create a method by same name to use
  @object.instance_eval %{
    def #{_name.to_s} *args
       h = $rb_prefix_map["#{_name}".to_sym]
       raise "No prefix_map named #{_name}, #{$rb_prefix_map.keys} " unless h
       ch = @window.getchar
       if ch
          res =  h[ch]
          if res.is_a? Proc
            res.call
          else
             send(res) if res
          end
       else
          0
       end
    end
  }
  return _name
end