1 # Functions that handle calling dialog(1) -*-perl-*-
2 # $Id: dialog.pl,v 1.4 2001/10/13 00:40:22 tom Exp $
4 # Return values are 1 for success and 0 for failure (or cancel)
5 # Resultant text (if any) is in dialog_result
7 # Unfortunately, the gauge requires use of /bin/sh to get going.
8 # I didn't bother to make the others shell-free, although it
9 # would be simple to do.
11 # Note that dialog generally returns 0 for success, so I invert the
12 # sense of the return code for more readable boolean expressions.
19 return system("dialog --clear");
23 local ( $title, $file, $width, $height ) = @_;
25 system("dialog --title \"$title\" --textbox $file $height $width");
31 local ( $title, $message, $width ) = @_;
32 local ( $tmp, $height, $message_len );
34 $message = &rhs_wordwrap
($message, $width);
35 $message_len = split(/^/, $message);
37 if (chop($tmp) eq "\n") {
40 $height = 4 + $message_len;
42 $tmp = system("dialog --title \"$title\" --msgbox \"$message\" $height $width");
51 local ( $title, $message, $width ) = @_;
52 local ( $tmp, $height, $message_len );
54 $message = &rhs_wordwrap
($message, $width);
55 $message_len = split(/^/, $message);
57 if (chop($tmp) eq "\n") {
60 $height = 2 + $message_len;
62 return system("dialog --title \"$title\" --infobox \"$message\" $height $width");
66 local ( $title, $message, $width ) = @_;
67 local ( $tmp, $height, $message_len );
69 $message = &rhs_wordwrap
($message, $width);
70 $message_len = split(/^/, $message);
72 if (chop($tmp) eq "\n") {
75 $height = 4 + $message_len;
77 $tmp = system("dialog --title \"$title\" --yesno \"$message\" $height $width");
78 # Dumb: dialog returns 0 for "yes" and 1 for "no"
87 local ( $title, $message, $width, $percent ) = @_;
88 local ( $tmp, $height, $message_len );
90 $gauge_width = $width;
92 $message = &rhs_wordwrap
($message, $width);
93 $message_len = split(/^/, $message);
95 if (chop($tmp) eq "\n") {
98 $height = 5 + $message_len;
100 open(GAUGE
, "|dialog --title \"$title\" --gauge \"$message\" $height $width $percent");
103 sub rhs_update_gauge
{
104 local ( $percent ) = @_;
106 &printflush
(GAUGE
, "$percent\n");
109 sub rhs_update_gauge_and_message
{
110 local ( $message, $percent ) = @_;
112 $message = &rhs_wordwrap
($message, $gauge_width);
113 $message =~ s/\n/\\n/g;
114 &printflush
(GAUGE
, "XXX\n$percent\n$message\nXXX\n");
122 local ( $title, $message, $width, $instr ) = @_;
123 local ( $tmp, $height, $message_len );
125 $message = &rhs_wordwrap
($message, $width);
126 $message_len = split(/^/, $message);
128 if (chop($tmp) eq "\n") {
131 $height = 7 + $message_len;
133 return &return_output
(0, "dialog --title \"$title\" --inputbox \"$message\" $height $width \"$instr\"");
137 local ( $title, $message, $width, $numitems ) = @_;
138 local ( $i, $tmp, $ent, $height, $menuheight, @list, $message_len );
140 shift; shift; shift; shift;
143 for ($i = 0; $i < $numitems; $i++) {
145 $list[@list] = "\"$ent\"";
147 $list[@list] = "\"$ent\"";
150 $message = &rhs_wordwrap
($message, $width);
152 $message_len = split(/^/, $message);
154 if (chop($tmp) eq "\n") {
158 $height = $message_len + 6 + $numitems;
159 if ($height <= $scr_lines) {
160 $menuheight = $numitems;
162 $height = $scr_lines;
163 $menuheight = $scr_lines - $message_len - 6;
166 return &return_output
(0, "dialog --title \"$title\" --menu \"$message\" $height $width $menuheight @list");
170 local ( $title, $message, $width, $numitems ) = @_;
171 local ( $i, $tmp, $ent, $height, $menuheight, @list, $message_len );
173 shift; shift; shift; shift;
176 for ($i = 0; $i < $numitems; $i++) {
178 $list[@list] = "\"$ent\"";
179 $list[@list] = "\"\"";
182 $message = &rhs_wordwrap
($message, $width);
184 $message_len = split(/^/, $message);
186 if (chop($tmp) eq "\n") {
190 $height = $message_len + 6 + $numitems;
191 if ($height <= $scr_lines) {
192 $menuheight = $numitems;
194 $height = $scr_lines;
195 $menuheight = $scr_lines - $message_len - 6;
198 return &return_output
(0, "dialog --title \"$title\" --menu \"$message\" $height $width $menuheight @list");
202 local ( $title, $message, $width, %items ) = @_;
203 local ( $tmp, $ent, $height, $menuheight, @list, $message_len );
206 foreach $ent (sort keys (%items)) {
207 $list[@list] = "\"$ent\"";
208 $list[@list] = "\"$items{$ent}\"";
211 $message = &rhs_wordwrap
($message, $width);
213 $message_len = split(/^/, $message);
215 if (chop($tmp) eq "\n") {
219 $numitems = keys(%items);
220 $height = $message_len + 6 + $numitems;
221 if ($height <= $scr_lines) {
222 $menuheight = $numitems;
224 $height = $scr_lines;
225 $menuheight = $scr_lines - $message_len - 6;
228 return &return_output
(0, "dialog --title \"$title\" --menu \"$message\" $height $width $menuheight @list");
232 local ( $title, $message, $width, $numitems ) = @_;
233 local ( $i, $tmp, $ent, $height, $menuheight, @list, $message_len );
235 shift; shift; shift; shift;
238 for ($i = 0; $i < $numitems; $i++) {
240 $list[@list] = "\"$ent\"";
242 $list[@list] = "\"$ent\"";
247 $list[@list] = "OFF";
251 $message = &rhs_wordwrap
($message, $width);
253 $message_len = split(/^/, $message);
255 if (chop($tmp) eq "\n") {
259 $height = $message_len + 6 + $numitems;
260 if ($height <= $scr_lines) {
261 $menuheight = $numitems;
263 $height = $scr_lines;
264 $menuheight = $scr_lines - $message_len - 6;
267 return &return_output
("list", "dialog --title \"$title\" --separate-output --checklist \"$message\" $height $width $menuheight @list");
271 local ( $title, $message, $width, $numitems ) = @_;
272 local ( $i, $tmp, $ent, $height, $menuheight, @list, $message_len );
274 shift; shift; shift; shift;
277 for ($i = 0; $i < $numitems; $i++) {
279 $list[@list] = "\"$ent\"";
280 $list[@list] = "\"\"";
281 $list[@list] = "OFF";
284 $message = &rhs_wordwrap
($message, $width);
286 $message_len = split(/^/, $message);
288 if (chop($tmp) eq "\n") {
292 $height = $message_len + 6 + $numitems;
293 if ($height <= $scr_lines) {
294 $menuheight = $numitems;
296 $height = $scr_lines;
297 $menuheight = $scr_lines - $message_len - 6;
299 return &return_output
("list", "dialog --title \"$title\" --separate-output --checklist \"$message\" $height $width $menuheight @list");
303 local ( $title, $message, $width, %items ) = @_;
304 local ( $tmp, $ent, $height, $menuheight, @list, $message_len );
306 shift; shift; shift; shift;
309 foreach $ent (sort keys (%items)) {
310 $list[@list] = "\"$ent\"";
311 $list[@list] = "\"$items{$ent}\"";
312 $list[@list] = "OFF";
315 $message = &rhs_wordwrap
($message, $width);
317 $message_len = split(/^/, $message);
319 if (chop($tmp) eq "\n") {
323 $numitems = keys(%items);
324 $height = $message_len + 6 + $numitems;
325 if ($height <= $scr_lines) {
326 $menuheight = $numitems;
328 $height = $scr_lines;
329 $menuheight = $scr_lines - $message_len - 6;
332 return &return_output
("list", "dialog --title \"$title\" --separate-output --checklist \"$message\" $height $width $menuheight @list");
336 local ( $title, $message, $width, $numitems ) = @_;
337 local ( $i, $tmp, $ent, $height, $menuheight, @list, $message_len );
339 shift; shift; shift; shift;
342 for ($i = 0; $i < $numitems; $i++) {
344 $list[@list] = "\"$ent\"";
346 $list[@list] = "\"$ent\"";
351 $list[@list] = "OFF";
355 $message = &rhs_wordwrap
($message, $width);
357 $message_len = split(/^/, $message);
359 if (chop($tmp) eq "\n") {
363 $height = $message_len + 6 + $numitems;
364 if ($height <= $scr_lines) {
365 $menuheight = $numitems;
367 $height = $scr_lines;
368 $menuheight = $scr_lines - $message_len - 6;
371 return &return_output
(0 , "dialog --title \"$title\" --radiolist \"$message\" $height $width $menuheight @list");
375 local ( $listp, $command ) = @_;
378 pipe(PARENT_READER
, CHILD_WRITER
);
379 # We have to fork (as opposed to using "system") so that the parent
380 # process can read from the pipe to avoid deadlock.
382 if ($pid == 0) { # child
383 close(PARENT_READER
);
384 open(STDERR
, ">&CHILD_WRITER");
388 if ($pid > 0) { # parent
389 close( CHILD_WRITER
);
392 while (<PARENT_READER
>) {
394 $dialog_result[@dialog_result] = $_;
397 else { $dialog_result = <PARENT_READER
>; }
398 close(PARENT_READER
);
403 # Again, dialog returns results backwards
412 local ( $intext, $width ) = @_;
413 local ( $outtext, $i, $j, @lines, $wrap, @words, $pos, $pad );
416 $pad = 3; # leave 3 spaces around each line
417 $pos = $pad; # current insert position
418 $wrap = 0; # 1 if we have been auto wraping
419 $insert_nl = 0; # 1 if we just did an absolute
420 # and we should preface any new text
422 @lines = split(/\n/, $intext);
423 for ($i = 0; $i <= $#lines; $i++) {
424 if ($lines[$i] =~ /^>/) {
425 $outtext .= "\n" if ($insert_nl);
426 $outtext .= "\n" if ($wrap);
427 $lines[$i] =~ /^>(.*)$/;
434 @words = split(/\s+/,$lines[$i]);
435 for ($j = 0; $j <= $#words; $j++) {
440 if ((length($words[$j]) + $pos) > $width - $pad) {
444 $outtext .= $words[$j] . " ";
445 $pos += length($words[$j]) + 1;