Class: Canis::PrefixCommand
- Defined in:
- lib/canis/core/include/appmethods.rb
Overview
utils
Instance Attribute Summary collapse
Instance Method Summary collapse
- #call ⇒ Object
-
#define_key(_keycode, *args, &blk) ⇒ Object
(also: #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.
- #define_prefix_command(_name, config = {}) ⇒ Object
-
#initialize(_symbol, calling, config = {}) {|_self| ... } ⇒ PrefixCommand
constructor
A new instance of PrefixCommand.
Constructor Details
#initialize(_symbol, calling, config = {}) {|_self| ... } ⇒ PrefixCommand
Returns a new instance of PrefixCommand.
93 94 95 96 97 98 99 |
# File 'lib/canis/core/include/appmethods.rb', line 93 def initialize _symbol, calling, config={}, &block @object = calling @symbol = _symbol @descriptions = {} define_prefix_command _symbol yield self if block_given? end |
Instance Attribute Details
#object ⇒ Object
92 93 94 |
# File 'lib/canis/core/include/appmethods.rb', line 92 def object @object end |
Instance Method Details
#call ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/canis/core/include/appmethods.rb', line 129 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
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/canis/core/include/appmethods.rb', line 159 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
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/canis/core/include/appmethods.rb', line 100 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 |