Class: RDialogNG

Inherits:
Object
  • Object
show all
Defined in:
lib/rdialog-ng/rdialog-ng.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRDialogNG

Returns a new RDialog Object



121
122
# File 'lib/rdialog-ng/rdialog-ng.rb', line 121

def initialize()
end

Instance Attribute Details

#aspectObject

This gives you some control over the box dimensions when using auto sizing (specifying 0 for height and width). It represents width / height. The default is 9, which means 9 characters wide to every 1 line high.



23
24
25
# File 'lib/rdialog-ng/rdialog-ng.rb', line 23

def aspect
  @aspect
end

#backtitleObject

Specifies a backtitle string to be displayed on the backdrop, at the top of the screen.



29
30
31
# File 'lib/rdialog-ng/rdialog-ng.rb', line 29

def backtitle
  @backtitle
end

#beepObject

Sound the audible alarm each time the screen is refreshed.



34
35
36
# File 'lib/rdialog-ng/rdialog-ng.rb', line 34

def beep
  @beep
end

#beginObject

Specify the position of the upper left corner of a dialog box on the screen, as an array containing two integers.



40
41
42
# File 'lib/rdialog-ng/rdialog-ng.rb', line 40

def begin
  @begin
end

#cancellabelObject

Override the label used for “Cancel” buttons.



45
46
47
# File 'lib/rdialog-ng/rdialog-ng.rb', line 45

def cancellabel
  @cancellabel
end

#crwrapObject

Interpret embedded newlines in the dialog text as a newline on the screen. Otherwise, dialog will only wrap lines where needed to fit inside the text box. Even though you can control line breaks with this, dialog will still wrap any lines that are too long for the width of the box. Without cr-wrap, the layout of your text may be formatted to look nice in the source code of your script without affecting the way it will look in the dialog.



56
57
58
# File 'lib/rdialog-ng/rdialog-ng.rb', line 56

def crwrap
  @crwrap
end

#itemhelpObject

Interpret the tags data for checklist, radiolist and menuboxes adding a column which is displayed in the bottom line of the screen, for the currently selected item.



63
64
65
# File 'lib/rdialog-ng/rdialog-ng.rb', line 63

def itemhelp
  @itemhelp
end

#nocancelObject

Suppress the “Cancel” button in checklist, inputbox and menubox modes. A script can still test if the user pressed the ESC key to cancel to quit.



70
71
72
# File 'lib/rdialog-ng/rdialog-ng.rb', line 70

def nocancel
  @nocancel
end

#oklabelObject

Override the label used for “Ok” buttons.



75
76
77
# File 'lib/rdialog-ng/rdialog-ng.rb', line 75

def oklabel
  @oklabel
end

#path_to_dialogObject

Alternate path to dialog. If this is not set, environment path is used.



117
118
119
# File 'lib/rdialog-ng/rdialog-ng.rb', line 117

def path_to_dialog
  @path_to_dialog
end

#shadowObject

Draw a shadow to the right and bottom of each dialog box.



80
81
82
# File 'lib/rdialog-ng/rdialog-ng.rb', line 80

def shadow
  @shadow
end

#sleepObject

Sleep (delay) for the given integer of seconds after processing a dialog box.



86
87
88
# File 'lib/rdialog-ng/rdialog-ng.rb', line 86

def sleep
  @sleep
end

#tabcorrectObject

Convert each tab character to one or more spaces. Otherwise, tabs are rendered according to the curses library’s interpretation.



93
94
95
# File 'lib/rdialog-ng/rdialog-ng.rb', line 93

def tabcorrect
  @tabcorrect
end

#tablenObject

Specify the number(int) of spaces that a tab character occupies if the tabcorrect option is set true. The default is 8.



99
100
101
# File 'lib/rdialog-ng/rdialog-ng.rb', line 99

def tablen
  @tablen
end

#timeoutObject

Specify the timeout in secs Timeout (exit with error code) if no user response within the given number of seconds. This is overridden if the background “–tailboxbg is used. A timeout of zero seconds is ignored.



107
108
109
# File 'lib/rdialog-ng/rdialog-ng.rb', line 107

def timeout
  @timeout
end

#titleObject

Title string to be displayed at the top of the dialog box.



112
113
114
# File 'lib/rdialog-ng/rdialog-ng.rb', line 112

def title
  @title
end

Instance Method Details

#calendar(text = "Select a Date", height = 0, width = 0, day = Date.today.mday(), month = Date.today.mon(), year = Date.today.year()) ⇒ Object

A calendar box displays month, day and year in separately

