File Coverage

blib/lib/UI/Dialog/Screen/Druid.pm
Criterion Covered Total %
statement 23 80 28.7
branch 11 54 20.3
condition n/a
subroutine 8 14 57.1
pod 5 6 83.3
total 47 154 30.5


line stmt bran cond sub pod time code
1             package UI::Dialog::Screen::Druid;
2             ###############################################################################
3             # Copyright (C) 2004-2016 Kevin C. Krinke
4             #
5             # This library is free software; you can redistribute it and/or
6             # modify it under the terms of the GNU Lesser General Public
7             # License as published by the Free Software Foundation; either
8             # version 2.1 of the License, or (at your option) any later version.
9             #
10             # This library is distributed in the hope that it will be useful,
11             # but 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.
14             #
15             # You should have received a copy of the GNU Lesser General Public
16             # License along with this library; if not, write to the Free Software
17             # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18             ###############################################################################
19 1     1   12759 use 5.006;
  1         2  
20 1     1   3 use strict;
  1         0  
  1         15  
21 1     1   3 use warnings;
  1         2  
  1         27  
22 1     1   4 use constant { TRUE => 1, FALSE => 0 };
  1         0  
  1         77  
23              
24             BEGIN {
25 1     1   4 use vars qw($VERSION);
  1         1  
  1         38  
26 1     1   13 $VERSION = '1.21';
27             }
28              
29 1     1   332 use UI::Dialog;
  1         1  
  1         613  
