aboutsummaryrefslogtreecommitdiff
path: root/contrib/dialog/demo.pl
blob: 8f1a37e7b37928a4d31cf3a59a631a8ba0b63433 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env perl
# $Id: demo.pl,v 1.23 2018/06/12 21:39:44 tom Exp $
################################################################################
#  Copyright 2018	Thomas E. Dickey
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License, version 2.1
#  as published by the Free Software Foundation.
#
#  This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this program; if not, write to
#	Free Software Foundation, Inc.
#	51 Franklin St., Fifth Floor
#	Boston, MA 02110, USA.
################################################################################
# This demonstration is provided solely to exercise the sample Perl wrapper for
# dialog which is included in its source-code.  See libui-dialog-perl for a
# more comprehensive binding.
#
# TODO: modify dialog.pl to use $DIALOG environment variable, drive from GetOpts here
# TODO: eliminate constant $scr_lines in dialog.pl

use warnings;
use strict;
use diagnostics;

use FindBin qw($Bin $Script);
use lib "$Bin";

require "dialog.pl";
dialog->import('@dialog_result');
our @dialog_result;

sub tput($$) {
    my $name    = shift;
    my $default = shift;
    my $value   = `tput "$name"`;
    chomp $value;
    $value = $default unless ( $value =~ /^[0-9]+$/ );
    return $value;
}

sub napms($) {
    my $msecs = shift;
    select( undef, undef, undef, $msecs * 0.001 );
}

sub show_results($$) {
    my $title = shift;
    my $width = shift;
    &rhs_msgbox(
        $title,
        sprintf(
            "Resulting text:\\n    %s", join( '\\n    ', @dialog_result )
        ),
        $width
    );
}

