Dialog/Screen/Druid.pm

filter: Statementstmt 29.6% 24 / 81 Branchbran 20.3% 11 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 57.1% 8 / 14 Podpod 83.3% 5 / 6 totaltotal 30.9% 48 / 155 CC 32 SCAR 51.2
CC
32
Cov
26%
perform
380.0
new
21.7
__verify_unique_tag
6.0
CRAP 167.1
« Dialog/Gauged.pm ↑ Summary Dialog/Console.pm »
1 package UI::Dialog::Screen::Druid;
2 ###############################################################################
3 # Copyright (C) 2004-2016 Kevin C. Krinke <kevin@krinke.ca>
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 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use constant { TRUE => 1, FALSE => 0 };
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 BEGIN {
25 1 ▶ use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶ $VERSION = '1.20';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 }
28  
29 1 ▶use UI::Dialog;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 1 ▶ my ($class, %args) = @_;
Subroutine new: called CC=12 SCAR=30.8
Pod documented
44 1 ▶ unless (exists $args{dialog}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"dialog"}) 1 0 1
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 1 ▶ );
Branch: 9 branches
BranchTrueFalseTotal
defined $args{'title'} ? : 0 1 1
defined $args{'backtitle'} ? : 0 1 1
defined $args{'height'} ? : 0 1 1
defined $args{'width'} ? : 0 1 1
defined $args{'listheight'} ? : 0 1 1
defined $args{'order'} ? : 0 1 1
defined $args{'PATH'} ? : 0 1 1
defined $args{'beepbefore'} ? : 0 1 1
defined $args{'beepafter'} ? : 0 1 1
57 }
58 1 ▶ $args{performance} = [] unless exists $args{performance};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"performance"} 1 0 1
59 1 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 ▶ my ($self,$tag) = @_;
Subroutine __verify_unique_tag: not called CC=2 SCAR=17.9
66 0 ▶ if (grep {m!^\Q$tag\E$!} keys %{$self->{performance}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/^\Q$tag\E$/;} keys %{$$self{"performance"};}) 0 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 ▶ my ($self,$tag,$text,$default) = @_;
Subroutine add_input_step: not called CC=2 SCAR=17.9
Pod documented
78 0 ▶ push( @{$self->{performance}},
Branch: 1 branch
BranchTrueFalseTotal
defined $default ? : 0 0 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 ▶ my ($self,$tag,$text) = @_;
Subroutine add_password_step: not called CC=1 SCAR=6.9
Pod documented
92 0 push( @{$self->{performance}},
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 ▶ my ($self,$tag,$text,$options) = @_;
Subroutine add_menu_step: not called CC=1 SCAR=6.9
Pod documented
105 0 push( @{$self->{performance}},
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 ▶ my ($self,$tag,$text) = @_;
Subroutine add_yesno_step: not called CC=1 SCAR=6.9
Pod documented
119 0 push( @{$self->{performance}},
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 ▶ my ($self) = @_;
Subroutine perform: not called CC=19 SCAR=59.4
Pod undocumented
132 0 my $key = undef;
133 0 my %answers = ();
134 0 foreach my $step ( @{$self->{performance}} ) {
135 0 $key = $step->{tag};
136 0 my $r = undef;
137 # yesno questions
138 0 ▶ if ($step->{type} eq "yesno") {
Branch: 4 branches
BranchTrueFalseTotal
if ($step->{'type'} eq 'yesno') { } 0 0 0
elsif ($step->{'type'} eq 'input') { } 0 0 0
elsif ($step->{'type'} eq 'password') { } 0 0 0
elsif ($step->{'type'} eq 'menu') { } 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 ▶ if $self->{dialog}->state() eq "ESC";
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dialog"}->state eq "ESC" 0 0 0
145 }
146 # text-input questions
147 elsif ($step->{type} eq "input") {
148 0 ▶ my $default = defined $step->{default} ? $step->{default} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $step->{'default'} ? : 0 0 0
149 0 foreach my $key (keys %answers) {
150 0 my $val = $answers{$key};
151 0 ▶ if ($default =~ m!\{\{\Q${key}\E\}\}!mg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default =~ /\{\{\Q$key\E\}\}/gm) 0 0 0
152 0 $default =~ s!\{\{\Q${key}\E\}\}!${val}!g;
153 }
154 }
155 0 foreach my $step (@{$self->{performance}}) {
156 0 ▶ if (exists $step->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $step->{"default"}) 0 0 0
157 0 my $key = $step->{tag};
158 0 my $val = $step->{default};
159 0 ▶ if ($default =~ m!\{\{\Q${key}\E\}\}!mg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default =~ /\{\{\Q$key\E\}\}/gm) 0 0 0
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 ▶ if $self->{dialog}->state() ne "OK";
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dialog"}->state ne "OK" 0 0 0
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 ▶ if $self->{dialog}->state() ne "OK";
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dialog"}->state ne "OK" 0 0 0
180 }
181 # menu questions
182 elsif ($step->{type} eq "menu") {
183 0 my @list = ();
184 0 my $count = 0;
185 0 foreach (@{$step->{options}}) {
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 ▶ if $self->{dialog}->state() ne "OK";
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dialog"}->state ne "OK" 0 0 0
196 0 $r = $step->{options}[$r-1];
197 }
198 0 $answers{$key} = $r;
199 }
200 0 ▶ return wantarray ? %answers : \%answers;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
201 0 PERFORM_STEP_FAILURE:
202 my %aborted = (aborted=>1,key=>$key);
203 0 ▶ return wantarray ? %aborted : \%aborted;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
204 }
205  
206  
207 1 1; # END OF UI::Dialog::Screen::Druid
« Dialog/Gauged.pm ↑ Summary Dialog/Console.pm »