adjustable  windows.   If the values for day, month or year are
missing or negative, the current  date's  corresponding  values
are  used.   You  can increment or decrement any of those using
the left-, up-, right- and down-arrows.  Use vi-style h,  j,  k
and  l for moving around the array of days in a month.  Use tab
or backtab to move between windows.  If the year  is  given  as
zero, the current date is used as an initial value.

Returns a Date object with the selected date


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rdialog-ng/rdialog-ng.rb', line 134

def calendar(text="Select a Date", height=0, width=0, day=Date.today.mday(), month=Date.today.mon(), year=Date.today.year())

  tmp = Tempfile.new('tmp')

  command = option_string() + "--calendar \"" + text.to_s +
    "\" " + height.to_i.to_s + " " + width.to_i.to_s + " " +
    day.to_i.to_s + " " + month.to_i.to_s + " " + year.to_i.to_s +
    " 2> " + tmp.path
  success = system(command)
  if success
    date = Date::civil(*tmp.readline.split('/').collect {|i| i.to_i}.reverse)
    tmp.close!
    return date
  else
    tmp.close!
    return success
  end

end

#checklist(text, items, height = 0, width = 0, listheight = 0) ⇒ Object

A checklist box is similar to a menu box; there are multiple

entries presented in the form of a menu.  Instead  of  choosing
one entry among the entries, each entry can be turned on or off
by the user.  The initial on/off state of each entry is  speci-
fied by status.


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
195
196
197
198
# File 'lib/rdialog-ng/rdialog-ng.rb', line 160

def checklist(text, items, height=0, width=0, listheight=0)

  tmp = Tempfile.new('tmp')

  itemlist = String.new

  for item in items
    if item[2]
      item[2] = "on"
    else
      item[2] = "off"
    end
    itemlist += "\"" + item[0].to_s + "\" \"" + item[1].to_s +
      "\" " + item[2] + " "

    if @itemhelp
      itemlist += "\"" + item[3].to_s + "\" "
    end
  end

  command = option_string() + "--separate-output --checklist \"" + text.to_s +
    "\" " + height.to_i.to_s + " " + width.to_i.to_s +
    " " + listheight.to_i.to_s + " " + itemlist + "2> " +
    tmp.path
  puts command
  success = system(command)
  puts success
  if success
    selected_array = []
    while !tmp.eof? do
      selected_array << tmp.readline.strip
    end
    return selected_array
  else
    tmp.close!
    return success
  end

end

#fselect(path, height = 0, width = 0) ⇒ Object

Here filepath can be a filepath in which case the file and

directory windows will display the contents of the path and the
text-entry window will contain the preselected filename.

Use  tab or arrow keys to move between the windows.  Within the
directory or filename windows, use the up/down  arrow  keys  to
scroll  the  current  selection.  Use the space-bar to copy the
current selection into the text-entry window.

Typing any printable characters switches  focus  to  the  text-
entry  window, entering that character as well as scrolling the
directory and filename windows to the closest match.

Use a carriage return or the "OK" button to accept the  current
value in the text-entry window and exit.


220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/rdialog-ng/rdialog-ng.rb', line 220

def fselect(path, height=0, width=0)
  tmp = Tempfile.new('tmp')

  command = option_string() + "--fselect \"" + path.to_s +
  "\" " + height.to_i.to_s + " " + width.to_i.to_s + " "

  command += "2> " + tmp.path

  success = system(command)

  if success
    begin
      selected_string = tmp.readline
    rescue EOFError
      selected_string = ""
    end
    tmp.close!
    return selected_string
  else
    tmp.close!
    return success
  end
end

#gauge(text, value, height = 0, width = 0) ⇒ Object

A gauge is basically an info box with showing of a percentage value,

given as the second parameter in the range from 0 to 100


247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/rdialog-ng/rdialog-ng.rb', line 247

def gauge(text, value, height=0, width=0)
  begin
    intval = value.to_i
  rescue
    return false
  end

  command = option_string() + "--gauge \""+text.to_s+"\" " + height.to_i.to_s + " " + width.to_i.to_s + " 0"

  success  = system("echo #{intval} | "+command)

  return success
end

#infobox(text, height = 0, width = 0) ⇒ Object

An info box is basically a message box. However, in this case,

dialog  will  exit  immediately after displaying the message to
the user.  The screen is not cleared when dialog exits, so that
the  message  will remain on the screen until the calling shell
script clears it later.  This is useful when you want to inform
the  user that some operations are carrying on that may require
some time to finish.

Returns false if esc was pushed


271
272
273
274
275
276
# File 'lib/rdialog-ng/rdialog-ng.rb', line 271

def infobox(text, height=0, width=0)
  command = option_string() + "--infobox \"" + text.to_s +
    "\" " + height.to_i.to_s + " " + width.to_i.to_s + " "
  success = system(command)
  return success
