Module: Io
- Included in:
- PromptMenu, RubyCurses::Widget
- Defined in:
- lib/rbcurse/io.rb
Overview
******************************************************* Some common io routines for getting data or putting at some point Arunachalesha
2010-03-06 12:10
Some are outdated.
Current are:
* rbgetstr (and those it calls)
* display_cmenu and create_mitem
*******************************************************#
Defined Under Namespace
Classes: CMenuItem, PromptMenu
Constant Summary collapse
- LINEONE =
from which line to print in footer_win
1
- FOOTER_COLOR_PAIR =
6
- MAIN_WINDOW_COLOR_PAIR =
5
- ERROR_COLOR_PAIR =
7
Instance Method Summary collapse
-
#askchoice(win, askstr, default, labels, validchars, config = {}) ⇒ Object
return single digit from given choices e.g.
-
#askyesno(win, askstr, default = "N", helptext = "Helptext for this question") ⇒ Object
key_labels and error messages.
-
#askyesnocancel(win, askstr, default = "N") ⇒ Object
return y or n or c.
-
#clear_error ⇒ Object
clear previous error, call inside getch loop after each ch.
- #clear_this(win, r, c, color, len) ⇒ Object
- #get_string(win, askstr, maxlen = 20, default = "", labels = nil) ⇒ Object
- #newaskyesno(win, askstr, config = {}) ⇒ Object
-
#old_print_header(win, htext, posy = 0, posx = 0) ⇒ Object
since it must always be @header_win, we should remove the first param why should user have any direct access to those 2 windows.
-
#old_print_top_right(win, htext) ⇒ Object
since it must always be @header_win, we should remove the first param why should user have any direct access to those 2 windows.
-
#print_action(text) ⇒ Object
This is only for the menu program, in which we print current action/menu string in the key labels below.
-
#print_error(text) ⇒ Object
prints error in footer_win only.
- #print_footer_help(helptext) ⇒ Object
- #print_header(htext, posy = 0, posx = 0) ⇒ Object
- #print_headers(form_hash) ⇒ Object
- #print_help(win, r, c, color, helptext) ⇒ Object
- #print_help_page(filename = "TODO") ⇒ Object
-
#print_in_middle(win, starty, startx, width, string, color) ⇒ Object
the old historical program which prints a string in middle of whereever thanks to this i was using stdscr which must never be used.
-
#print_key_labels(posy, posx, arr) ⇒ Object
splits that bad hack array into even and odd arrays and prints on last 2 lines.
- #print_key_labels_row(posy, posx, arr) ⇒ Object
-
#print_screen_labels(my_form_win, labelarr) ⇒ Object
prints labels defined by user in the DSL.
-
#print_status(text) ⇒ Object
prints status in footer_win only.
-
#print_this(win, text, color, x, y) ⇒ Object
prints given text to window, in color at x and y coordinates Consider using Window#printstring.
-
#print_top_right(htext) ⇒ Object
win.refresh.
-
#rbgetstr(win, r, c, prompt, maxlen, config = {}) ⇒ Object
TODO We should put a field there, make it visible and mv it to after the prompt and handle all editing events on it.
Instance Method Details
#askchoice(win, askstr, default, labels, validchars, config = {}) ⇒ Object
return single digit from given choices e.g.
<code>
labels=["N~No ", "Y~Yes ","C~Cancel"," ~ ","S~SurpiseMe","G~GoAway! "]
ret = @main.askchoice(nil, "Are you sure you wish to proceed?","N",labels,"NYCSG")
@main.clear_error
</code>
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 257 258 259 |
# File 'lib/rbcurse/io.rb', line 230 def askchoice(win, askstr, default, labels, validchars, config={}) win ||= @footer_win askstr = "#{askstr} [#{default}]: " len = askstr.length helptext = config.fetch("helptext", "No helptext provided for this action") clear_error # 2008-10-13 20:26 print_this(win, askstr, 4, LINEONE, 0) #labels=["N~No ", "Y~Yes ","C~Cancel"," ~ "] print_key_labels( 0, 0, labels) yn='' validchars.downcase! #win.mvwgetnstr(LINEONE-3,askstr.length,yn,maxlen) while true ch=win.mvwgetch(LINEONE,askstr.length) yn = ch.chr # 2008-10-31 18:08 if ch == ?\C-g or yn == '?' (helptext) print_key_labels( 0, 0, labels) next end yn = default if yn == '' or ch == 10 # KEY_ENTER yn.downcase! break if validchars.include?yn Ncurses.beep end restore_application_key_labels # must be done after using print_key_labels return yn end |
#askyesno(win, askstr, default = "N", helptext = "Helptext for this question") ⇒ Object
key_labels and error messages. WHEN WE CREATE A PANEL BWLOW we can do away wit that. FIXME XXX return true for y, false for n
e.g.
<code>
ret = @main.askyesno(nil, "Are you sure you wish to proceed?")
</code>
2008-10-09 18:27 added call to print_footer_help
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/rbcurse/io.rb', line 160 def askyesno(win, askstr, default = "N", helptext="Helptext for this question") win ||= @footer_win askstr = "#{askstr} [#{default}]: " len = askstr.length clear_error # 2008-10-13 20:26 print_this(win, askstr, 4, LINEONE, 0) labels=["?~Help "," ~ ", "N~No ", "Y~Yes "] print_key_labels( 0, 0, labels) win.refresh #Ncurses.echo(); yn='' #win.mvwgetnstr(Ncurses.LINES-3,askstr.length,yn,maxlen) while true ch=win.mvwgetch(LINEONE,askstr.length) if ch < 0 || ch > 255 next end yn = ch.chr yn = default if yn == '' or ch == 10 # KEY_ENTER yn.downcase! break if yn =~/[yn]/ if yn == '?' (helptext) print_key_labels( 0, 0, labels) next end Ncurses.beep end #Ncurses.noecho(); clear_error # 2008-11-06 19:27 restore_application_key_labels # must be done after using print_key_labels win.refresh return yn == 'y' end |
#askyesnocancel(win, askstr, default = "N") ⇒ Object
return y or n or c
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/rbcurse/io.rb', line 198 def askyesnocancel(win, askstr, default = "N") win ||= @footer_win askstr = "#{askstr} [#{default}]: " len = askstr.length clear_error # 2008-10-13 20:26 print_this(win, askstr, 4, LINEONE, 0) labels=["N~No ", "Y~Yes ","C~Cancel"," ~ "] print_key_labels( 0, 0, labels) win.refresh yn='' #win.mvwgetnstr(LINEONE,askstr.length,yn,maxlen) while true ch=win.mvwgetch(LINEONE,askstr.length) yn = ch.chr yn = default if yn == '' or ch == 10 # KEY_ENTER yn.downcase! break if yn =~/[ync]/ Ncurses.beep end restore_application_key_labels # must be done after using print_key_labels return yn end |
#clear_error ⇒ Object
clear previous error, call inside getch loop after each ch.
321 322 323 |
# File 'lib/rbcurse/io.rb', line 321 def clear_error print_this(@footer_win, "%-*s" % [Ncurses.COLS," "], 5, LINEONE, 0) end |
#clear_this(win, r, c, color, len) ⇒ Object
143 144 145 |
# File 'lib/rbcurse/io.rb', line 143 def clear_this win, r, c, color, len print_this(win, "%-*s" % [len," "], color, r, c) end |
#get_string(win, askstr, maxlen = 20, default = "", labels = nil) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/rbcurse/io.rb', line 260 def get_string(win, askstr, maxlen=20, default="", labels=nil ) win ||= @footer_win askstr = "#{askstr} [#{default}]: " len = askstr.length clear_error # 2008-11-06 19:25 print_this(win, askstr, 4, LINEONE, 0) #labels=["N~No ", "Y~Yes ","C~Cancel"," ~ "] mylabels = ["^G~Help ", "^C~Cancel"] mylabels = (mylabels + labels) if !labels.nil? print_key_labels( 0, 0, mylabels) Ncurses.echo(); yn='' begin Signal.trap("INT"){ return nil } win.mvwgetnstr(LINEONE,askstr.length,yn,maxlen) rescue yn='' ensure Ncurses.noecho(); clear_error # 2008-11-02 11:51 restore_application_key_labels # must be done after using print_key_labels end yn = default if yn == "" # 2008-10-31 18:59 return yn end |
#newaskyesno(win, askstr, config = {}) ⇒ Object
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 |
# File 'lib/rbcurse/io.rb', line 512 def newaskyesno(win, askstr, config = {}) win ||= @footer_win default = config.fetch("default", "N") helptext = config.fetch("helptext", "This is helptext for this action") askstr = "#{askstr} [#{default}]: " len = askstr.length clear_error # 2008-10-13 20:26 print_this(win, askstr, 4, LINEONE, 0) labels=config.fetch("labels", ["?~Help "," ~ ", "N~No ", "Y~Yes "]) print_key_labels( 0, 0, labels) win.refresh yn='' #win.mvwgetnstr(Ncurses.LINES-3,askstr.length,yn,maxlen) while true ch=win.mvwgetch(LINEONE,askstr.length) if ch < 0 || ch > 255 next end yn = ch.chr yn = default if yn == '' or ch == 10 # KEY_ENTER yn.downcase! break if yn =~/[yn]/ if yn == '?' (helptext) print_key_labels( 0, 0, labels) next end Ncurses.beep end # while begin config[yn].call if config.include? yn ensure restore_application_key_labels # must be done after using print_key_labels win.refresh end return yn == 'y' end |
#old_print_header(win, htext, posy = 0, posx = 0) ⇒ Object
since it must always be @header_win, we should remove the first param why should user have any direct access to those 2 windows.
416 417 418 419 420 |
# File 'lib/rbcurse/io.rb', line 416 def old_print_header(win, htext, posy = 0, posx = 0) win.attron(Ncurses.COLOR_PAIR(6)) win.mvprintw(posy, posx, "%-*s" % [Ncurses.COLS, htext] ); win.attroff(Ncurses.COLOR_PAIR(6)) end |
#old_print_top_right(win, htext) ⇒ Object
since it must always be @header_win, we should remove the first param why should user have any direct access to those 2 windows.
430 431 432 433 434 435 436 |
# File 'lib/rbcurse/io.rb', line 430 def old_print_top_right(win, htext) hlen = htext.length win.attron(Ncurses.COLOR_PAIR(6)) win.mvprintw(0, Ncurses.COLS-hlen, htext ); win.attroff(Ncurses.COLOR_PAIR(6)) #win.refresh end |
#print_action(text) ⇒ Object
This is only for the menu program, in which we print current action/menu string in the key labels below. Its a dirty hack edpending on:
* String CurRow present in key labels
* field_init_proc called this method to set it.
329 330 331 |
# File 'lib/rbcurse/io.rb', line 329 def print_action(text) print_this(@footer_win, " %-10s" % ("["+text+"]"), FOOTER_COLOR_PAIR, @action_posy, @action_posx) end |
#print_error(text) ⇒ Object
prints error in footer_win only
311 312 313 314 |
# File 'lib/rbcurse/io.rb', line 311 def print_error(text) clear_error print_in_middle(@footer_win, LINEONE, 10, 80, text, Ncurses.COLOR_PAIR(ERROR_COLOR_PAIR)) end |
#print_footer_help(helptext) ⇒ Object
489 490 491 492 493 494 |
# File 'lib/rbcurse/io.rb', line 489 def (helptext) print_this(@footer_win, "%-*s" % [Ncurses.COLS," "], 6, LINEONE+1, 0) print_this(@footer_win, "%-*s" % [Ncurses.COLS," "], 6, LINEONE+2, 0) print_this(@footer_win, "%s" % helptext, 6, LINEONE+1, 0) sleep(5) end |
#print_header(htext, posy = 0, posx = 0) ⇒ Object
421 422 423 424 425 426 |
# File 'lib/rbcurse/io.rb', line 421 def print_header(htext, posy = 0, posx = 0) win = @header_win win.attron(Ncurses.COLOR_PAIR(6)) win.mvprintw(posy, posx, "%-*s" % [Ncurses.COLS, htext] ); win.attroff(Ncurses.COLOR_PAIR(6)) end |
#print_headers(form_hash) ⇒ Object
470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/rbcurse/io.rb', line 470 def print_headers(form_hash) header = form_hash["header"] header_top_left = form_hash["header_top_left"] || "" header_top_center = form_hash["header_top_center"] || "" header_top_right = form_hash["header_top_right"] || "" posy = 0 posx = 0 htext = " <APP NAME> <VERSION> MAIN MENU" posy, posx, htext = header if !header.nil? print_header(htext + " %15s " % header_top_left + " %20s" % header_top_center , posy, posx) print_top_right(header_top_right) @header_win.wrefresh(); end |
#print_help(win, r, c, color, helptext) ⇒ Object
146 147 148 149 150 |
# File 'lib/rbcurse/io.rb', line 146 def print_help(win, r, c, color, helptext) print_this(win, "%-*s" % [helptext.length+2," "], color, r, c) print_this(win, "%s" % helptext, color, r, c) sleep(5) end |
#print_help_page(filename = "TODO") ⇒ Object
495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'lib/rbcurse/io.rb', line 495 def print_help_page(filename = "TODO") #require 'transactionviewer' #tp = TransactionViewer.new(nil) #tp.run require 'panelreader' tp = PanelReader.new() tp.view_file(filename) #require 'padreader' #tp = PadReader.new() #tp.view_file(filename) end |
#print_in_middle(win, starty, startx, width, string, color) ⇒ Object
the old historical program which prints a string in middle of whereever thanks to this i was using stdscr which must never be used
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/rbcurse/io.rb', line 336 def print_in_middle(win, starty, startx, width, string, color) if(win == nil) raise "window is nil" end x = Array.new y = Array.new Ncurses.getyx(win, y, x); if(startx != 0) x[0] = startx; end if(starty != 0) y[0] = starty; end if(width == 0) width = 80; end length = string.length; temp = (width - length)/ 2; x[0] = startx + temp.floor; win.attron(color); win.mvprintw(y[0], x[0], "%s", string); win.attroff(color); win.refresh(); end |
#print_key_labels(posy, posx, arr) ⇒ Object
splits that bad hack array into even and odd arrays and prints on last 2 lines
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/rbcurse/io.rb', line 363 def print_key_labels(posy, posx, arr) ## paint so-called key bindings from key_labels posx = 0 even = [] odd = [] arr.each_index { |i| if i % 2 == 0 even << arr[i] else odd << arr[i] end } posy = LINEONE+1 print_key_labels_row(posy, posx, even) posy = LINEONE+2 print_key_labels_row(posy, posx, odd) # 2008-09-29 21:58 @footer_win.wrefresh # needed else secod row not shown after askchoice XXX end |
#print_key_labels_row(posy, posx, arr) ⇒ Object
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/rbcurse/io.rb', line 383 def print_key_labels_row(posy, posx, arr) #clear first my_form_win = @footer_win # first clear the line print_this(my_form_win, "%-*s" % [Ncurses.COLS," "], FOOTER_COLOR_PAIR, posy, 0) padding = 8 padding = 4 if arr.length > 5 padding = 0 if arr.length > 7 arr.each_index { |i| kl = arr[i].split('~') if kl[0].strip !="" # don't print that white blank space for fillers color_pair=2 my_form_win.attron(Ncurses.COLOR_PAIR(color_pair)) my_form_win.mvprintw(posy, posx, "%s" % kl[0] ); my_form_win.attroff(Ncurses.COLOR_PAIR(color_pair)) end color_pair=FOOTER_COLOR_PAIR posx = posx + kl[0].length my_form_win.attron(Ncurses.COLOR_PAIR(color_pair)) lab = sprintf(" %s %*s" , kl[1], padding, " "); # hack if kl[1].strip == "CurRow" @action_posx = posx @action_posy = posy end my_form_win.mvprintw(posy, posx, lab) my_form_win.attroff(Ncurses.COLOR_PAIR(color_pair)) posx = posx + lab.length } end |
#print_screen_labels(my_form_win, labelarr) ⇒ Object
prints labels defined by user in the DSL.
Array of labels with:
* position = [y,x] i.e, row, column
* text = "label text"
* color_pair = 6 (optional, default 6)
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
# File 'lib/rbcurse/io.rb', line 453 def print_screen_labels(my_form_win, labelarr) table_width = @table_width || Ncurses.LINES-1 return if labelarr.nil? labelarr.each{ |lhash| posy, posx = lhash["position"] posy = table_width + posy if posy < 0 posx = Ncurses.COLS + posy if posx < 0 text = lhash["text"] color_pair = lhash["color_pair"] || 6 my_form_win.attron(Ncurses.COLOR_PAIR(color_pair)) my_form_win.mvprintw(posy, posx, "%-s" % text ); my_form_win.attroff(Ncurses.COLOR_PAIR(color_pair)) } end |
#print_status(text) ⇒ Object
prints status in footer_win only
316 317 318 319 |
# File 'lib/rbcurse/io.rb', line 316 def print_status(text) text = text[text.length-80..-1] if text.length > 80 print_error(text) end |
#print_this(win, text, color, x, y) ⇒ Object
prints given text to window, in color at x and y coordinates Consider using Window#printstring
298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/rbcurse/io.rb', line 298 def print_this(win, text, color, x, y) if(win == nil) raise "win nil in printthis" end #$log.debug " printthis #{win} , #{text} , #{x} , #{y} " color=Ncurses.COLOR_PAIR(color); win.attron(color); #win.mvprintw(x, y, "%-40s" % text); win.mvprintw(x, y, "%s" % text); win.attroff(color); win.refresh end |
#print_top_right(htext) ⇒ Object
win.refresh
437 438 439 440 441 442 443 444 |
# File 'lib/rbcurse/io.rb', line 437 def print_top_right(htext) hlen = htext.length win = @header_win win.attron(Ncurses.COLOR_PAIR(6)) win.mvprintw(0, Ncurses.COLS-hlen, htext ); win.attroff(Ncurses.COLOR_PAIR(6)) #win.refresh end |
#rbgetstr(win, r, c, prompt, maxlen, config = {}) ⇒ Object
TODO We should put a field there, make it visible and mv it to after the prompt and handle all editing events on it. def rbgetstr(win, r, c, prompt, maxlen, default, labels, validints=[], helptext=“”)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/rbcurse/io.rb', line 27 def rbgetstr(win, r, c, prompt, maxlen, config={}) #win ||= @target_window $log.debug " inside rbgetstr #{win} r:#{r} c:#{c} p:#{prompt} m:#{maxlen} " raise "rbgetstr got no window. io.rb" if win.nil? ins_mode = false default = config[:default] || "" prompt = "#{prompt} [#{default}]: " unless default len = prompt.length # clear the area of len+maxlen color = $datacolor str = default clear_this win, r, c, color, len+maxlen+1 print_this(win, prompt+str, color, r, c) len = prompt.length + str.length #x mylabels=["^G~Help ", "^C~Cancel"] #x mylabels += labels if !labels.nil? begin Ncurses.echo(); #x print_key_labels( 0, 0, mylabels) #curpos = 0 curpos = str.length prevchar = 0 entries = nil #win.mvwgetnstr(LINEONE-3,askstr.length,yn,maxlen) while true #ch=win.mvwgetch(r, len) # get to right of prompt - WHY NOT WORKING ??? ch=win.getchar() $log.debug " rbgetstr got ch:#{ch}, str:#{str}. " case ch when 3 # -1 # C-c return -1, nil when 10, 13 break when ?\C-h.getbyte(0), ?\C-?.getbyte(0), 127 # delete previous character/backspace len -= 1 if len > prompt.length curpos -= 1 if curpos > 0 str.slice!(curpos) clear_this win, r, c, color, len+maxlen+1 #print_this(win, prompt+str, color, r, c) when 330 # delete character on cursor #len -= 1 if len > prompt.length #curpos -= 1 if curpos > 0 str.slice!(curpos) #rescue next clear_this win, r, c, color, len+maxlen+1 when ?\C-g.getbyte(0) #x print_footer_help(helptext) helptext = config[:helptext] || "No help provided" print_help(win, r, c, color, helptext) return 7, nil when KEY_LEFT curpos -= 1 if curpos > 0 len -= 1 if len > prompt.length win.wmove r, c+len # since getchar is not going back on del and bs next when KEY_RIGHT if curpos < str.length curpos += 1 #if curpos < str.length len += 1 win.wmove r, c+len # since getchar is not going back on del and bs end next when ?\M-i.getbyte(0) ins_mode = !ins_mode next when 9 # TAB if config if prevchar == 9 if !entries.nil? and !entries.empty? str = entries.delete_at(0) end else tabc = config[:tab_completion] unless tabc next unless tabc entries = tabc.call(str) $log.debug " tab got #{entries} " str = entries.delete_at(0) unless entries.nil? or entries.empty? end end else #if validints.include?ch #print_status("Found in validints") #return ch, nil #else if ch < 0 || ch > 255 Ncurses.beep next end # if control char, beep if ch.chr =~ /[[:cntrl:]]/ Ncurses.beep next end # we need to trap KEY_LEFT and RIGHT and what of UP for history ? #end #str << ch.chr if ins_mode str[curpos] = ch.chr else str.insert(curpos, ch.chr) end len += 1 curpos += 1 break if str.length > maxlen end print_this(win, prompt+str, color, r, c) win.wmove r, c+len # more for arrow keys, curpos may not be end prevchar = ch end str = default if str == "" ensure Ncurses.noecho(); #x restore_application_key_labels # must be done after using print_key_labels end return 0, str end |