Class: MRDialog

Inherits:
Object
  • Object
show all
Defined in:
lib/mrdialog/mrdialog.rb

Constant Summary collapse

DIALOG_OK =
0
DIALOG_CANCEL =
1
DIALOG_HELP =
2
DIALOG_EXTRA =
3
DIALOG_ITEM_HELP =
4
DIALOG_ESC =
255

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMRDialog

Returns a new RDialog Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/mrdialog/mrdialog.rb', line 195

def initialize
    # [email protected] mod starts--
    $stdout.sync = true
    $stderr.sync = true
#        @tags = true
    @dialog_ok = DIALOG_OK
    @dialog_cancel = DIALOG_CANCEL
    @dialog_help = DIALOG_HELP
    @dialog_extra = DIALOG_EXTRA
    @dialog_item_help = DIALOG_ITEM_HELP
    @dialog_esc = DIALOG_ESC
    @exit_code = 0
    # [email protected] mod ends--
end

Instance Attribute Details

#ascii_linesObject

rather than draw graphics lines around boxes, draw ASCII + and -



181
182
183
# File 'lib/mrdialog/mrdialog.rb', line 181

def ascii_lines
  @ascii_lines
end

#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.



51
52
53
# File 'lib/mrdialog/mrdialog.rb', line 51

def aspect
  @aspect
end

#backtitleObject

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



57
58
59
# File 'lib/mrdialog/mrdialog.rb', line 57

def backtitle
  @backtitle
end

#beepObject

Sound the audible alarm each time the screen is refreshed.



62
63
64
# File 'lib/mrdialog/mrdialog.rb', line 62

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.



68
69
70
# File 'lib/mrdialog/mrdialog.rb', line 68

def begin
  @begin
end

#cancel_labelObject

Override the label used for “Cancel” buttons



153
154
155
# File 'lib/mrdialog/mrdialog.rb', line 153

def cancel_label
  @cancel_label
end

#clearObject

clear screen



174
175
176
# File 'lib/mrdialog/mrdialog.rb', line 174

def clear
  @clear
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.



79
80
81
# File 'lib/mrdialog/mrdialog.rb', line 79

def crwrap
  @crwrap
end

#dialog_cancelObject

Returns the value of attribute dialog_cancel.



136
137
138
# File 'lib/mrdialog/mrdialog.rb', line 136

def dialog_cancel
  @dialog_cancel
end

#dialog_escObject

Returns the value of attribute dialog_esc.



139
140
141
# File 'lib/mrdialog/mrdialog.rb', line 139

def dialog_esc
  @dialog_esc
end

#dialog_extraObject

Returns the value of attribute dialog_extra.



140
141
142
# File 'lib/mrdialog/mrdialog.rb', line 140

def dialog_extra
  @dialog_extra
end

#dialog_helpObject

Returns the value of attribute dialog_help.



137
138
139
# File 'lib/mrdialog/mrdialog.rb', line 137

def dialog_help
  @dialog_help
end

#dialog_item_helpObject

Returns the value of attribute dialog_item_help.



138
139
140
# File 'lib/mrdialog/mrdialog.rb', line 138

def dialog_item_help
  @dialog_item_help
end

#dialog_okObject

exit codes



135
136
137
# File 'lib/mrdialog/mrdialog.rb', line 135

def dialog_ok
  @dialog_ok
end

#dialog_optionsObject

pass dialog’s option exactly



143
144
145
# File 'lib/mrdialog/mrdialog.rb', line 143

def dialog_options
  @dialog_options
end

#extra_buttonObject

Add an “Extra” button



168
169
170
# File 'lib/mrdialog/mrdialog.rb', line 168

def extra_button
  @extra_button
end

#extra_labelObject

Override the label used for the “Extra” button



171
172
173
# File 'lib/mrdialog/mrdialog.rb', line 171

def extra_label
  @extra_label
end

#help_buttonObject

Add a “Help” button



162
163
164
# File 'lib/mrdialog/mrdialog.rb', line 162

def help_button
  @help_button
end

#help_labelObject

Override the label used for the “Help” button



165
166
167
# File 'lib/mrdialog/mrdialog.rb', line 165

def help_label
  @help_label
end

#insecureObject

make the password widget friendlier but less secure, by echoing asterisks for each character.



178
179
180
# File 'lib/mrdialog/mrdialog.rb', line 178

def insecure
  @insecure
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.