end

#inputbox(text = "Please enter some text", height = 0, width = 0, init = "") ⇒ Object



510
511
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
# File 'lib/rdialog-ng/rdialog-ng.rb', line 510

def inputbox(text="Please enter some text", height=0, width=0, init="")
  tmp = Tempfile.new('tmp')

  command = option_string() + "--inputbox \"" + text.to_s +
    "\" " + height.to_i.to_s + " " + width.to_i.to_s + " "

  unless init.empty?
    command += init.to_s + " "
  end

  command += "2> " + tmp.path

  success = system(command)

  if success
    begin
      selected_string = tmp.readline
    rescue EOFError
      selected_string = ""
    end
    tmp.close!
    return selected_string
  else
    tmp.close!
    return success
  end
end

As its name suggests, a menu box is a dialog box that can be

used to present a list of choices in the form of a menu for the
user  to  choose.   Choices  are  displayed in the order given.
Each menu entry consists of a tag string and  an  item  string.
The tag gives the entry a name to distinguish it from the other
entries in the menu.  The item is a short  description  of  the
option  that  the  entry represents.  The user can move between
the menu entries by pressing the cursor keys, the first  letter
of  the  tag  as  a  hot-key, or the number keys 1-9. There are
menu-height entries displayed in the menu at one time, but  the
menu will be scrolled if there are more entries than that.

Returns a string containing the tag of the chosen menu entry.


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
# File 'lib/rdialog-ng/rdialog-ng.rb', line 333

def menu(text="Text Goes Here", items=nil, height=0, width=0, listheight=0)
  tmp = Tempfile.new('tmp')

  itemlist = String.new

  for item in items
    itemlist += "\"" + item[0].to_s + "\" \"" + item[1].to_s +  "\" "

    if @itemhelp
      itemlist += "\"" + item[2].to_s + "\" "
    end
  end

  command = option_string() + "--menu \"" + text.to_s +
    "\" " + height.to_i.to_s + " " + width.to_i.to_s +
    " " + listheight.to_i.to_s + " " + itemlist + "2> " +
    tmp.path
  success = system(command)

  if success
    selected_string = tmp.readline
    tmp.close!
    return selected_string
  else
    tmp.close!
    return success
  end

end

#msgbox(text = "Text Goes Here", height = 0, width = 0) ⇒ Object

A message box is very similar to a yes/no box. The only dif-

ference  between  a message box and a yes/no box is that a mes-
sage box has only a single OK button.  You can use this  dialog
box  to  display  any message you like.  After reading the mes-
sage, the user can press the ENTER key so that dialog will exit
and the calling shell script can continue its operation.


370
371
372
373
374
375
376
# File 'lib/rdialog-ng/rdialog-ng.rb', line 370

def msgbox(text="Text Goes Here", height=0, width=0)
  command = option_string() + "--msgbox \"" + text.to_s +
    "\" " + height.to_i.to_s + " " + width.to_i.to_s + " "

  success = system(command)
  return success
end

#passwordbox(text = "Please enter some text", height = 0, width = 0, init = "") ⇒ Object

A password box is similar to an input box, except that the text

the user enters is not displayed.  This is useful when  prompt-
ing  for  passwords  or  other sensitive information.  Be aware
that if anything is passed in "init", it will be visible in the
system's  process  table  to casual snoopers.  Also, it is very
confusing to the user to provide them with a  default  password
they  cannot  see.   For  these reasons, using "init" is highly
discouraged.


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/rdialog-ng/rdialog-ng.rb', line 387

def passwordbox(text="Please enter some text", height=0, width=0, init="")
  tmp = Tempfile.new('tmp')
  command = option_string() + "--passwordbox \"" + text.to_s +
    "\" " + height.to_i.to_s + " " + width.to_i.to_s + " "

  unless init.empty?
    command += init.to_s + " "
  end

  command += "2> " + tmp.path

  success = system(command)

  if success
    begin
      selected_string = tmp.readline
    rescue EOFError
      selected_string = ""
    end
    tmp.close!
    return selected_string
  else
    tmp.close!
    return success
  end
end

#radiolist(text, items, height = 0, width = 0, listheight = 0) ⇒ Object

A radiolist box is similar to a menu box. The only difference

is that you can indicate which entry is currently selected,  by
setting its status to true.


282
283
284
285
286
287
288
289
290
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
# File 'lib/rdialog-ng/rdialog-ng.rb', line 282

