Class: RubyCurses::PrefixCommand

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

Overview

utils

Since:

  • 1.2.0 UNTESTED

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:

Since:

  • 1.2.0 UNTESTED



121
122
123
124
125
126
127
# File 'lib/rbcurse/core/include/appmethods.rb', line 121

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

Since:

  • 1.2.0 UNTESTED



120
121
122
# File 'lib/rbcurse/core/include/appmethods.rb', line 120

def object
  @object
end

Instance Method Details

#callObject

Since:

  • 1.2.0 UNTESTED



157
158
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/rbcurse/core/include/appmethods.rb', line 157

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)

Since:

  • 1.2.0 UNTESTED



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/rbcurse/core/include/appmethods.rb', line 187

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

Since:

  • 1.2.0 UNTESTED



128
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
155
156
# File 'lib/rbcurse/core/include/appmethods.rb', line 128

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