Module: RubyCurses::Utils
- Included in:
- BasicListCellRenderer, CellEditor, ComboBoxCellRenderer, CommandWindow, Form, ListCellRenderer, MessageBox, TableCellRenderer, TableDateCellRenderer, TreeCellRenderer, Widget
- Defined in:
- lib/rbcurse/rwidget.rb
Instance Method Summary collapse
-
#_process_key(keycode, object, window) ⇒ Object
e.g.
-
#bind_key(keycode, *args, &blk) ⇒ Object
bind an action to a key, required if you create a button which has a hotkey or a field to be focussed on a key, or any other user defined action based on key e.g.
- #clean_string!(content) ⇒ Object
-
#get_color(default = $datacolor, color = @color, bgcolor = @bgcolor) ⇒ Object
if passed a string in second or third param, will create a color and return, else it will return default color Use this in order to create a color pair with the colors provided, however, if user has not provided, use supplied default.
-
#keycode_tos(keycode) ⇒ Object
needs to move to a keystroke class please use these only for printing or debugging, not comparing I could soon return symbols instead 2010-09-07 14:14.
-
#repeatm ⇒ Object
repeats the given action based on how value of universal numerica argument + set using the C-u key.
-
#view(what, config = {}) ⇒ Object
view a file or array of strings.
-
#wrap_text(txt, max) ⇒ Object
wraps text given max length, puts newlines in it.
Instance Method Details
#_process_key(keycode, object, window) ⇒ Object
e.g. process_key ch, self returns UNHANDLED if no block for it after form handles basic keys, it gives unhandled key to current field, if current field returns unhandled, then it checks this map. added 2009-01-06 19:13 since widgets need to handle keys properly added 2009-01-18 12:58 returns ret val of blk.call so that if block does not handle, the key can still be handled e.g. table last row, last col does not handle, so it will auto go to next field
2010-02-24 13:45 handles 2 key combinations, copied from Form, must be identical in logic
except maybe for window pointer. TODO not tested
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/rbcurse/rwidget.rb', line 333 def _process_key keycode, object, window return :UNHANDLED if @key_handler.nil? blk = @key_handler[keycode] return :UNHANDLED if blk.nil? if blk.is_a? OrderedHash ch = window.getch if ch < 0 || ch > 255 #next return nil end $log.debug " process_key: got #{keycode} , #{ch} " yn = ch.chr blk1 = blk[ch] window.ungetch(ch) if blk1.nil? # trying 2011-09-27 return :UNHANDLED if blk1.nil? # changed nil to unhandled 2011-09-27 $log.debug " process_key: found block for #{keycode} , #{ch} " blk = blk1 end #$log.debug "called process_key #{object}, kc: #{keycode}, args #{@key_args[keycode]}" if blk.is_a? Symbol $log.debug "SYMBOL " if $log.debug? if respond_to? blk return send(blk, *@key_args[keycode]) else alert "This ( #{self.class} ) does not respond to #{blk.to_s} " end else $log.debug "rwidget BLOCK called _process_key " if $log.debug? return blk.call object, *@key_args[keycode] end #0 end |
#bind_key(keycode, *args, &blk) ⇒ Object
bind an action to a key, required if you create a button which has a hotkey or a field to be focussed on a key, or any other user defined action based on key e.g. bind_key ?C-x, object, block added 2009-01-06 19:13 since widgets need to handle keys properly
2010-02-24 12:43 to take in multiple key bindings, TODO unbind
TODO add symbol so easy to map from config file or mapping file
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/rbcurse/rwidget.rb', line 291 def bind_key keycode, *args, &blk $log.debug " #{@name} bind_key received #{keycode} " @key_handler ||= {} if !block_given? blk = args.pop raise "If block not passed, last arg should be a method symbol" if !blk.is_a? Symbol #$log.debug " #{@name} bind_key received a symbol #{blk} " end case keycode when String keycode = keycode.getbyte(0) #if keycode.class==String ## 1.9 2009-10-05 19:40 #$log.debug " #{name} Widg String called bind_key BIND #{keycode}, #{keycode_tos(keycode)} " $log.debug " assigning #{keycode} " if $log.debug? @key_handler[keycode] = blk when Array # for starters lets try with 2 keys only raise "A one key array will not work. Pass without array" if keycode.size == 1 a0 = keycode[0] a0 = keycode[0].getbyte(0) if keycode[0].class == String a1 = keycode[1] a1 = keycode[1].getbyte(0) if keycode[1].class == String @key_handler[a0] ||= OrderedHash.new $log.debug " assigning #{keycode} , A0 #{a0} , A1 #{a1} " if $log.debug? @key_handler[a0][a1] = blk #$log.debug " XX assigning #{keycode} to key_handler " if $log.debug? else #$log.debug " assigning #{keycode} to key_handler " if $log.debug? @key_handler[keycode] = blk end @key_args ||= {} @key_args[keycode] = args end |
#clean_string!(content) ⇒ Object
188 189 190 191 192 193 |
# File 'lib/rbcurse/rwidget.rb', line 188 def clean_string! content content.chomp! # don't display newline content.gsub!(/[\t\n]/, ' ') # don't display tab content.gsub!(/[^[:print:]]/, '') # don't display non print characters content end |
#get_color(default = $datacolor, color = @color, bgcolor = @bgcolor) ⇒ Object
if passed a string in second or third param, will create a color and return, else it will return default color Use this in order to create a color pair with the colors provided, however, if user has not provided, use supplied default.
267 268 269 270 271 272 273 |
# File 'lib/rbcurse/rwidget.rb', line 267 def get_color default=$datacolor, color=@color, bgcolor=@bgcolor return default if color.nil? || bgcolor.nil? raise ArgumentError, "Color not valid: #{color}: #{ColorMap.colors} " if !ColorMap.is_color? color raise ArgumentError, "Bgolor not valid: #{bgcolor} : #{ColorMap.colors} " if !ColorMap.is_color? bgcolor acolor = ColorMap.get_color(color, bgcolor) return acolor end |
#keycode_tos(keycode) ⇒ Object
needs to move to a keystroke class please use these only for printing or debugging, not comparing I could soon return symbols instead 2010-09-07 14:14
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/rbcurse/rwidget.rb', line 197 def keycode_tos keycode case keycode when 33..126 return keycode.chr when ?\C-a.getbyte(0) .. ?\C-z.getbyte(0) return "C-" + (keycode + ?a.getbyte(0) -1).chr when ?\M-A.getbyte(0)..?\M-z.getbyte(0) return "M-"+ (keycode - 128).chr when ?\M-\C-A.getbyte(0)..?\M-\C-Z.getbyte(0) return "M-C-"+ (keycode - 32).chr when ?\M-0.getbyte(0)..?\M-9.getbyte(0) return "M-"+ (keycode-?\M-0.getbyte(0)).to_s when 32 return "space" # changed to lowercase so consistent when 27 return "esc" # changed to lowercase so consistent when ?\C-].getbyte(0) return "C-]" when 258 return "down" when 259 return "up" when 260 return "left" when 261 return "right" when FFI::NCurses::KEY_F1..FFI::NCurses::KEY_F12 return "F"+ (keycode-264).to_s when 330 return "delete" when 127 return "bs" when 353 return "btab" when 481 return "M-S-tab" when 393..402 return "M-F"+ (keycode-392).to_s when 0 return "C-space" # i hope this is correct, just guessing when 160 return "M-space" # at least on OSX Leopard now (don't remember this working on PPC) when C_LEFT return "C-left" when C_RIGHT return "C-right" when S_F9 return "S_F9" else others=[?\M--,?\M-+,?\M-=,?\M-',?\M-",?\M-;,?\M-:,?\M-\,, ?\M-.,?\M-<,?\M->,?\M-?,?\M-/] others.collect! {|x| x.getbyte(0) } ## added 2009-10-04 14:25 for 1.9 s_others=%w[M-- M-+ M-= M-' M-" M-; M-: M-, M-. M-< M-> M-? M-/ ] if others.include? keycode index = others.index keycode return s_others[index] end # all else failed return keycode.to_s end end |
#repeatm ⇒ Object
repeats the given action based on how value of universal numerica argument + set using the C-u key. Or in vim-mode using numeric keys
276 277 278 279 280 281 282 |
# File 'lib/rbcurse/rwidget.rb', line 276 def repeatm $inside_multiplier_action = true _multiplier = ( ($multiplier.nil? || $multiplier == 0) ? 1 : $multiplier ) _multiplier.times { yield } $multiplier = 0 $inside_multiplier_action = false end |
#view(what, config = {}) ⇒ Object
view a file or array of strings
366 367 368 369 |
# File 'lib/rbcurse/rwidget.rb', line 366 def view what, config={} # :yields: textview for further configuration require 'rbcurse/extras/viewer' RubyCurses::Viewer.view what, config end |
#wrap_text(txt, max) ⇒ Object
wraps text given max length, puts newlines in it. it does not take into account existing newlines Some classes have @maxlen or display_length which may be passed as the second parameter
184 185 186 187 |
# File 'lib/rbcurse/rwidget.rb', line 184 def wrap_text(txt, max ) txt.gsub(/(.{1,#{max}})( +|$\n?)|(.{1,#{max}})/, "\\1\\3\n") end |