86
87
88
# File 'lib/mrdialog/mrdialog.rb', line 86

def itemhelp
  @itemhelp
end

#loggerObject

ruby logger



147
148
149
# File 'lib/mrdialog/mrdialog.rb', line 147

def logger
  @logger
end

#no_labelObject

Override the label used for “No” buttons



159
160
161
# File 'lib/mrdialog/mrdialog.rb', line 159

def no_label
  @no_label
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.



93
94
95
# File 'lib/mrdialog/mrdialog.rb', line 93

def nocancel
  @nocancel
end

#notagsObject

MMM



101
102
103
# File 'lib/mrdialog/mrdialog.rb', line 101

def notags
  @notags
end

#ok_labelObject

Override the label used for “OK” buttons



150
151
152
# File 'lib/mrdialog/mrdialog.rb', line 150

def ok_label
  @ok_label
end

#password_formObject

set it to true for passwordform.



186
187
188
# File 'lib/mrdialog/mrdialog.rb', line 186

def password_form
  @password_form
end

#path_to_dialogObject

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



130
131
132
# File 'lib/mrdialog/mrdialog.rb', line 130

def path_to_dialog
  @path_to_dialog
end

#rc_fileObject

Specify the rc file to use for dialog. Default is $HOME/.dialogrc



43
44
45
# File 'lib/mrdialog/mrdialog.rb', line 43

def rc_file
  @rc_file
end

#scrollbarObject

For widgets holding a scrollable set of data, draw a scrollbar on its right-margin. This does not respond to the mouse.



190
191
192
# File 'lib/mrdialog/mrdialog.rb', line 190

def scrollbar
  @scrollbar
end

#separatorObject

Returns the value of attribute separator.



183
184
185
# File 'lib/mrdialog/mrdialog.rb', line 183

def separator
  @separator
end

#shadowObject

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



98
99
100
# File 'lib/mrdialog/mrdialog.rb', line 98

def shadow
  @shadow
end

#sleepObject

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



107
108
109
# File 'lib/mrdialog/mrdialog.rb', line 107

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.



114
115
116
# File 'lib/mrdialog/mrdialog.rb', line 114

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.



120
121
122
# File 'lib/mrdialog/mrdialog.rb', line 120

def tablen
  @tablen
end

#titleObject

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



125
126
127
# File 'lib/mrdialog/mrdialog.rb', line 125

def title
  @title
end

#yes_labelObject

Override the label used for “Yes” buttons



156
157
158
# File 'lib/mrdialog/mrdialog.rb', line 156

def yes_label
  @yes_label
end

Instance Method Details

#buildlist(text = "Text Goes Here", items = nil, height = 0, width = 0, listheight = 0) ⇒ Object

A buildlist dialog displays two lists, side-by-side. The list on the left shows unselected items. The list on the right shows selected items. As items are selected or unselected, they move between the lists. Use SPACE bar to select/unselect an item.

Use a carriage return or the “OK” button to accept the current value in the selected-window and exit. The results are written using the order displayed in the selected-window.

The caller is responsile to create the items properly. Please look at samples/buildlist.rb for an example.

return an array of selected tags

Author

[email protected]



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
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
537
538
539
540
# File 'lib/mrdialog/mrdialog.rb', line 467

def buildlist(text="Text Goes Here", items = nil, height=0, width=0, listheight=0)
  tmp = Tempfile.new('dialog') 
  selected_tags = []
  itemlist = ''

  items.each do |item|
    itemlist << '"' 
    itemlist << item[0].to_s
    itemlist << '"'
    itemlist << " "
    itemlist << '"'
    itemlist << item[1].to_s
    itemlist << '"'
    itemlist << " "
    itemlist << '"'
    if item[2]
      item[2] = "on"
    else
      item[2] = "off"
    end
    itemlist << item[2]
    itemlist << '"'
    itemlist << " "
  end
  itemlist << "2>"
  itemlist << tmp.path

  cmd = ""

  cmd << option_string()
  if !@separator
    @separator = "|"
    cmd << " "
    cmd << "--separator"
    cmd << " "
    cmd << '"'
    cmd << @separator
    cmd << '"'
  end
  cmd << " "
  cmd << "--buildlist"
  cmd << " "
  cmd << '"'
  cmd << " "
  cmd << text
  cmd << '"'
  cmd << " "
  cmd << height.to_s
  cmd << " "
  cmd << width.to_s
  cmd << " "
  cmd << listheight.to_s
  cmd << " "
  cmd << itemlist

  log_debug "Number of items: #{items.size}"
  log_debug "Command:\n#{cmd}"

  system(cmd)
  @exit_code = $?.exitstatus
  log_debug "Exit code: #{exit_code}"
  if @exit_code == 0
    lines = tmp.read
    log_debug "lines: #{lines} #{lines.class}"
    sep = Shellwords.escape(@separator)
    a = lines.split(/#{sep}/)
    a.each do |tag|
      log_debug "tag: '#{tag}'"
      selected_tags << tag if tag.to_s.length > 0
    end
  end
  tmp.close!
  return selected_tags
end

#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


747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
# File 'lib/mrdialog/mrdialog.rb', line 747

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)
  @exit_code = $?.exitstatus
  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. return an array of selected items



