Module: RubyCurses::Utils
- Included in:
- CellEditor, CheckBoxCellRenderer, ComboBoxCellRenderer, Form, ListCellRenderer, MessageBox, TableCellRenderer, TableDateCellRenderer, 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
-
#keycode_tos(keycode) ⇒ Object
needs to move to a keystroke class.
-
#repeatm ⇒ Object
repeats the given action based on how value of universal numerica argument + set using the C-u key.
-
#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
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/rbcurse/rwidget.rb', line 244 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] return nil if blk1.nil? $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 return send(blk, *@key_args[keycode]) else 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 trying to take in multiple key bindings, TODO unbind
TODO add symbol so easy to map from config file or mapping file
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 |
# File 'lib/rbcurse/rwidget.rb', line 208 def bind_key keycode, *args, &blk @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)} " @key_handler[keycode] = blk when Array # for starters lets try with 2 keys only 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 @key_handler[a0][a1] = blk else @key_handler[keycode] = blk end @key_args ||= {} @key_args[keycode] = args end |
#clean_string!(content) ⇒ Object
123 124 125 126 127 128 |
# File 'lib/rbcurse/rwidget.rb', line 123 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
183 184 185 186 187 188 189 190 |
# File 'lib/rbcurse/rwidget.rb', line 183 def get_color default=$datacolor, color=@color, bgcolor=@bgcolor if bgcolor.is_a? String and color.is_a? String acolor = ColorMap.get_color(color, bgcolor) else acolor = default end return acolor end |
#keycode_tos(keycode) ⇒ Object
needs to move to a keystroke class
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 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 |
# File 'lib/rbcurse/rwidget.rb', line 130 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" when 27 return "Esc" when ?\C-].getbyte(0) return "C-]" when 258 return "down" when 259 return "up" when 260 return "left" when 261 return "right" when KEY_F1..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 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
193 194 195 196 197 198 199 |
# File 'lib/rbcurse/rwidget.rb', line 193 def repeatm $inside_multiplier_action = true _multiplier = ( ($multiplier.nil? || $multiplier == 0) ? 1 : $multiplier ) _multiplier.times { yield } $multiplier = 0 $inside_multiplier_action = false 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
119 120 121 122 |
# File 'lib/rbcurse/rwidget.rb', line 119 def wrap_text(txt, max ) txt.gsub(/(.{1,#{max}})( +|$\n?)|(.{1,#{max}})/, "\\1\\3\n") end |