def radiolist(text, items, height=0, width=0, listheight=0)

  tmp = Tempfile.new('tmp')

  itemlist = String.new

  for item in items
    if item[2]
      item[2] = "on"
    else
      item[2] = "off"
    end
    itemlist += "\"" + item[0].to_s + "\" \"" + item[1].to_s +
      "\" " + item[2] + " "

    if @itemhelp
      itemlist += "\"" + item[3].to_s + "\" "
    end
  end

  command = option_string() + "--radiolist \"" + text.to_s +
    "\" " + height.to_i.to_s + " " + width.to_i.to_s +
    " " + listheight.to_i.to_s + " " + itemlist + "2> " +
    tmp.path
  success = system(command)

  if success
    selected_string = tmp.readline
    tmp.close!
    return selected_string
  else
    close!
    return success
  end

end

#textbox(file, type = "text", height = 0, width = 0) ⇒ Object

The textbox method handles three similar dialog functions, textbox,

 tailbox, and tailboxbg. They are activated by setting type to
 "text", "tail", and "bg" respectively

 Textbox mode:
A  text  box  lets you display the contents of a text file in a
dialog box.  It is like a simple text file  viewer.   The  user
can  move  through  the file by using the cursor, PGUP/PGDN and
HOME/END keys available on most keyboards.  If  the  lines  are
too long to be displayed in the box, the LEFT/RIGHT keys can be
used to scroll the text region horizontally.  You may also  use
vi-style  keys h, j, k, l in place of the cursor keys, and B or
N in place of the pageup/pagedown keys.  Scroll  up/down  using
vi-style  'k'  and 'j', or arrow-keys.  Scroll left/right using
vi-style  'h'  and  'l',  or  arrow-keys.   A  '0'  resets  the
left/right  scrolling.   For more convenience, vi-style forward
and backward searching functions are also provided.

 Tailbox mode:
Display text from a file in a dialog box, as  in  a  "tail  -f"
command.   Scroll  left/right  using  vi-style  'h' and 'l', or
arrow-keys.  A '0' resets the scrolling.

 Tailboxbg mode:
Display text from a file in a dialog box as a background  task,
as  in a "tail -f &" command.  Scroll left/right using vi-style
'h' and 'l', or arrow-keys.  A '0' resets the scrolling.


442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/rdialog-ng/rdialog-ng.rb', line 442

def textbox(file, type="text", height=0, width=0)
  temp = Tempfile.new('tmp')

  case type
    when "text"
      opt = "--textbox"
    when "tail"
      opt = "--tailbox"
    when "bg"
      opt = "--textboxbg"
    when "edit"
      opt = "--editbox"
  end

  command = option_string() + opt +" \"" + file.to_s +
    "\" " + height.to_i.to_s + " " + width.to_i.to_s + " 2>" + temp.path

  success = system(command)

  if success
    data = temp.read()
    temp.close!
    return data
  else
    temp.close!
    return success
  end
end

#timebox(file, type = "text", height = 0, width = 0, time = Time.now) ⇒ Object

A dialog is displayed which allows you to select hour, minute

and second.  If the values for hour, minute or second are miss-
ing or negative, the current date's  corresponding  values  are
used.   You  can  increment or decrement any of those using the
left-, up-, right- and down-arrows.  Use tab or backtab to move
between windows.

On  exit, a Time object is returned.


480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/rdialog-ng/rdialog-ng.rb', line 480

def timebox(file, type="text", height=0, width=0, time=Time.now)
  tmp = Tempfile.new('tmp')

  command = option_string() + "--timebox \"" + text.to_s +
    "\" " + height.to_i.to_s + " " + width.to_i.to_s + " " +
    time.hour.to_s + " " + time.min.to_s + " " +
    time.sec.to_s + " 2> " + tmp.path
  success = system(command)
  if success
    time = Time.parse(tmp.readline)
    tmp.close!
    return time
  else
    tmp.close!
    return success
  end

end

#yesno(text = "Please enter some text", height = 0, width = 0) ⇒ Object

A yes/no dialog box of size height rows by width columns will

be displayed.  The string specified by text is displayed inside
the dialog box.  If this string is too long to fit in one line,
it  will be automatically divided into multiple lines at appro-
priate places.  The text string can also contain the sub-string
"\n"  or  newline  characters  '\n'  to  control  line breaking
explicitly.  This dialog box is  useful  for  asking  questions
that  require  the user to answer either yes or no.  The dialog
box has a Yes button and a No button, in  which  the  user  can
switch between by pressing the TAB key.


549
550
551
552
553
554
555
# File 'lib/rdialog-ng/rdialog-ng.rb', line 549

def yesno(text="Please enter some text", height=0, width=0)
  command = option_string() + "--yesno \"" + text.to_s +
          "\" " + height.to_i.to_s + " " + width.to_i.to_s

  success = system(command)
  return success
end