774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
# File 'lib/mrdialog/mrdialog.rb', line 774

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

  sep = "|"
  command = option_string() + "--checklist \"" + text.to_s +
                      "\" " + height.to_i.to_s + " " + width.to_i.to_s +
    " " + listheight.to_i.to_s + " " + itemlist + "2> " +
    tmp.path 
    log_debug "Command:\n#{command}"
  success = system(command)
  @exit_code = $?.exitstatus
  selected_array = []
  if success
    selected_string = tmp.readline
    tmp.close!
    log_debug "Separator: #{@separator}"

    sep = Shellwords.escape(@separator)
    a = selected_string.split(/#{sep}/)
    a.each do |item|
      log_debug ">> #{item}"
      selected_array << item if item && item.to_s.length > 0
    end
    return selected_array
  else
    tmp.close!
    return success
  end

end

#editbox(filepath, height = 0, width = 0) ⇒ Object

The edit-box dialog displays a copy of the file. You may edit it using the backspace, delete and cursor keys to correct typing errors. It also recognizes pageup/pagedown. Unlike the –in- putbox, you must tab to the “OK” or “Cancel” buttons to close the dialog. Pressing the “Enter” key within the box will split the corresponding line.

On exit, the contents of the edit window are written to dialog’s output.



579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# File 'lib/mrdialog/mrdialog.rb', line 579

def editbox(filepath, height=0, width=0)
  tmp = Tempfile.new('dialog') 

  cmd = ""
  cmd << option_string()
  cmd << " "
  cmd << "--editbox"
  cmd << " "
  cmd << '"'
  cmd << filepath
  cmd << '"'
  cmd << " "
  cmd << height.to_s
  cmd << " "
  cmd << width.to_s
  cmd << " "
  cmd << "2>"
  cmd << tmp.path

  log_debug "Command:\n#{cmd}"

  system(cmd)
  result = ''
  @exit_code = $?.exitstatus
  log_debug "Exit code: #{exit_code}"
  if @exit_code == 0
    result = tmp.read
  end
  tmp.close!
  return result
end

#exit_codeObject

return the exit code of the dialog



222
223
224
# File 'lib/mrdialog/mrdialog.rb', line 222

def exit_code
    return @exit_code
end

#form(text, items, height = 0, width = 0, formheight = 0) ⇒ Object

form/mixedform dialog A form dialog displays a form consisting of labels and fields, which are positioned on a scrollable window by coordinates given in the script. The field length flen and input-length ilen tell how long the field can be. The former defines the length shown for a selected field, while the latter defines the permissible length of the data entered in the field.

The caller is responsile to create the items properly. Please look at samples/form.rb for an example

return a hash. keys are the labels

Author

[email protected]



624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
# File 'lib/mrdialog/mrdialog.rb', line 624

def form(text, items, height=0, width=0, formheight=0)
  res_hash = {}
  tmp = Tempfile.new('dialog') 
  itemlist = ''
  mixed_form = false
  item_size = items[0].size
  log_debug "Item size:#{item_size}"
  # if there are 9 elements, it's a mixedform
  if item_size == 9
      mixed_form = true
  end
  items.each do |item|
    itemlist << '"'
    itemlist << item[0].to_s
    itemlist << '"'
    itemlist << " "
    itemlist << item[1].to_s
    itemlist << " "
    itemlist << item[2].to_s
    itemlist << " "
    itemlist << '"'
    itemlist << item[3].to_s
    itemlist << '"'
    itemlist << " "
    itemlist << item[4].to_s
    itemlist << " "
    itemlist << item[5].to_s
    itemlist << " "
    itemlist << item[6].to_s
    itemlist << " "
    itemlist << item[7].to_s
    itemlist << " "
    if mixed_form
        itemlist << item[8].to_s
        itemlist << " "
    end
  end
  itemlist << " "
  itemlist << "2>"
  itemlist << tmp.path

  cmd = ""
  cmd << option_string()
  cmd << " "
  if mixed_form
    cmd << "--mixedform"
  else
    if @password_form
      cmd << "--passwordform"
    else
      cmd << "--form"
    end
  end
  cmd << " "
  cmd << '"'
  cmd << text
  cmd << '"'
  cmd << " "
  cmd << height.to_s
  cmd << " "
  cmd << width.to_s
  cmd << " "
  cmd << formheight.to_s
  cmd << " "
  cmd << itemlist

  log_debug("Number of items: #{items.size}")
  log_debug("Command:\n#{cmd}")
  system(cmd)
  @exit_code = $?.exitstatus
  log_debug "Exit code: #{exit_code}"

  if @exit_code == 0
    lines = tmp.readlines
    lines.each_with_index do |val, idx|
        key = items[idx][0]
        res_hash[key] = val.chomp
    end
  end

  tmp.close!
  return res_hash
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.


842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
# File 'lib/mrdialog/mrdialog.rb', line 842

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)
  @exit_code = $?.exitstatus

  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, height = 0, width = 0, percent = 0) ⇒ Object