sub doit() {
    my $status         = 1;
    my $RHS_CLEAR      = "clear";
    my $RHS_TEXTBOX    = "textbox";
    my $RHS_MSGBOX     = "msgbox";
    my $RHS_INFOBOX    = "infobox";
    my $RHS_YESNO      = "yesno";
    my $RHS_GAUGE      = "gauge";
    my $RHS_INPUTBOX   = "inputbox";
    my $RHS_MENU       = "menu";
    my $RHS_MENUL      = "menul";
    my $RHS_MENUA      = "menua";
    my $RHS_CHECKLIST  = "checklist";
    my $RHS_CHECKLISTL = "checklistl";
    my $RHS_CHECKLISTA = "checklista";
    my $RHS_RADIOLIST  = "radiolist";

    my @demo_2col = qw(
      This      that
      is        has
      a         this
      2-column  quoted
      menu      "tag".
    );
    my @demo_3col;
    my @demo_tags;
    my %demo_hash;

    for ( my $s = 0, my $t = 0 ; $s <= $#demo_2col ; $s += 2, $t += 3 ) {
        my $d  = $s / 2;
        my $c1 = $demo_2col[$s];
        my $c2 = $demo_2col[ $s + 1 ];
        $demo_3col[$t] = $c1;
        $demo_3col[ $t + 1 ] = $c2;
        $demo_3col[ $t + 2 ] = ( $c1 =~ /2/ ) ? 1 : 0;
        $demo_tags[$d] = $c1;
        $demo_tags[$d] =~ s/2/1/;
        $demo_tags[ $d + ( $#demo_2col + 1 ) / 2 ] = $c2;
        $demo_hash{ sprintf( "%d %s", $d, $c1 ) } = $c2;
    }

    while ( $status > 0 ) {
        my $lines = &tput( "lines", 24 );
        my $cols  = &tput( "cols",  80 );
        my $maxcols = $cols - 4;
        my $mincols = ( $cols > 8 ) ? 8 : $cols;
        my $midcols = int( ( $cols * 3 ) / 4 );

        @dialog_result = ();
        $status        = &rhs_menu(
            "My title",      "My message",
            0,               14,
            $RHS_CLEAR,      "clear and exit",
            $RHS_TEXTBOX,    "text-box of this script",
            $RHS_MSGBOX,     "informational-message, OK button",
            $RHS_INFOBOX,    "informational-message, no button",
            $RHS_YESNO,      "message with Yes/No buttons",
            $RHS_GAUGE,      "message with progress-gauge",
            $RHS_INPUTBOX,   "input-box",
            $RHS_MENU,       "menu, with tags and description",
            $RHS_MENUL,      "menu, using only tags",
            $RHS_MENUA,      "alphabetically sorted menu",
            $RHS_CHECKLIST,  "check-list with tags and description",
            $RHS_CHECKLISTL, "check-list using only tags",
            $RHS_CHECKLISTA, "alphabetically sorted check-list",
            $RHS_RADIOLIST,  "list of radio-buttons"
        );
        if ( $status > 0 and $#dialog_result == 0 ) {

            my $testcase = $dialog_result[0];
            if ( $testcase eq $RHS_CLEAR ) {
                &rhs_clear;
                last;
            }
            elsif ( $testcase eq $RHS_TEXTBOX ) {
                &rhs_textbox( "This script", "$Script", 0, 0 );
            }
            elsif ( $testcase eq $RHS_MSGBOX ) {
                my $msg =
                    "This is a demonstration script.\\n"
                  . "This should be the second line,\\n"
                  . "and this should be the third line,";
                &rhs_msgbox( "A message", $msg,
                    int( ( length($msg) + 3 ) / 3 ) + 3 );
            }
            elsif ( $testcase eq $RHS_INFOBOX ) {
                my $msg =
                    "This is a fairly long line of text, used to"
                  . " show how dialog can be used to wrap lines to fit in"
                  . " screens with different width.  The text will start wide,"
                  . " then get narrower, showing a new infobox for each width"
                  . " before going back up to the full width of the terminal.";
                my $wide = $maxcols;
                while ( $wide > $mincols ) {
                    &rhs_infobox( "Info-box", $msg, $wide-- );
                    &napms(50);
                }
                while ( $wide < $maxcols ) {
                    &rhs_infobox( "Info-box", $msg, ++$wide );
                    &napms(50);
                }
                &rhs_msgbox( "Info-end", $msg, $wide );
            }
            elsif ( $testcase eq $RHS_YESNO ) {
                if (
                    &rhs_yesno(
                        "Yes/no",
                        "Should \"dialog --yesno\" return \"1\" on \"yes\""
                          . " to simplify (some) shell scripts?",
                        $cols / 2
                    )
                  )
                {
                    &rhs_msgbox(
                        "Explanation",
                        "No, a successful program exits with "
                          . "\"0\" (EXIT_SUCCESS)",
                        $cols / 2
                    );
                }
                else {
                    &rhs_msgbox(
                        "Explanation",
                        "Shell scripts assume that \"exit\ 0\" is successful;"
                          . " Perl is different.",
                        $cols / 2
                    );
                }
            }
            elsif ( $testcase eq $RHS_GAUGE ) {
                my $pct = 0;
                my $sec = 10;
                &rhs_gauge(
                    "My gauge",
                    "Show progress (or lack of it)",
                    $midcols * 3, $pct
                );
                while ( $pct < 100 ) {
                    $pct++;
                    &napms($sec);
                    $sec *= 1.04;
                    &rhs_update_gauge($pct);
                }
                $pct = 99;
                &rhs_update_gauge_and_message( "This will go faster", $pct );
                while ( $pct > 0 ) {
                    $pct--;
                    &napms($sec);
                    $sec /= 1.05;
                    &rhs_update_gauge($pct);
                }
                &napms(1000);
                &rhs_stop_gauge;
            }
            elsif ( $testcase eq $RHS_INPUTBOX ) {
                if (
                    &rhs_inputbox(
                        "My inputbox", "This demonstrates the inputbox",
                        $maxcols,      ""
                    )
                  )
                {
                    &show_results( "My inputbox", $midcols );
                }
            }
            elsif ( $testcase eq $RHS_MENU ) {
                if (
                    &rhs_menu(
                        (
                            "A menu",
                            "This menu uses \"tag\" values and descriptions:",
                            $midcols, ( $#demo_2col + 1 ) / 2
                        ),
                        @demo_2col
                    )
                  )
                {
                    &show_results( "My menu", $midcols );
                }
            }
            elsif ( $testcase eq $RHS_MENUL ) {
                if (
                    &rhs_menul(
                        (
                            "A menu", "This menu uses only the \"tag\" values:",
                            $midcols, $#demo_tags + 1
                        ),
                        @demo_tags
                    )
                  )
                {
                    &show_results( "My long-menu", $midcols );
                }
            }
            elsif ( $testcase eq $RHS_MENUA ) {
                if (
                    &rhs_menua(
                        "A menu", "This menu uses the sorted keys from a hash:",
                        $midcols, %demo_hash
                    )
                  )
                {
                    &show_results( "My alpha-menu", $midcols );
                }
            }
            elsif ( $testcase eq $RHS_CHECKLIST ) {
                if (
                    &rhs_checklist(
                        (
                            "A checklist",
                            "This checklist uses \"tag\" values"
                              . " and descriptions:",
                            $midcols,
                            ( $#demo_3col + 1 ) / 3
                        ),
                        @demo_3col
                    )
                  )
                {
                    &show_results( "My checklist", $midcols );
                }
            }
            elsif ( $testcase eq $RHS_CHECKLISTL ) {
                if (
                    &rhs_checklistl(
                        (
                            "A checklist",
                            "This checklist uses only the \"tag\" values:",
                            $midcols, $#demo_tags + 1
                        ),
                        @demo_tags
                    )
                  )
                {
                    &show_results( "My long-checklist", $midcols );
                }
            }
            elsif ( $testcase eq $RHS_CHECKLISTA ) {
                if (
                    &rhs_checklista(
                        "A checklist",
                        "This checklist uses the sorted keys from a hash:",
                        $midcols, %demo_hash
                    )
                  )
                {
                    &show_results( "My alpha-checklist", $midcols );
                }
            }
            elsif ( $testcase eq $RHS_RADIOLIST ) {
                if (
                    &rhs_radiolist(
                        (
                            "A radiolist",
                            "This radiolist uses \"tag\" values"
                              . " and descriptions:",
                            $midcols,
                            ( $#demo_3col + 1 ) / 3
                        ),
                        @demo_3col
                    )
                  )
                {
                    &show_results( "My radiolist", $midcols );
                }
            }
        }
    }
}

&doit;

1;