2 # $Id: demo.pl,v 1.23 2018/06/12 21:39:44 tom Exp $
3 ################################################################################
4 # Copyright 2018 Thomas E. Dickey
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU Lesser General Public License, version 2.1
8 # as published by the Free Software Foundation.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this program; if not, write to
17 # Free Software Foundation, Inc.
18 # 51 Franklin St., Fifth Floor
19 # Boston, MA 02110, USA.
20 ################################################################################
21 # This demonstration is provided solely to exercise the sample Perl wrapper for
22 # dialog which is included in its source-code. See libui-dialog-perl for a
23 # more comprehensive binding.
25 # TODO: modify dialog.pl to use $DIALOG environment variable, drive from GetOpts here
26 # TODO: eliminate constant $scr_lines in dialog.pl
32 use FindBin qw($Bin $Script);
36 dialog->import('@dialog_result');
42 my $value = `tput "$name"`;
44 $value = $default unless ( $value =~ /^[0-9]+$/ );
50 select( undef, undef, undef, $msecs * 0.001 );
53 sub show_results($$) {
59 "Resulting text:\\n %s", join( '\\n ', @dialog_result )
67 my $RHS_CLEAR = "clear";
68 my $RHS_TEXTBOX = "textbox";
69 my $RHS_MSGBOX = "msgbox";
70 my $RHS_INFOBOX = "infobox";
71 my $RHS_YESNO = "yesno";
72 my $RHS_GAUGE = "gauge";
73 my $RHS_INPUTBOX = "inputbox";
74 my $RHS_MENU = "menu";
75 my $RHS_MENUL = "menul";
76 my $RHS_MENUA = "menua";
77 my $RHS_CHECKLIST = "checklist";
78 my $RHS_CHECKLISTL = "checklistl";
79 my $RHS_CHECKLISTA = "checklista";
80 my $RHS_RADIOLIST = "radiolist";
93 for ( my $s = 0, my $t = 0 ; $s <= $#demo_2col ; $s += 2, $t += 3 ) {
95 my $c1 = $demo_2col[$s];
96 my $c2 = $demo_2col[ $s + 1 ];
98 $demo_3col[ $t + 1 ] = $c2;
99 $demo_3col[ $t + 2 ] = ( $c1 =~ /2/ ) ? 1 : 0;
100 $demo_tags[$d] = $c1;
101 $demo_tags[$d] =~ s/2/1/;
102 $demo_tags[ $d + ( $#demo_2col + 1 ) / 2 ] = $c2;
103 $demo_hash{ sprintf( "%d %s", $d, $c1 ) } = $c2;
106 while ( $status > 0 ) {
107 my $lines = &tput( "lines", 24 );
108 my $cols = &tput( "cols", 80 );
109 my $maxcols = $cols - 4;
110 my $mincols = ( $cols > 8 ) ? 8 : $cols;
111 my $midcols = int( ( $cols * 3 ) / 4 );
115 "My title", "My message",
117 $RHS_CLEAR, "clear and exit",
118 $RHS_TEXTBOX, "text-box of this script",
119 $RHS_MSGBOX, "informational-message, OK button",
120 $RHS_INFOBOX, "informational-message, no button",
121 $RHS_YESNO, "message with Yes/No buttons",
122 $RHS_GAUGE, "message with progress-gauge",
123 $RHS_INPUTBOX, "input-box",
124 $RHS_MENU, "menu, with tags and description",
125 $RHS_MENUL, "menu, using only tags",
126 $RHS_MENUA, "alphabetically sorted menu",
127 $RHS_CHECKLIST, "check-list with tags and description",
128 $RHS_CHECKLISTL, "check-list using only tags",
129 $RHS_CHECKLISTA, "alphabetically sorted check-list",
130 $RHS_RADIOLIST, "list of radio-buttons"
132 if ( $status > 0 and $#dialog_result == 0 ) {
134 my $testcase = $dialog_result[0];
135 if ( $testcase eq $RHS_CLEAR ) {
139 elsif ( $testcase eq $RHS_TEXTBOX ) {
140 &rhs_textbox( "This script", "$Script", 0, 0 );
142 elsif ( $testcase eq $RHS_MSGBOX ) {
144 "This is a demonstration script.\\n"
145 . "This should be the second line,\\n"
146 . "and this should be the third line,";
147 &rhs_msgbox( "A message", $msg,
148 int( ( length($msg) + 3 ) / 3 ) + 3 );
150 elsif ( $testcase eq $RHS_INFOBOX ) {
152 "This is a fairly long line of text, used to"
153 . " show how dialog can be used to wrap lines to fit in"
154 . " screens with different width. The text will start wide,"
155 . " then get narrower, showing a new infobox for each width"
156 . " before going back up to the full width of the terminal.";
158 while ( $wide > $mincols ) {
159 &rhs_infobox( "Info-box", $msg, $wide-- );
162 while ( $wide < $maxcols ) {
163 &rhs_infobox( "Info-box", $msg, ++$wide );
166 &rhs_msgbox( "Info-end", $msg, $wide );
168 elsif ( $testcase eq $RHS_YESNO ) {
172 "Should \"dialog --yesno\" return \"1\" on \"yes\""
173 . " to simplify (some) shell scripts?",
180 "No, a successful program exits with "
181 . "\"0\" (EXIT_SUCCESS)",
188 "Shell scripts assume that \"exit\ 0\" is successful;"
189 . " Perl is different.",
194 elsif ( $testcase eq $RHS_GAUGE ) {
199 "Show progress (or lack of it)",
202 while ( $pct < 100 ) {
206 &rhs_update_gauge($pct);
209 &rhs_update_gauge_and_message( "This will go faster", $pct );
214 &rhs_update_gauge($pct);
219 elsif ( $testcase eq $RHS_INPUTBOX ) {
222 "My inputbox", "This demonstrates the inputbox",
227 &show_results( "My inputbox", $midcols );
230 elsif ( $testcase eq $RHS_MENU ) {
235 "This menu uses \"tag\" values and descriptions:",
236 $midcols, ( $#demo_2col + 1 ) / 2
242 &show_results( "My menu", $midcols );
245 elsif ( $testcase eq $RHS_MENUL ) {
249 "A menu", "This menu uses only the \"tag\" values:",
250 $midcols, $#demo_tags + 1
256 &show_results( "My long-menu", $midcols );
259 elsif ( $testcase eq $RHS_MENUA ) {
262 "A menu", "This menu uses the sorted keys from a hash:",
267 &show_results( "My alpha-menu", $midcols );
270 elsif ( $testcase eq $RHS_CHECKLIST ) {
275 "This checklist uses \"tag\" values"
276 . " and descriptions:",
278 ( $#demo_3col + 1 ) / 3
284 &show_results( "My checklist", $midcols );
287 elsif ( $testcase eq $RHS_CHECKLISTL ) {
292 "This checklist uses only the \"tag\" values:",
293 $midcols, $#demo_tags + 1
299 &show_results( "My long-checklist", $midcols );
302 elsif ( $testcase eq $RHS_CHECKLISTA ) {
306 "This checklist uses the sorted keys from a hash:",
311 &show_results( "My alpha-checklist", $midcols );
314 elsif ( $testcase eq $RHS_RADIOLIST ) {
319 "This radiolist uses \"tag\" values"
320 . " and descriptions:",
322 ( $#demo_3col + 1 ) / 3
328 &show_results( "My radiolist", $midcols );