30              
31             # Example Usage
32             #
33             # my $druid = new UI::Dialog::Screen::Druid
34             # ( dialog => $DIALOG,
35             # title => 'druid walkthrough'
36             # );
37             # $druid->add_yesno_step('bool0',"Boolean 0");
38             # $druid->add_yesno_step('bool1',"Boolean 1");
39             # my (%answers) = $druid->perform();
40             #
41              
42             sub new {
43 2     2 1 573 my ($class, %args) = @_;
44 2 50       7 unless (exists $args{dialog}) {
45             $args{dialog} = new UI::Dialog
46             (
47             title => (defined $args{title}) ? $args{title} : '',
48             backtitle => (defined $args{backtitle}) ? $args{backtitle} : '',
49             height => (defined $args{height}) ? $args{height} : 20,
50             width => (defined $args{width}) ? $args{width} : 65,
51             listheight => (defined $args{listheight}) ? $args{listheight} : 5,
52             order => (defined $args{order}) ? $args{order} : undef,
53             PATH => (defined $args{PATH}) ? $args{PATH} : undef,
54             beepbefore => (defined $args{beepbefore}) ? $args{beepbefore} : undef,
55             beepafter => (defined $args{beepafter}) ? $args{beepafter} : undef,
56 2 50       29 );
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
57             }
58 2 50       6 $args{performance} = [] unless exists $args{performance};
59 2         10 return bless { %args }, $class;
60             }
61              
62             #: not used yet, not sure keys being forced unique isn't too rigid
63             #
64             sub __verify_unique_tag {
65 0     0     my ($self,$tag) = @_;
66 0 0         if (grep {m!^\Q$tag\E$!} keys %{$self->{performance}}) {
  0            
  0            
67 0           return FALSE; # exists already, not unique tag
68             }
69             # doesn't exist, is unique tag
70 0           return TRUE;
71             }
72              
73             #: $druid->add_input_step( "key", "Label text", "Default text");
74             #: Add a text-input step to the performance
75             #
76             sub add_input_step {
77 0     0 1   my ($self,$tag,$text,$default) = @_;
78 0 0         push( @{$self->{performance}},
  0            
79             { type=>"input",
80             tag=>$tag,
81             text=>$text,
82             default=>defined $default ? $default : '',
83             }
84             );
85             }
86              
87             #: $druid->add_password_step( "key", "Label text" );
88             #: Add a password step to the performance
89             #
90             sub add_password_step {
91 0     0 1   my ($self,$tag,$text) = @_;
92 0           push( @{$self->{performance}},
  0            
93             { type=>"password",
94             tag=>$tag,
95             text=>$text
96             }
97             );
98             }
99              
100             #: $druid->add_menu_step( "key", "Label text", [qw|opt1 opt2 op3|] );
101             #: Add a menu select step to the performance
102             #
103             sub add_menu_step {
104 0     0 1   my ($self,$tag,$text,$options) = @_;
105 0           push( @{$self->{performance}},
  0            
106             { type=>"menu",
107             tag=>$tag,
108             text=>$text,
109             options=>$options
110             }
111             );
112             }
113              
114             #: $druid->add_yesno_step( "key", "Label text" );
115             #: Add a yesno step to the performance
116             #
117             sub add_yesno_step {
118 0     0 1   my ($self,$tag,$text) = @_;
119 0           push( @{$self->{performance}},
  0            
120             { type=>"yesno",
121             tag=>$tag,
122             text=>$text
123             }
124             );
125             }
126              
127             #: my (%answers) = $druid->perform();
128             #: Show the performance! Walk the user to the druid's step :)
129             #
130             sub perform {
131 0     0 0   my ($self) = @_;
132 0           my $key = undef;
133 0           my %answers = ();
134 0           foreach my $step ( @{$self->{performance}} ) {
  0            
135 0           $key = $step->{tag};
136 0           my $r = undef;
137             # yesno questions
138 0 0         if ($step->{type} eq "yesno") {
    0          
    0          
    0          
139             $r = $self->{dialog}->yesno
140             ( title => $step->{tag},
141             text => $step->{text}
142 0           );
143             goto PERFORM_STEP_FAILURE
144 0 0         if $self->{dialog}->state() eq "ESC";
145             }
146             # text-input questions
147             elsif ($step->{type} eq "input") {
148 0 0         my $default = defined $step->{default} ? $step->{default} : '';
149 0           foreach my $key (keys %answers) {
150 0           my $val = $answers{$key};
151 0 0         if ($default =~ m!\{\{\Q${key}\E\}\}!mg) {
152 0           $default =~ s!\{\{\Q${key}\E\}\}!${val}!g;
153             }
154             }
155 0           foreach my $step (@{$self->{performance}}) {
  0            
156 0 0         if (exists $step->{default}) {
157 0           my $key = $step->{tag};
158 0           my $val = $step->{default};
159 0 0         if ($default =~ m!\{\{\Q${key}\E\}\}!mg) {
160 0           $default =~ s!\{\{\Q${key}\E\}\}!${val}!g;
161             }
162             }
163             }
164             $r = $self->{dialog}->inputbox
165             ( title => $step->{tag},
166             text => $step->{text},
167 0           entry => $default
168             );
169             goto PERFORM_STEP_FAILURE
170 0 0         if $self->{dialog}->state() ne "OK";
171             }
172             # password questions
173             elsif ($step->{type} eq "password") {
174             $r = $self->{dialog}->password
175             ( title => $step->{tag},
176             text => $step->{text}
177 0           );
178             goto PERFORM_STEP_FAILURE
179 0 0         if $self->{dialog}->state() ne "OK";
180             }
181             # menu questions
182             elsif ($step->{type} eq "menu") {
183 0           my @list = ();
184 0           my $count = 0;
185 0           foreach (@{$step->{options}}) {
  0            
186 0           $count++;
187 0           push(@list,$count,$_);
188             }
189             $r = $self->{dialog}->menu
190             ( title => $step->{tag},
191             text => $step->{text},
192 0           list => \@list
193             );
194             goto PERFORM_STEP_FAILURE
195 0 0         if $self->{dialog}->state() ne "OK";
196 0           $r = $step->{options}[$r-1];
197             }
198 0           $answers{$key} = $r;
199             }
200 0 0         return wantarray ? %answers : \%answers;
201 0           PERFORM_STEP_FAILURE:
202             my %aborted = (aborted=>1,key=>$key);
203 0 0         return wantarray ? %aborted : \%aborted;
204             }
205              
206              
207             1; # END OF UI::Dialog::Screen::Druid