A gauge box displays a meter along the bottom of the box. The

meter indicates the percentage. New percentages are read from standard input, one integer per line. The meter is updated to reflect each new percentage. If the standard input reads the string “XXX”, then the first line following is taken as an integer percentage, then subsequent lines up to another “XXX” are used for a new prompt. The gauge exits when EOF is reached on the standard input.

The percent value denotes the initial percentage shown in the meter. If not speciied, it is zero.

On exit, no text is written to dialog’s output. The widget accepts no input, so the exit status is always OK.

The caller will write the text markers to stdout as described above inside a block and will pass the block to the method. Look at samples/gauge for an example on how the method is called. Thanks to Mike Morgan for the idea to use a block.

Author

[email protected] Apr-02-2014




273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/mrdialog/mrdialog.rb', line 273

def gauge(text, height=0, width=0, percent=0)
    cmd = ""
    cmd << option_string()
    cmd << " "
    cmd << "--gauge"
    cmd << " "
    cmd << '"'
    cmd << text
    cmd << '"'
    cmd << " "
    cmd << height.to_s
    cmd << " "
    cmd << width.to_s
    cmd << " "
    cmd << percent.to_s

    log_debug "Command:\n#{cmd}"
    IO.popen(cmd, "w") {|fh| yield fh}
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



878
879
880
881
882
883
884
# File 'lib/mrdialog/mrdialog.rb', line 878

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)
  @exit_code = $?.exitstatus
  return success
end

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

An input box is useful when you want to ask questions that

require  the  user to input a string as the answer.  If init is
supplied it is used  to  initialize  the  input  string.   When
entering  the string, the backspace, delete and cursor keys can
be used to correct typing  errors.   If  the  input  string  is
longer  than can fit in the dialog box, the input field will be
scrolled.

On exit, the input string will be returned.


1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
# File 'lib/mrdialog/mrdialog.rb', line 1116

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

  log_debug(command)
  success = system(command)
  @exit_code = $?.exitstatus

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

#log_debug(msg) ⇒ Object


if @logger is set, log




215
216
217
218
219
# File 'lib/mrdialog/mrdialog.rb', line 215

def log_debug(msg)
  if @logger
      @logger.debug("#{msg}")
  end
end

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



942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
# File 'lib/mrdialog/mrdialog.rb', line 942

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

  log_debug("Command:\n#{command}")
  success = system(command)
  @exit_code = $?.exitstatus

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

#mixedform(text, items, height = 0, width = 0, formheight = 0) ⇒ Object

A mixedform dialog displays a form consisting of labels and fields,

much like the –form dialog. It differs by adding a field-type parameter to each field’s description. Each bit in the type denotes an attribute of the field:

  • 1 hidden, e.g., a password field.

  • 2 readonly, e.g., a label.#

Author

[email protected]



716
717
718
719
720
721
722
723
# File 'lib/mrdialog/mrdialog.rb', line 716

