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



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/mrdialog/mrdialog.rb', line 171

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 -



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

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.



48
49
50
# File 'lib/mrdialog/mrdialog.rb', line 48

def aspect
  @aspect
end

#backtitleObject

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



54
55
56
# File 'lib/mrdialog/mrdialog.rb', line 54

def backtitle
  @backtitle
end

#beepObject

Sound the audible alarm each time the screen is refreshed.



59
60
61
# File 'lib/mrdialog/mrdialog.rb', line 59

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.



65
66
67
# File 'lib/mrdialog/mrdialog.rb', line 65

def begin
  @begin
end

#clearObject

clear screen



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

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.



76
77
78
# File 'lib/mrdialog/mrdialog.rb', line 76

def crwrap
  @crwrap
end

#dialog_cancelObject

Returns the value of attribute dialog_cancel.



133
134
135
# File 'lib/mrdialog/mrdialog.rb', line 133

def dialog_cancel
  @dialog_cancel
end

#dialog_escObject

Returns the value of attribute dialog_esc.



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

def dialog_esc
  @dialog_esc
end

#dialog_extraObject

Returns the value of attribute dialog_extra.



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

def dialog_extra
  @dialog_extra
end

#dialog_helpObject

Returns the value of attribute dialog_help.



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

def dialog_help
  @dialog_help
end

#dialog_item_helpObject

Returns the value of attribute dialog_item_help.



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

def dialog_item_help
  @dialog_item_help
end

#dialog_okObject

exit codes



132
133
134
# File 'lib/mrdialog/mrdialog.rb', line 132

def dialog_ok
  @dialog_ok
end

#dialog_optionsObject

pass dialog’s option exactly



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

def dialog_options
  @dialog_options
end

#insecureObject

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



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

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.



83
84
85
# File 'lib/mrdialog/mrdialog.rb', line 83

def itemhelp
  @itemhelp
end

#loggerObject

ruby logger



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

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



90
91
92
# File 'lib/mrdialog/mrdialog.rb', line 90

def nocancel
  @nocancel
end

#notagsObject

MMM



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

def notags
  @notags
end

#ok_labelObject

Override the label used for “OK” buttons



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

def ok_label
  @ok_label
end

#password_formObject

set it to true for passwordform.



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

def password_form
  @password_form
end

#path_to_dialogObject

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



127
128
129
# File 'lib/mrdialog/mrdialog.rb', line 127

def path_to_dialog
  @path_to_dialog
end

#scrollbarObject

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



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

def scrollbar
  @scrollbar
end

#separatorObject

Returns the value of attribute separator.



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

def separator
  @separator
end

#shadowObject

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



95
96
97
# File 'lib/mrdialog/mrdialog.rb', line 95

def shadow
  @shadow
end

#sleepObject

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



104
105
106
# File 'lib/mrdialog/mrdialog.rb', line 104

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.



111
112
113
# File 'lib/mrdialog/mrdialog.rb', line 111

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.



117
118
119
# File 'lib/mrdialog/mrdialog.rb', line 117

def tablen
  @tablen
end

#titleObject

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



122
123
124
# File 'lib/mrdialog/mrdialog.rb', line 122

def title
  @title
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]



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
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
# File 'lib/mrdialog/mrdialog.rb', line 443

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


723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
# File 'lib/mrdialog/mrdialog.rb', line 723

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. return an array of selected items



749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
# File 'lib/mrdialog/mrdialog.rb', line 749

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



555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'lib/mrdialog/mrdialog.rb', line 555

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



198
199
200
# File 'lib/mrdialog/mrdialog.rb', line 198

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]



600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
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
# File 'lib/mrdialog/mrdialog.rb', line 600

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.


816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
# File 'lib/mrdialog/mrdialog.rb', line 816

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, 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




249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/mrdialog/mrdialog.rb', line 249

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



851
852
853
854
855
856
# File 'lib/mrdialog/mrdialog.rb', line 851

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

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.


1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
# File 'lib/mrdialog/mrdialog.rb', line 1083

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)

              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




191
192
193
194
195
# File 'lib/mrdialog/mrdialog.rb', line 191

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

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



914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
# File 'lib/mrdialog/mrdialog.rb', line 914

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)

              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]



692
693
694
695
696
697
698
699
# File 'lib/mrdialog/mrdialog.rb', line 692

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.


953
954
955
956
957
958
959
960
# File 'lib/mrdialog/mrdialog.rb', line 953

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


971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
# File 'lib/mrdialog/mrdialog.rb', line 971

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)

      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.



704
705
706
707
# File 'lib/mrdialog/mrdialog.rb', line 704

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.



523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# File 'lib/mrdialog/mrdialog.rb', line 523

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.



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/mrdialog/mrdialog.rb', line 336

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



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

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



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/mrdialog/mrdialog.rb', line 281

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.


862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
# File 'lib/mrdialog/mrdialog.rb', line 862

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)

              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.


1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
# File 'lib/mrdialog/mrdialog.rb', line 1026

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)

  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)



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

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



369
370
371
372
373
374
375
376
377
378
379
380
381
382
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/mrdialog/mrdialog.rb', line 369

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]




209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/mrdialog/mrdialog.rb', line 209

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


1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
# File 'lib/mrdialog/mrdialog.rb', line 1125

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)
  return success
end