blib/lib/QWizard/Generator/ReadLine.pm untested

filter: Statementstmt 0.0% 0 / 196 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 37 Podpod 0.0% 0 / 33 totaltotal 0.0% 0 / 348 CC 20 SCAR 23.5
CC
20
Cov
0%
do_radio
42.0
do_yn
20.0
do_checkbox
20.0
CRAP 10.5
1 package QWizard::Generator::ReadLine;
2  
3 use Term::ReadLine;
4 require Exporter;
5 use QWizard::Generator;
6  
7 use strict;
8 our $VERSION = '3.15';
9  
10 @QWizard::Generator::ReadLine::ISA = qw(Exporter QWizard::Generator);
11  
12 our %defaults = ();
13  
14 our $width = 78;
15  
16 #our $clear_screen = `clear`; # not ideal, but hey. (see perlfaq8)
17 our $clear_screen = "\n\n";
18  
19 sub new {
20 my $type = shift;
21 my ($class) = ref($type) || $type;
22 my %self = %defaults;
23 my $self = \%self;
24 $self->{'term'} = new Term::ReadLine "QWizard::ReadLine";
25 bless($self, $class);
26 $self->add_handler('text',\&QWizard::Generator::ReadLine::do_entry,
27 [['single','name'],
28 ['default']]);
29 $self->add_handler('checkbox',\&QWizard::Generator::ReadLine::do_checkbox,
30 [['multi','values'],
31 ['default'],
32 ['single', 'name']]);
33 $self->add_handler('label',\&QWizard::Generator::ReadLine::do_label,
34 [['multi','values']]);
35 $self->add_handler('paragraph',\&QWizard::Generator::ReadLine::do_label,
36 [['multi','values']]);
37 $self->add_handler('multi_checkbox',
38 \&QWizard::Generator::ReadLine::do_multicheckbox,
39 [['multi','default'],
40 ['values,labels']]);
41 $self->add_handler('radio',
42 \&QWizard::Generator::ReadLine::do_radio,
43 [['values,labels', " "],
44 ['default'],
45 ['single','name']]);
46 $self->add_handler('textbox',\&QWizard::Generator::ReadLine::do_textbox,
47 [['single','name'],
48 ['default'],
49 ['single','size'],
50 ['single','maxsize'],
51 ['single','submit']]);
52 # reuse a radio since they're the same
53 $self->add_handler('menu',
54 \&QWizard::Generator::ReadLine::do_radio,
55 [['values,labels', " "],
56 ['default'],
57 ['single','name']]);
58 # pick a entry box for a file upload. Not quite right, but...
59 $self->add_handler('fileupload',
60 \&QWizard::Generator::ReadLine::do_entry,
61 [['default','values']]);
62 $self->add_handler('unknown',\&QWizard::Generator::ReadLine::do_unknown,
63 []);
64 $self->init_default_storage();
65 return $self;
66 }
67  
68 sub init_screen {
69 my ($self, $wiz, $title) = @_;
70  
71 print $clear_screen;
72 barrier("*");
73 maybechompp($title," -- ", " --");
74 }
75  
76 sub wait_for {
77 barrier("-");
78 }
79  
80 sub do_question {
81 my ($self, $q, $wiz, $p, $text, $qcount) = @_;
82  
83 chompp($text);
84 }
85  
86 sub do_question_end {
87 print "\n";
88 }
89  
90 sub start_questions {
91 my ($self, $wiz, $p, $title, $intro) = @_;
92 barrier("=");
93 maybechompp("$title", "title: ");
94 maybechompp($intro);
95 print "\n";
96 }
97  
98 sub end_questions {
99 $_[0]->install_tempvars();
100 }
101  
102 sub do_entry {
103 my ($self, $q, $wiz, $p, $name, $def) = @_;
104 $self->read_it($q, $name, $def);
105 }
106  
107 sub do_error {
108 my ($self, $q, $wiz, $p, $err) = @_;
109 print "ERROR: $err\n";
110 }
111  
112 sub do_textbox {
113 my ($self, $q, $wiz, $p, $name, $def) = @_;
114  
115 my ($text, $val);
116  
117 print "Enter text below. Enter a '.' on a line by itself to finish.\n";
118 do {
119 $text .= $val . "\n";
120 $val = $self->{'term'}->readline(": ");
121 } while ($val ne '.');
122 chomp($text);
123 $self->qwtemp($q->{'name'}, $text);
124 }
125  
126 sub do_yn {
127 my ($self, $def) = @_;
128 my $val = '';
129 do {
130 $val = $self->{'term'}->readline("y/n" . (($def) ? " [$def]" : "")
131 . ": ");
132 if ($val eq '' && $def) {
133 $val = $def;
134 } elsif ($val ne 'y' && $val ne 'n') {
135 print "*** illegal answer. You must pick 'y' or 'n'\n";
136 }
137 } while ($val ne 'y' && $val ne 'n');
138 return $val;
139 }
140  
141 sub get_yn_default {
142 my ($def, $vals) = @_;
143 my $val;
144 if ($def && $def eq $vals->[0]) {
145 $val = 'y';
146 } else { # lets just assume a default of n for easy entry
147 $val = 'n';
148 }
149 return $val;
150 }
151  
152 sub do_checkbox {
153 my ($self, $q, $wiz, $p, $vals, $def, $name) = @_;
154 my $val = '';
155 $vals = [1,0] if ($#$vals == -1);
156 $val = $self->do_yn(get_yn_default($def, $vals));
157 if ($val eq 'y') {
158 $self->qwtemp($name, $vals->[0]);
159 } elsif ($val eq 'n') {
160 $self->qwtemp($name, $vals->[1]);
161 }
162 }
163  
164 sub do_multicheckbox {
165 my ($self, $q, $wiz, $p, $defs, $vals, $labels) = @_;
166 my $count = -1;
167 foreach my $v (@$vals) {
168 $count++;
169 my $l = (($labels->{$v}) ? $labels->{$v} : "$v");
170 print $l,"\n";
171 my $val = $self->do_yn(get_yn_default($defs->[$count], [$v,'']));
172 if ($val eq 'y') {
173 $self->qwtemp($q->{name} . $v, $vals->[$count]);
174 } else {
175 $self->qwtemp($q->{name} . $v, '');
176 }
177 push @{$wiz->{'passvars'}},$q->{'name'} . $v;
178 }
179 }
180  
181 sub do_radio {
182 my ($self, $q, $wiz, $p, $vals, $labels, $def) = @_;
183 my $count = 0;
184 my $defcount;
185 foreach my $v (@$vals) {
186 $count++;
187 my $text = (($labels->{$v}) ? $labels->{$v} : "$v");
188 if ($def eq $v) {
189 $defcount = $count;
190 }
191 printf " %3d) %s\n", $count, $text;
192 }
193 my $val;
194 while ($val !~ /^\d+$/ || $val-1 < 0 || $val-1 > $#$vals) {
195 if ($val ne '') {
196 print "*** illegal answer\n";
197 }
198 $val = $self->{'term'}->readline("Pick one" .
199 (($defcount) ? " [$defcount]" : "") .
200 ": ");
201 $val = $defcount if ($val eq '')
202 }
203 $self->qwtemp($q->{name}, $vals->[$val-1]);
204 }
205  
206 sub do_label {
207 my ($self, $q, $wiz, $p, $vals, $def) = @_;
208 if (defined ($vals)) {
209 map { chompp($_); }@$vals;
210 }
211 }
212  
213 sub do_menu {
214 $_[0]->do_radio(@_);
215 }
216  
217 sub do_separator {
218 my ($self, $q, $wiz, $p, $text) = @_;
219 print "$text\n";
220 }
221  
222 sub do_unknown {
223 my ($self, $q, $wiz, $p) = @_;
224  
225 print "Error: Unhandled question type '$q->{type}' in primary '$p->{module_name}'.\nIt is highly likely that this page will not function properly after this point.\n";
226 }
227  
228 sub read_it {
229 my ($self, $q, $name, $def, $prompt) = @_;
230 my $val = $self->{'term'}->readline($prompt ||
231 ((($def)?"[$def] ":"") . ": "));
232 if ($val eq '' && $def) {
233 $self->qwtemp($name, $def);
234 } else {
235 $self->qwtemp($name, $val);
236 }
237 }
238  
239 ##################################################
240 # action confirm
241 ##################################################
242  
243 sub start_confirm {
244 my ($self, $wiz) = @_;
245  
246 barrier("*");
247 print "Wrapping up:\n";
248 print "Do you want to commit the following changes:\n";
249 }
250  
251 sub end_confirm {
252 my ($self, $wiz) = @_;
253 my $val = $self->do_yn();
254 if ($val eq 'y') {
255 $self->qwparam('wiz_confirmed','Commit');
256 } else {
257 $self->qwparam('wiz_confirmed','Cancel');
258 }
259 }
260  
261 sub do_confirm_message {
262 my ($self, $wiz, $msg) = @_;
263 chompp($msg, " ");
264 }
265  
266 sub canceled_confirm {
267 print "*** cancelled\n";
268 }
269  
270 ##################################################
271 # actions
272 ##################################################
273  
274 sub start_actions {
275 my ($self, $wiz) = @_;
276 barrier("*");
277 print "Processing your request...\n";
278 }
279 sub end_actions {
280 my ($self, $wiz) = @_;
281 print "Done!\n";
282 }
283 sub do_action_output {
284 my ($self, $wiz, $action) = @_;
285 chompp("$action", " ");
286 }
287 sub do_action_error {
288 my ($self, $wiz, $errstr) = @_;
289 chompp("$errstr", "ERROR: ");
290 }
291  
292 ##################################################
293 # utils
294 ##################################################
295  
296 sub chompp {
297 my ($text, $prefix, $suffix) = shift;
298 chomp($text);
299 print $prefix, $text, $suffix,"\n";
300 }
301  
302 sub maybechompp {
303 return if (!$_[0]);
304 chompp(@_);
305 }
306  
307 sub barrier {
308 print $_[0] x $width, "\n";
309 }
310  
311 ## we have a temporary storage that we fill in later into the master storage.
312  
313 our %tempvars;
314  
315 sub qwtemp {
316 my ($self, $name, $val) = @_;
317 $tempvars{$name} = $val;
318 }
319  
320 sub install_tempvars {
321 my $self = shift;
322 foreach my $k (keys(%tempvars)) {
323 $self->qwparam($k, $tempvars{$k});
324 }
325 %tempvars = ();
326 }
327  
328 1;