def mixedform(text, items, height=0, width=0, formheight=0)
    item_size = items[0].size
    log_debug "Item size:#{item_size}"
    if item_size == 9
        return form(text, items, height, width, formheight)
    end
    return nil
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.


982
983
984
985
986
987
988
989
990
# File 'lib/mrdialog/mrdialog.rb', line 982

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 + " "

  log_debug "Command\n#{command}"
  success = system(command)
  @exit_code = $?.exitstatus
  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.


1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
# File 'lib/mrdialog/mrdialog.rb', line 1001

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
  log_debug(command)
  success = system(command)
  @exit_code = $?.exitstatus

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

#passwordform(text, items, height = 0, width = 0, formheight = 0) ⇒ Object

This is identical to –form except that all text fields are treated as password widgets rather than inputbox widgets.



728
729
730
731
# File 'lib/mrdialog/mrdialog.rb', line 728

def passwordform(text, items, height=0, width=0, formheight=0)
  @password_form = true
  return form(text, items, height, width, formheight)
end

#pause(text = "Text Goes Here", height = 0, width = 0, secs = 10) ⇒ Object

A pause box displays a meter along the bottom of the box. The meter indicates how many seconds remain until the end of the pause. The pause exits when timeout is reached or the user presses the OK button (status OK) or the user presses the CANCEL button or Esc key.



547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/mrdialog/mrdialog.rb', line 547

def pause(text="Text Goes Here", height=0, width=0, secs=10)
  cmd = ""
  cmd << option_string()
  cmd << " "
  cmd << "--pause"
  cmd << " "
  cmd << '"'
  cmd << text
  cmd << '"'
  cmd << " "
  cmd << height.to_s
  cmd << " "
  cmd << width.to_s
  cmd << " "
  cmd << secs.to_s
  log_debug "Command:\n#{cmd}"

  system(cmd)
  result = ''
  @exit_code = $?.exitstatus
  log_debug "Exit code: #{exit_code}"
end

#prgbox(command, height = 0, width = 0, text = '') ⇒ Object

A prgbox is very similar to a programbox.

This dialog box is used to display the output of a command that is specified as an argument to prgbox.

After the command completes, the user can press the ENTER key so that dialog will exit and the calling shell script can continue its operation.

If three parameters are given, it displays the text under the title, delineated from the scrolling file’s contents. If only two parameters are given, this text is omitted.



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/mrdialog/mrdialog.rb', line 360

def prgbox(command, height=0, width=0, text='')
  cmd = ""
  cmd << option_string()
  cmd << " "
  cmd << "--prgbox"
  cmd << " "
  if text.length > 0
    cmd << '"'
    cmd << text
    cmd << '"'
  end
  cmd << " "
  cmd << '"'
  cmd << command
  cmd << '"'
  cmd << " "
  cmd << height.to_s
  cmd << " "
  cmd << width.to_s
  system(cmd)
  @exit_code = $?.exitstatus
end

#programbox(description = '', height = 0, width = 0) ⇒ Object

same as progressbox but displays OK button at the end



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/mrdialog/mrdialog.rb', line 326

def programbox(description='', height=0, width=0)
  cmd = ""
  cmd << option_string()
  cmd << " "
  cmd << "--programbox"
  cmd << " "
  if description.length > 0
      cmd << '"'
      cmd << description
      cmd << '"'
  end
  cmd << " "
  cmd << height.to_s
  cmd << " "
  cmd << width.to_s
  
  log_debug("Command\n#{cmd}")
  IO.popen(cmd, "w") {|fh| yield fh}
end

#progressbox(description = '', height = 0, width = 0) ⇒ Object

Progressbox is used to display the piped output of a command.

After the command completes, the user can press the ENTER key so that dialog will exit and the calling shell script can continue its operation. If three parameters are given, it displays the text under the title, delineated from the scrolling file’s contents. If only two parameters are given, this text is omitted.

The caller will write the progress string on stdout in a block and will pass the block to the method. Please look at samples/ progress.rb for an example. Author: [email protected] Apr-02-2014



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/mrdialog/mrdialog.rb', line 305

def progressbox(description='', height=0, width=0)
  cmd = ""
  cmd << option_string()
  cmd << " "
  cmd << "--progressbox"
  cmd << " "
  if description.length > 0
      cmd << '"'
      cmd << description
      cmd << '"'
  end
  cmd << " "
  cmd << height.to_s
  cmd << " "
  cmd << width.to_s
  
  log_debug("Command\n#{cmd}")
  IO.popen(cmd, "w") {|fh| yield fh}
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.


889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
# File 'lib/mrdialog/mrdialog.rb', line 889

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
  log_debug("Command:\n#{command}")
  success = system(command)
  @exit_code = $?.exitstatus

  if success
    selected_string = tmp.readline
    tmp.close!
    return selected_string
  else
    tmp.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.


1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
# File 'lib/mrdialog/mrdialog.rb', line 1057

def textbox(file, type="text", height=0, width=0)
  case type
    when "text"
      opt = "--textbox"
    when "tail"
      opt = "--tailbox"
    when "bg"
      opt = "--textboxbg"
  end

  command = option_string() + opt +" \"" + file.to_s +
              "\" " + height.to_i.to_s + " " + width.to_i.to_s + " "
  
  success = system(command)
  @exit_code = $?.exitstatus

  return success
end

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

  • def timebox(file, type=“text”, height=0, width=0, time=Time.now)



1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
# File 'lib/mrdialog/mrdialog.rb', line 1086

def timebox(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
  log_debug("Command:\n#{command}")
  success = system(command)
  @exit_code = $?.exitstatus
  if success
    time = Time.parse(tmp.readline)
    tmp.close!
    return time
  else
    tmp.close!
    return success
  end
  
end

#treeview(text = "Text Goes Here", items = nil, height = 0, width = 0, listheight = 0) ⇒ Object

Display data organized as a tree. Each group of data contains a tag, the text to display for the item, its status (“on” or “off”) and the depth of the item in the tree.

Only one item can be selected (like the radiolist). The tag is not displayed.

On exit, the tag of the selected item is written to dialog’s output.



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/mrdialog/mrdialog.rb', line 393

def treeview(text="Text Goes Here", items=nil, height=0, width=0, listheight=0)
  tmp = Tempfile.new('dialog') 
  itemlist = ''
  items.each do |item|
    itemlist << '"' 
    itemlist << item[0].to_s
    itemlist << '"'
    itemlist << " "
    itemlist << '"'
    itemlist << item[1].to_s
    itemlist << '"'
    itemlist << " "
    itemlist << '"'
    if item[2]
      item[2] = "on"
    else
      item[2] = "off"
    end
    itemlist << item[2]
    itemlist << '"'
    itemlist << " "
    itemlist << item[3].to_s
    itemlist << " "
  end
  itemlist << "2>"
  itemlist << tmp.path

  cmd = ""
  cmd << option_string()
  cmd << " "
  cmd << "--treeview"
  cmd << " "
  cmd << '"'
  cmd << " "
  cmd << text
  cmd << '"'
  cmd << " "
  cmd << height.to_s
  cmd << " "
  cmd << width.to_s
  cmd << " "
  cmd << listheight.to_s
  cmd << " "
  cmd << itemlist

  log_debug "Number of items: #{items.size}"
  log_debug "Command:\n#{cmd}"

  system(cmd)
  @exit_code = $?.exitstatus
  log_debug "Exit code: #{exit_code}"
  tag = ''
  if @exit_code == 0
    tag = tmp.read
  end
  tmp.close!
  return tag
end

#which(prog) ⇒ Object


return the path of the executable which exists in the PATH env variable return nil otherwise arg is the name of the program without extensin [email protected]




233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/mrdialog/mrdialog.rb', line 233

def which(prog)
    path_ext = ENV['PATHEXT']
    exts = ['']
    if path_ext # WINDOW$
        exts = path_ext.split(';')
    end
    path = ENV['PATH']
    path.split(File::PATH_SEPARATOR).each do |dir|
        exts.each do |ext|
          candidate = File.join(dir, "#{prog}#{ext}")
          return candidate if File.executable?(candidate)
        end
    end
    return nil
end

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

changing –inputbox to –yesno

muquit@muquit.com Apr-01-2014


1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
# File 'lib/mrdialog/mrdialog.rb', line 1159

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

  command = ""
  command << option_string();
  command << " "
  command << '"'
  command << "--yesno"
  command << '"'
  command << " "
  command << '"'
  command << text
  command << '"'
  command << " "
  command << height.to_s
  command << " "
  command << width.to_s


  log_debug("Command:\n#{command}")
  success = system(command)
  @exit_code = $?.exitstatus
  return success
end