lib/TUI/Handy.pm

filter: Statementstmt 71.0% 376 / 529 Branchbran 57.0% 219 / 384 Conditioncond 61.9% 65 / 105 MC/DCmcdc 47.6% 30 / 63 Subroutinesub 78.0% 32 / 41 Podpod 100% 6 / 6 totaltotal 64.5% 728 / 1128 CC 239 SCAR 47.3
CC
239
Cov
65%
_read_key
702.0
_handle_event
244.6
_main
56.0
CRAP 112.9
↑ Summary
1 package TUI::Handy;
2 #
3 # TUI::Handy - Text-based user interface (ANSI escape only, no external modules)
4 #
5 # A tiny form toolkit driven by a plain-text DSL. You write one text file that
6 # looks like the screen you want; running it turns that text into an interactive
7 # keyboard-driven form on the console. The result is returned as a hash
8 # reference whose keys are the labels written in the DSL.
9 #
10 # Design notes
11 # * Pure Perl, no CPAN / no ncurses. Works on Perl 5.005_03 and later.
12 # * Source is US-ASCII; all DSL text (which may be Japanese) is read at run
13 # time, so no non-ASCII byte ever appears in this file.
14 # * Multibyte (zenkaku) text is handled at the byte level. UTF-8, Shift_JIS
15 # (CP932) and EUC-JP are supported for display-width and character counting.
16 # * Two drivers: an ANSI full-screen driver (used when the terminal can be put
17 # into cbreak/raw mode via stty) and a portable line-oriented fallback (used
18 # on bare Windows cmd.exe where stty is not available).
19 #
20 # Usage as a module
21 # use TUI::Handy;
22 # my $tui = TUI::Handy->new(dsl => $text); # or new(file => $path)
23 # $tui->set('Register', sub { my $form = shift; save($form); 0 }); # button
24 # $tui->set('Company', 'ACME'); # preset
25 # my $form = $tui->run; # returns a hash reference
26 #
27 # Usage as a command
28 # perl Handy.pm form.txt # runs form.txt, prints results
29 #
30 # Install path when used as a module: lib/TUI/Handy.pm
31 #
32 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 7 ▶use vars qw($VERSION $ENCODING $SAVED_STTY $RAW_ON $SAVED_SIG);
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 7 $VERSION = '0.01';
36  
37 # Default encoding is guessed from the operating system, but may be overridden
38 # with $TUI::Handy::ENCODING or the TUI_HANDY_ENCODING environment variable.
39 # Recognised values: 'utf8', 'sjis', 'euc'.
40 7 ▶$ENCODING = ($^O =~ /MSWin32/i) ? 'sjis' : 'utf8';
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /MSWin32/i ? : 0 7 7
41 7 ▶$ENCODING = $ENV{'TUI_HANDY_ENCODING'} if defined $ENV{'TUI_HANDY_ENCODING'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"TUI_HANDY_ENCODING"} 0 7 7
42  
43 7 $SAVED_STTY = ''; # terminal settings saved before entering cbreak mode
44 7 $RAW_ON = 0; # true while the terminal is in cbreak mode
45 7 $SAVED_SIG = {}; # signal handlers displaced while in cbreak mode
46  
47 # ANSI escape (single byte 0x1b) - written with \e so the source stays ASCII.
48 7 my $E = "\e";
49  
50 #--------------------------------------------------------------------------
51 # Low level multibyte helpers
52 #
53 # Both routines walk a byte string one character at a time. The number of
54 # bytes consumed for each character depends on the encoding. _width() returns
55 # the number of terminal columns the string occupies; _chars() splits the
56 # string into a list of one-character byte strings.
57 #--------------------------------------------------------------------------
58  
59 # Return how many bytes the character starting at $o (a lead byte) occupies,
60 # and how many display columns it uses, for the current encoding.
61 #
62 # $b2 and $b3 are the two bytes that follow, or -1 when they are unknown or
63 # past the end of the string. They are needed only by the UTF-8 branch, to
64 # tell half-width katakana apart from the other three-byte characters; a
65 # caller that wants the byte length alone may leave them out.
66 1390 ▶sub _char_len {
Subroutine _char_len: called CC=19 SCAR=31.1
67 1390 my ($o, $enc, $b2, $b3) = @_;
68 1390 ▶ $b2 = -1 unless defined $b2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b2 0 1390 1390
69 1390 ▶ $b3 = -1 unless defined $b3;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b3 0 1390 1390
70 1390 ▶ return (1, 1) if $o < 0x80; # plain ASCII
Branch: 1 branch
BranchTrueFalseTotal
if $o < 128 1373 17 1390
71 17 ▶ if ($enc eq 'sjis') {
Branch: 2 branches
BranchTrueFalseTotal
if ($enc eq 'sjis') { } 3 14 17
elsif ($enc eq 'euc') { } 2 12 14
72 3 ▶ return (1, 1) if $o >= 0xa1 && $o <= 0xdf; # half-width katakana
Branch: 1 branch
BranchTrueFalseTotal
if $o >= 161 and $o <= 223 1 2 3
Condition66%2/3
$o >= 161 and $o <= 223
!ll&&!rl&&r
201
MC/DC50%1/2
$o >= 161 and $o <= 223
$o >= 161 $o <= 223
Truth table66%2/3
A and B
A= $o >= 161B= $o <= 223
ABresult
0X0
100
111
73 2 return (2, 2); # double-byte
74 }
75 elsif ($enc eq 'euc') {
76 2 ▶ return (2, 1) if $o == 0x8e; # SS2 half-width katakana
Branch: 1 branch
BranchTrueFalseTotal
if $o == 142 1 1 2
77 1 ▶ return (3, 2) if $o == 0x8f; # SS3
Branch: 1 branch
BranchTrueFalseTotal
if $o == 143 0 1 1
78 1 return (2, 2); # GR double-byte
79 }
80 else { # utf8
81 12 ▶ return (4, 2) if $o >= 0xf0;
Branch: 1 branch
BranchTrueFalseTotal
if $o >= 240 0 12 12
82 12 ▶ if ($o == 0xef) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o == 239) 9 3 12
83 # Half-width katakana U+FF61-U+FF9F encodes as ef bd a1 - ef bd bf
84 # and ef be 80 - ef be 9f. One column, like its Shift_JIS and
85 # EUC-JP counterparts.
86 9 ▶ return (3, 1) if $b2 == 0xbd && $b3 >= 0xa1;
Branch: 1 branch
BranchTrueFalseTotal
if $b2 == 189 and $b3 >= 161 3 6 9
Condition100%3/3
$b2 == 189 and $b3 >= 161
!ll&&!rl&&r
423
MC/DC100%2/2
$b2 == 189 and $b3 >= 161
$b2 == 189 $b3 >= 161
Truth table100%3/3
A and B
A= $b2 == 189B= $b3 >= 161
ABresult
0X0
100
111
87 6 ▶ return (3, 1) if $b2 == 0xbe && $b3 >= 0x80 && $b3 <= 0x9f;
Branch: 1 branch
BranchTrueFalseTotal
if $b2 == 190 and $b3 >= 128 and $b3 <= 159 3 3 6
Condition66%4/6
$b2 == 190 and $b3 >= 128 and $b3 <= 159
!ll&&!rl&&r
303
$b2 == 190 and $b3 >= 128
!ll&&!rl&&r
303
MC/DC33%1/3
$b2 == 190 and $b3 >= 128 and $b3 <= 159
$b2 == 190 $b3 >= 128 $b3 <= 159
Truth table50%2/4
A and B and C
A= $b2 == 190B= $b3 >= 128C= $b3 <= 159
ABCresult
0XX0
10X0
1100
1111
88 }
89 6 ▶ return (3, 2) if $o >= 0xe0;
Branch: 1 branch
BranchTrueFalseTotal
if $o >= 224 6 0 6
90 0 ▶ return (2, 1) if $o >= 0xc0; # 2-byte utf8 (Latin etc.)
Branch: 1 branch
BranchTrueFalseTotal
if $o >= 192 0 0 0
91 0 return (1, 1); # stray continuation byte
92 }
93 }
94  
95 # Display width (columns) of a byte string.
96 310 ▶sub _width {
Subroutine _width: called CC=5 SCAR=16.1
97 310 my ($s, $enc) = @_;
98 310 ▶ $enc = $ENCODING unless defined $enc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $enc 296 14 310
99 310 my $w = 0;
100 310 my $i = 0;
101 310 my $n = length $s;
102 310 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 900 310 1210
103 900 ▶ my ($bl, $cw) = _char_len(ord(substr($s, $i, 1)), $enc,
Branch: 2 branches
BranchTrueFalseTotal
$i + 1 < $n ? : 763 137 900
$i + 2 < $n ? : 630 270 900
104 ($i + 1 < $n) ? ord(substr($s, $i + 1, 1)) : -1,
105 ($i + 2 < $n) ? ord(substr($s, $i + 2, 1)) : -1);
106 900 $w += $cw;
107 900 $i += $bl;
108 }
109 310 return $w;
110 }
111  
112 # Split a byte string into a list of characters (each a 1+ byte string).
113 85 ▶sub _chars {
Subroutine _chars: called CC=5 SCAR=16.1
114 85 my ($s, $enc) = @_;
115 85 ▶ $enc = $ENCODING unless defined $enc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $enc 81 4 85
116 85 my @out;
117 85 my $i = 0;
118 85 my $n = length $s;
119 85 ▶ while ($i < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 490 85 575
120 490 ▶ my ($bl, $cw) = _char_len(ord(substr($s, $i, 1)), $enc,
Branch: 2 branches
BranchTrueFalseTotal
$i + 1 < $n ? : 406 84 490
$i + 2 < $n ? : 325 165 490
121 ($i + 1 < $n) ? ord(substr($s, $i + 1, 1)) : -1,
122 ($i + 2 < $n) ? ord(substr($s, $i + 2, 1)) : -1);
123 490 push @out, substr($s, $i, $bl);
124 490 $i += $bl;
125 }
126 85 return @out;
127 }
128  
129 # True if the whole string is 7-bit ASCII.
130 2 ▶sub _is_ascii {
Subroutine _is_ascii: called CC=2 SCAR=6.9
131 2 my $s = shift;
132 2 ▶ return $s =~ /^[\x00-\x7f]*$/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /^[\x00-\x7f]*$/ ? : 1 1 2
133 }
134  
135 #--------------------------------------------------------------------------
136 # Constructor
137 #--------------------------------------------------------------------------
138 45 ▶sub new {
Subroutine new: called CC=4 SCAR=14.8
Pod documented
139 45 my $class = shift;
140 45 my %arg = @_;
141 45 my $self = {
142 widgets => [], # ordered list of parsed widgets
143 focus => [], # indices (into widgets) of focusable widgets
144 form => {}, # the value hash returned by run()
145 order_keys => [], # data keys in DSL order (for command-mode dump)
146 done => 0, # set true to leave the run loop
147 aborted => 0, # set true when the user pressed ESC
148 pressed => undef, # label of the button that closed the form
149 };
150 45 bless $self, $class;
151  
152 45 my $dsl = $arg{'dsl'};
153 45 ▶ if (!defined $dsl && defined $arg{'file'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $dsl and defined $arg{"file"}) 0 45 45
Condition33%1/3
not defined $dsl and defined $arg{"file"}
!ll&&!rl&&r
4500
MC/DC0%0/2
not defined $dsl and defined $arg{"file"}
not defined $dsl defined $arg{"file"}
Truth table33%1/3
not(A) and B
A= not defined $dslB= defined $arg{"file"}
ABresult
0X0
100
111
154 0 $dsl = _slurp($arg{'file'});
155 }
156 45 ▶ $dsl = '' unless defined $dsl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dsl 0 45 45
157 45 $self->_parse($dsl);
158 45 return $self;
159 }
160  
161 # Read a whole file using a two-argument open (5.005_03 style).
162 0 ▶sub _slurp {
Subroutine _slurp: not called CC=2 SCAR=17.9
163 0 my $file = shift;
164 0 local *IN;
165 0 ▶ open(IN, "<$file") or die "TUI::Handy: cannot open $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open IN, "<$file" or die "TUI::Handy: cannot open ${file}: $!\n" 0 0 0
166 0 local $/;
167 0 my $data = <IN>;
168 0 close(IN);
169 0 return $data;
170 }
171  
172 #--------------------------------------------------------------------------
173 # DSL parser
174 #
175 # Every line becomes exactly one widget so that the screen and the data
176 # structure stay in one-to-one correspondence. The widget kinds are:
177 #
178 # label plain text / separator line (also serves as a radio group title)
179 # text labelled input box: Label: [____] [####] [$$$$] [YYYYMMDD] [box]
180 # check checkbox: [X] Label or [ ] Label
181 # radio radio button: (*) Label or ( ) Label
182 # button push button: [Label] (bracket only, no label before it)
183 #--------------------------------------------------------------------------
184 45 ▶sub _parse {
Subroutine _parse: called CC=26 SCAR=32.8
185 45 my ($self, $dsl) = @_;
186 45 my @lines = split(/\r?\n/, $dsl, -1);
187 # Drop a single trailing empty element produced by a final newline.
188 45 ▶ pop @lines if @lines && $lines[-1] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if @lines and $lines[-1] eq "" 45 0 45
Condition33%1/3
@lines and $lines[-1] eq ""
!ll&&!rl&&r
0045
MC/DC0%0/2
@lines and $lines[-1] eq ""
@lines $lines[-1] eq ""
Truth table33%1/3
A and B
A= @linesB= $lines[-1] eq ""
ABresult
0X0
100
111
189  
190 45 my $last_heading = ''; # most recent heading, used as a radio group title
191 45 my $prev_radio = 0; # was the previous parsed line a radio button?
192 45 my $cur_group = ''; # current radio group title
193  
194 45 my $row = 0;
195 45 for my $line (@lines) {
196 322 $row++;
197 322 $line =~ s/\r$//;
198  
199 # -- radio button: (*) label or ( ) label -----------------------
200 322 ▶ if ($line =~ /^(\s*)\(([ *])\)\s*(\S.*?)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\s*)\(([ *])\)\s*(\S.*?)\s*$/) 77 245 322
201 77 my ($pre, $mark, $label) = ($1, $2, $3);
202 77 ▶ if (!$prev_radio) { # any non-radio line starts a new group
Branch: 1 branch
BranchTrueFalseTotal
unless ($prev_radio) 39 38 77
203 39 $cur_group = $last_heading;
204 39 $cur_group =~ s/\s*:\s*$//; # strip a trailing colon
205 39 $cur_group =~ s/^\s+//;
206 39 $cur_group =~ s/\s+$//;
207 }
208 77 ▶ my $sel = ($mark eq '*') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$mark eq '*' ? : 39 38 77
209 77 $self->_add({
210 kind => 'radio',
211 group => $cur_group,
212 label => $label,
213 selected=> $sel,
214 pre => $pre . '(',
215 post => ') ' . $label,
216 row => $row,
217 col => _width($pre) + 1,
218 });
219 77 ▶ $self->{form}{$cur_group} = $label if $sel;
Branch: 1 branch
BranchTrueFalseTotal
if $sel 39 38 77
220 77 $self->_remember_key($cur_group);
221 77 $prev_radio = 1;
222 77 next;
223 }
224 245 $prev_radio = 0;
225  
226 # -- checkbox: [X] label or [ ] label ---------------------------
227 245 ▶ if ($line =~ /^(\s*)\[([ xX])\]\s*(\S.*?)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\s*)\[([ xX])\]\s*(\S.*?)\s*$/) 40 205 245
228 40 my ($pre, $mark, $label) = ($1, $2, $3);
229 40 ▶ my $val = ($mark =~ /[xX]/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$mark =~ /[xX]/ ? : 22 18 40
230 40 $self->_add({
231 kind => 'check',
232 label => $label,
233 value => $val,
234 pre => $pre . '[',
235 post => '] ' . $label,
236 row => $row,
237 col => _width($pre) + 1,
238 });
239 40 $self->{form}{$label} = $val;
240 40 $self->_remember_key($label);
241 40 next;
242 }
243  
244 # -- something in square brackets: text box or button ----------------
245 205 ▶ if ($line =~ /^(.*?)\[([^\]]*)\](.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(.*?)\[([^\]]*)\](.*)$/) 110 95 205
246 110 my ($pre, $inner, $post) = ($1, $2, $3);
247  
248 # No label text before the bracket -> push button.
249 110 ▶ if ($pre =~ /^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pre =~ /^\s*$/) 47 63 110
250 47 $self->_add({
251 kind => 'button',
252 label => $inner,
253 pre => $pre . '[',
254 post => ']' . $post,
255 row => $row,
256 col => _width($pre) + 1,
257 });
258 # Buttons default to no handler until the caller registers one.
259 47 ▶ $self->{form}{$inner} = undef unless exists $self->{form}{$inner};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"form"}{$inner} 47 0 47
260 47 $self->_remember_key($inner);
261 47 next;
262 }
263  
264 # Labelled bracket -> text input box. Decide the sub-type from
265 # the fill characters written between the brackets.
266 63 my $subtype;
267 63 my $just = 'L';
268 63 ▶ if ($inner =~ /^_+$/) { $subtype = 'hankaku'; }
Branch: 6 branches
BranchTrueFalseTotal
if ($inner =~ /^_+$/) { } 47 16 63
elsif ($inner =~ /^\#+$/) { } 14 2 16
elsif ($inner =~ /^[\$\\]+$/) { } 1 1 2
elsif ($inner =~ /^%+$/) { } 0 1 1
elsif ($inner =~ /^[YMD]+$/i) { } 1 0 1
elsif (not _is_ascii($inner)) { } 0 0 0
269 14 elsif ($inner =~ /^\#+$/) { $subtype = 'num'; $just = 'R'; }
270 1 elsif ($inner =~ /^[\$\\]+$/) { $subtype = 'cur'; $just = 'R'; }
271 0 elsif ($inner =~ /^%+$/) { $subtype = 'date'; }
272 1 elsif ($inner =~ /^[YMD]+$/i) { $subtype = 'date'; }
273 0 elsif (!_is_ascii($inner)) { $subtype = 'zenkaku'; }
274 0 else { $subtype = 'hankaku'; }
275  
276 63 my $key = $pre;
277 63 $key =~ s/\s*:\s*$//; # "Company:" -> "Company"
278 63 $key =~ s/^\s+//;
279 63 $key =~ s/\s+$//;
280  
281 63 my @cells = _chars($inner); # marker cells -> max input length
282 63 $self->_add({
283 kind => 'text',
284 subtype => $subtype,
285 key => $key,
286 label => $key,
287 size => scalar(@cells), # maximum number of characters
288 iw => _width($inner), # interior width in columns
289 just => $just,
290 chars => [], # current value as a list of chars
291 caret => 0,
292 pre => $pre . '[',
293 post => ']' . $post,
294 row => $row,
295 col => _width($pre) + 1,
296 });
297 63 $self->{form}{$key} = '';
298 63 $self->_remember_key($key);
299 63 next;
300 }
301  
302 # -- plain heading / separator line ----------------------------------
303 95 $self->_add({ kind => 'label', text => $line, row => $row });
304 95 ▶ if ($line =~ /\S/ && $line !~ /^[\s\-=_.*]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\S/ and not $line =~ /^[\s\-=_.*]+$/) 56 39 95
Condition100%3/3
$line =~ /\S/ and not $line =~ /^[\s\-=_.*]+$/
!ll&&!rl&&r
38156
MC/DC100%2/2
$line =~ /\S/ and not $line =~ /^[\s\-=_.*]+$/
$line =~ /\S/ not $line =~ /^[\s\-=_.*]+$/
Truth table100%3/3
A and not(B)
A= $line =~ /\S/B= not $line =~ /^[\s\-=_.*]+$/
ABresult
0X0
100
111
305 56 $last_heading = $line;
306 }
307 }
308  
309 # Build the focus ring (every interactive widget, in screen order).
310 45 my $i = 0;
311 45 for my $w (@{$self->{widgets}}) {
312 227 push @{$self->{focus}}, $i
313 if $w->{kind} eq 'text'
314 || $w->{kind} eq 'check'
315 || $w->{kind} eq 'radio'
316 322 ▶ || $w->{kind} eq 'button';
Branch: 1 branch
BranchTrueFalseTotal
if $w->{"kind"} eq "text" or $w->{"kind"} eq "check" or $w->{"kind"} eq "radio" or $w->{"kind"} eq "button" 227 95 322
Condition100%9/9
$w->{"kind"} eq "text" or $w->{"kind"} eq "check" or $w->{"kind"} eq "radio" or $w->{"kind"} eq "button"
l!l&&r!l&&!r
1804795
$w->{"kind"} eq "text" or $w->{"kind"} eq "check" or $w->{"kind"} eq "radio"
l!l&&r!l&&!r
10377142
$w->{"kind"} eq "text" or $w->{"kind"} eq "check"
l!l&&r!l&&!r
6340219
MC/DC100%4/4
$w->{"kind"} eq "text" or $w->{"kind"} eq "check" or $w->{"kind"} eq "radio" or $w->{"kind"} eq "button"
$w->{"kind"} eq "text" $w->{"kind"} eq "check" $w->{"kind"} eq "radio" $w->{"kind"} eq "button"
Truth table100%5/5
A or B or C or D
A= $w->{"kind"} eq "text"B= $w->{"kind"} eq "check"C= $w->{"kind"} eq "radio"D= $w->{"kind"} eq "button"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
317 322 $i++;
318 }
319 45 $self->{fi} = 0; # current position within the focus ring
320 }
321  
322 322 ▶sub _add {
Subroutine _add: called CC=1 SCAR=0.0
323 322 my ($self, $w) = @_;
324 322 push @{$self->{widgets}}, $w;
325 }
326  
327 # Record a data key once, preserving DSL order (used by command-mode output).
328 227 ▶sub _remember_key {
Subroutine _remember_key: called CC=2 SCAR=6.9
329 227 my ($self, $k) = @_;
330 227 ▶ return if $self->{_seen}{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_seen"}{$k} 38 189 227
331 189 $self->{_seen}{$k} = 1;
332 189 push @{$self->{order_keys}}, $k;
333 }
334  
335 #--------------------------------------------------------------------------
336 # Public helpers
337 #--------------------------------------------------------------------------
338  
339 # set(LABEL, VALUE)
340 # If VALUE is a code reference it becomes the handler for the button LABEL.
341 # Otherwise VALUE presets the value of the text box / checkbox / radio group
342 # whose key is LABEL.
343 28 ▶sub set {
Subroutine set: called CC=19 SCAR=29.5
Pod documented
344 28 my ($self, $key, $val) = @_;
345 28 ▶ if (ref($val) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "CODE") 20 8 28
346 20 $self->{form}{$key} = $val; # button handler
347 20 return $self;
348 }
349 8 for my $w (@{$self->{widgets}}) {
350 37 ▶ if ($w->{kind} eq 'text' && $w->{key} eq $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w->{"kind"} eq "text" and $w->{"key"} eq $key) 4 33 37
Condition100%3/3
$w->{"kind"} eq "text" and $w->{"key"} eq $key
!ll&&!rl&&r
2494
MC/DC100%2/2
$w->{"kind"} eq "text" and $w->{"key"} eq $key
$w->{"kind"} eq "text" $w->{"key"} eq $key
Truth table100%3/3
A and B
A= $w->{"kind"} eq "text"B= $w->{"key"} eq $key
ABresult
0X0
100
111
351 4 $self->_set_text($w, $val);
352 4 return $self;
353 }
354 33 ▶ if ($w->{kind} eq 'check' && $w->{label} eq $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w->{"kind"} eq "check" and $w->{"label"} eq $key) 1 32 33
Condition100%3/3
$w->{"kind"} eq "check" and $w->{"label"} eq $key
!ll&&!rl&&r
2931
MC/DC100%2/2
$w->{"kind"} eq "check" and $w->{"label"} eq $key
$w->{"kind"} eq "check" $w->{"label"} eq $key
Truth table100%3/3
A and B
A= $w->{"kind"} eq "check"B= $w->{"label"} eq $key
ABresult
0X0
100
111
355 1 ▶ $w->{value} = $val ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 0 1 1
356 1 $self->{form}{$key} = $w->{value};
357 1 return $self;
358 }
359 }
360 # Radio group preset: select the member whose label matches VALUE. A
361 # value naming no member of an existing group is ignored rather than
362 # applied: clearing every button and storing a label that is not on the
363 # screen would leave the form in a state the user could not have reached,
364 # and the caller would never learn of the typo.
365 3 ▶ my $v = defined($val) ? $val : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 3 0 3
366 3 my $group = 0;
367 3 my $hit = 0;
368 3 for my $w (@{$self->{widgets}}) {
369 24 ▶ next unless $w->{kind} eq 'radio' && $w->{group} eq $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $w->{"kind"} eq "radio" and $w->{"group"} eq $key 18 6 24
Condition66%2/3
$w->{"kind"} eq "radio" and $w->{"group"} eq $key
!ll&&!rl&&r
1806
MC/DC50%1/2
$w->{"kind"} eq "radio" and $w->{"group"} eq $key
$w->{"kind"} eq "radio" $w->{"group"} eq $key
Truth table66%2/3
A and B
A= $w->{"kind"} eq "radio"B= $w->{"group"} eq $key
ABresult
0X0
100
111
370 6 $group = 1;
371 6 ▶ $hit = 1 if $w->{label} eq $v;
Branch: 1 branch
BranchTrueFalseTotal
if $w->{"label"} eq $v 1 5 6
372 }
373 3 ▶ return $self if $group && !$hit;
Branch: 1 branch
BranchTrueFalseTotal
if $group and not $hit 2 1 3
Condition66%2/3
$group and not $hit
!ll&&!rl&&r
012
MC/DC50%1/2
$group and not $hit
$group not $hit
Truth table66%2/3
A and not(B)
A= $groupB= not $hit
ABresult
0X0
100
111
374  
375 1 for my $w (@{$self->{widgets}}) {
376 8 ▶ if ($w->{kind} eq 'radio' && $w->{group} eq $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w->{"kind"} eq "radio" and $w->{"group"} eq $key) 2 6 8
Condition66%2/3
$w->{"kind"} eq "radio" and $w->{"group"} eq $key
!ll&&!rl&&r
602
MC/DC50%1/2
$w->{"kind"} eq "radio" and $w->{"group"} eq $key
$w->{"kind"} eq "radio" $w->{"group"} eq $key
Truth table66%2/3
A and B
A= $w->{"kind"} eq "radio"B= $w->{"group"} eq $key
ABresult
0X0
100
111
377 2 ▶ $w->{selected} = ($w->{label} eq $v) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$w->{'label'} eq $v ? : 1 1 2
378 }
379 }
380 1 $self->{form}{$key} = $val;
381 1 return $self;
382 }
383  
384 # Return the (live) value hash.
385 23 ▶sub form { return $_[0]->{form}; }
Subroutine form: called CC=1 SCAR=0.0
Pod documented
386  
387 # Ask the run loop to finish (may be called from a button handler).
388 2 ▶sub close_form { $_[0]->{done} = 1; return; }
Subroutine close_form: called CC=1 SCAR=0.0
Pod documented
389  
390 # Which button closed the form (undef if closed some other way).
391 20 ▶sub pressed { return $_[0]->{pressed}; }
Subroutine pressed: called CC=1 SCAR=0.0
Pod documented
392  
393 #--------------------------------------------------------------------------
394 # Value editing (shared by both drivers)
395 #--------------------------------------------------------------------------
396  
397 # True if $char is acceptable input for text widget $w.
398 91 ▶sub _accept {
Subroutine _accept: called CC=6 SCAR=19.9
399 91 my ($w, $char) = @_;
400 91 my $t = $w->{subtype};
401 91 ▶ if ($t eq 'hankaku') {
Branch: 2 branches
BranchTrueFalseTotal
if ($t eq 'hankaku') { } 86 5 91
elsif ($t eq 'zenkaku') { } 0 5 5
402 86 ▶ return (length($char) == 1 && ord($char) >= 0x20 && ord($char) < 0x7f);
Condition33%2/6
length $char == 1 && ord $char >= 32 && ord $char < 127
!ll&&!rl&&r
0086
length $char == 1 and ord $char >= 32
!ll&&!rl&&r
0086
MC/DC0%0/3
length $char == 1 && ord $char >= 32 && ord $char < 127
length $char == 1 ord $char >= 32 ord $char < 127
Truth table25%1/4
A and B && C
A= length $char == 1B= ord $char >= 32C= ord $char < 127
ABCresult
0XX0
10X0
1100
1111
403 }
404 elsif ($t eq 'zenkaku') {
405 0 return (ord($char) >= 0x80); # any multibyte character
406 }
407 else { # num / cur / date
408 5 ▶ return ($char =~ /^[0-9]$/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$char =~ /^[0-9]$/ ? : 3 2 5
409 }
410 }
411  
412 # Replace the whole value of a text widget from a string (used by set() and
413 # by the line-mode driver).
414 18 ▶sub _set_text {
Subroutine _set_text: called CC=4 SCAR=13.9
415 18 my ($self, $w, $str) = @_;
416 18 my @c;
417 18 for my $ch (_chars($str)) {
418 85 ▶ last if @c >= $w->{size};
Branch: 1 branch
BranchTrueFalseTotal
if @c >= $w->{"size"} 1 84 85
419 84 ▶ push @c, $ch if _accept($w, $ch);
Branch: 1 branch
BranchTrueFalseTotal
if _accept($w, $ch) 82 2 84
420 }
421 18 $w->{chars} = [ @c ];
422 18 $w->{caret} = scalar(@c);
423 18 $self->{form}{$w->{key}} = join('', @c);
424 }
425  
426 # Insert one character at the caret.
427 7 ▶sub _insert {
Subroutine _insert: called CC=3 SCAR=11.1
428 7 my ($self, $w, $char) = @_;
429 7 ▶ return unless _accept($w, $char);
Branch: 1 branch
BranchTrueFalseTotal
unless _accept($w, $char) 0 7 7
430 7 ▶ return if @{$w->{chars}} >= $w->{size};
Branch: 1 branch
BranchTrueFalseTotal
if @{$w->{"chars"};} >= $w->{"size"} 0 7 7
431 7 splice(@{$w->{chars}}, $w->{caret}, 0, $char);
432 7 $w->{caret}++;
433 7 $self->{form}{$w->{key}} = join('', @{$w->{chars}});
434 }
435  
436 # Delete the character before the caret (Backspace).
437 2 ▶sub _backspace {
Subroutine _backspace: called CC=2 SCAR=7.0
438 2 my ($self, $w) = @_;
439 2 ▶ return if $w->{caret} <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $w->{"caret"} <= 0 0 2 2
440 2 splice(@{$w->{chars}}, $w->{caret} - 1, 1);
441 2 $w->{caret}--;
442 2 $self->{form}{$w->{key}} = join('', @{$w->{chars}});
443 }
444  
445 # Toggle a checkbox.
446 2 ▶sub _toggle {
Subroutine _toggle: called CC=2 SCAR=6.9
447 2 my ($self, $w) = @_;
448 2 ▶ $w->{value} = $w->{value} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$w->{'value'} ? : 1 1 2
449 2 $self->{form}{$w->{label}} = $w->{value};
450 }
451  
452 # Select a radio button (and clear its group siblings).
453 8 ▶sub _select_radio {
Subroutine _select_radio: called CC=5 SCAR=16.1
454 8 my ($self, $w) = @_;
455 8 for my $o (@{$self->{widgets}}) {
456 75 ▶ next unless $o->{kind} eq 'radio' && $o->{group} eq $w->{group};
Branch: 1 branch
BranchTrueFalseTotal
unless $o->{"kind"} eq "radio" and $o->{"group"} eq $w->{"group"} 59 16 75
Condition66%2/3
$o->{"kind"} eq "radio" and $o->{"group"} eq $w->{"group"}
!ll&&!rl&&r
59016
MC/DC50%1/2
$o->{"kind"} eq "radio" and $o->{"group"} eq $w->{"group"}
$o->{"kind"} eq "radio" $o->{"group"} eq $w->{"group"}
Truth table66%2/3
A and B
A= $o->{"kind"} eq "radio"B= $o->{"group"} eq $w->{"group"}
ABresult
0X0
100
111
457 16 ▶ $o->{selected} = ($o == $w) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$o == $w ? : 8 8 16
458 }
459 8 $self->{form}{$w->{group}} = $w->{label};
460 }
461  
462 # Fire a button handler. Returns true if the form should close.
463 18 ▶sub _press {
Subroutine _press: called CC=5 SCAR=16.1
464 18 my ($self, $w) = @_;
465 18 my $h = $self->{form}{$w->{label}};
466 18 ▶ if (ref($h) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $h eq "CODE") 16 2 18
467 16 my $r = $h->($self->{form}, $self);
468 16 ▶ my $closing = ($r || $self->{done}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$r || $self->{'done'} ? : 9 7 16
Condition100%3/3
$r or $self->{"done"}
l!l&&r!l&&!r
727
MC/DC100%2/2
$r or $self->{"done"}
$r $self->{"done"}
Truth table100%3/3
A or B
A= $rB= $self->{"done"}
ABresult
1X1
011
000
469 # The press is recorded only when it actually closes the form. A
470 # handler that returns false is saying "stay open", and leaving
471 # pressed() set from that attempt would make a later abort look like
472 # a successful button press to the caller.
473 16 ▶ $self->{pressed} = $closing ? $w->{label} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$closing ? : 9 7 16
474 16 return $closing;
475 }
476 # No handler registered: treat the press as "submit and close".
477 2 $self->{pressed} = $w->{label};
478 2 return 1;
479 }
480  
481 #--------------------------------------------------------------------------
482 # Rendering (ANSI driver)
483 #
484 # _render_frame() returns the whole screen as one string of bytes including
485 # escape sequences. Keeping it a pure function makes the layout testable
486 # without a real terminal.
487 #--------------------------------------------------------------------------
488 2 ▶sub _render_frame {
Subroutine _render_frame: called CC=18 SCAR=29.5
489 2 my $self = shift;
490 # -1 when nothing is focusable (a form of nothing but headings), so that
491 # the comparison below is still a comparison of two numbers.
492 2 ▶ my $focus_idx = @{$self->{focus}} ? $self->{focus}[$self->{fi}] : -1;
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'focus'};} ? : 1 1 2
493 2 my $out = $E . "[H"; # cursor home
494 2 my $cursor = ''; # trailing cursor placement, if any
495  
496 2 my $i = 0;
497 2 for my $w (@{$self->{widgets}}) {
498 7 ▶ my $focused = ($i == $focus_idx) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$i == $focus_idx ? : 1 6 7
499 7 my $line;
500  
501 7 ▶ if ($w->{kind} eq 'label') {
Branch: 5 branches
BranchTrueFalseTotal
if ($w->{'kind'} eq 'label') { } 3 4 7
elsif ($w->{'kind'} eq 'text') { } 1 3 4
elsif ($w->{'kind'} eq 'check') { } 1 2 3
elsif ($w->{'kind'} eq 'radio') { } 1 1 2
elsif ($w->{'kind'} eq 'button') { } 1 0 1
502 3 ▶ $line = defined($w->{text}) ? $w->{text} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $w->{'text'} ? : 3 0 3
503 }
504 elsif ($w->{kind} eq 'text') {
505 1 my $body = $self->_field_body($w);
506 1 ▶ my $inner = $focused ? ($E."[7m".$body.$E."[0m") : $body;
Branch: 1 branch
BranchTrueFalseTotal
$focused ? : 1 0 1
507 1 $line = $w->{pre} . $inner . $w->{post};
508 1 ▶ if ($focused) {
Branch: 1 branch
BranchTrueFalseTotal
if ($focused) 1 0 1
509 1 $cursor = $E . "[" . $w->{row} . ";" . $self->_caret_col($w) . "H";
510 }
511 }
512 elsif ($w->{kind} eq 'check') {
513 1 ▶ my $mark = $w->{value} ? 'X' : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$w->{'value'} ? : 1 0 1
514 1 $line = $w->{pre} . $mark . $w->{post};
515 1 ▶ $line = $E."[7m".$line.$E."[0m" if $focused;
Branch: 1 branch
BranchTrueFalseTotal
if $focused 0 1 1
516 }
517 elsif ($w->{kind} eq 'radio') {
518 1 ▶ my $mark = $w->{selected} ? '*' : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$w->{'selected'} ? : 1 0 1
519 1 $line = $w->{pre} . $mark . $w->{post};
520 1 ▶ $line = $E."[7m".$line.$E."[0m" if $focused;
Branch: 1 branch
BranchTrueFalseTotal
if $focused 0 1 1
521 }
522 elsif ($w->{kind} eq 'button') {
523 1 $line = $w->{pre} . $w->{label} . $w->{post};
524 1 ▶ $line = $E."[7m".$line.$E."[0m" if $focused;
Branch: 1 branch
BranchTrueFalseTotal
if $focused 0 1 1
525 }
526 else {
527 0 $line = '';
528 }
529  
530 7 $out .= $line . $E . "[K\r\n"; # clear rest of line, then newline
531 7 $i++;
532 }
533  
534 2 $out .= $E . "[J"; # clear anything below the form
535  
536 # Show the cursor only inside a text box; hide it otherwise.
537 2 ▶ if ($cursor ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cursor ne '') { } 1 1 2
538 1 $out .= $cursor . $E . "[?25h";
539 }
540 else {
541 1 $out .= $E . "[?25l";
542 }
543 2 return $out;
544 }
545  
546 # Build the padded interior of a text box (exactly iw columns wide).
547 2 ▶sub _field_body {
Subroutine _field_body: called CC=3 SCAR=11.1
548 2 my ($self, $w) = @_;
549 2 my $val = join('', @{$w->{chars}});
550 2 my $vw = _width($val);
551 2 my $pad = $w->{iw} - $vw;
552 2 ▶ $pad = 0 if $pad < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pad < 0 0 2 2
553 2 my $sp = ' ' x $pad;
554 2 ▶ return ($w->{just} eq 'R') ? ($sp . $val) : ($val . $sp);
Branch: 1 branch
BranchTrueFalseTotal
$w->{'just'} eq 'R' ? : 0 2 2
555 }
556  
557 # Terminal column (1-based) where the edit caret should sit.
558 1 ▶sub _caret_col {
Subroutine _caret_col: called CC=4 SCAR=14.1
559 1 my ($self, $w) = @_;
560 1 my $base = $w->{col} + 1; # column just after the '['
561 1 ▶ if ($w->{just} eq 'R') {
Branch: 1 branch
BranchTrueFalseTotal
if ($w->{"just"} eq "R") 0 1 1
562 0 return $base + $w->{iw} - 1; # right-justified: sit at the last cell
563 }
564 1 my @c = @{$w->{chars}};
565 1 my $off = 0;
566 1 ▶ for (my $k = 0; $k < $w->{caret} && $k < @c; $k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $w->{"caret"} and $k < @c) 4 1 5
Condition66%2/3
$k < $w->{"caret"} and $k < @c
!ll&&!rl&&r
104
MC/DC50%1/2
$k < $w->{"caret"} and $k < @c
$k < $w->{"caret"} $k < @c
Truth table66%2/3
A and B
A= $k < $w->{"caret"}B= $k < @c
ABresult
0X0
100
111
567 4 $off += _width($c[$k]);
568 }
569 1 return $base + $off;
570 }
571  
572 #--------------------------------------------------------------------------
573 # Focus movement and event handling (shared logic, no I/O)
574 #--------------------------------------------------------------------------
575 11 ▶sub _focus_next { my $s = shift; $s->{fi} = ($s->{fi} + 1) % scalar(@{$s->{focus}}) if @{$s->{focus}}; }
Subroutine _focus_next: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if @{$s->{"focus"};} 11 0 11
576 1 ▶sub _focus_prev { my $s = shift; $s->{fi} = ($s->{fi} - 1) % scalar(@{$s->{focus}}) if @{$s->{focus}}; }
Subroutine _focus_prev: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if @{$s->{"focus"};} 1 0 1
577  
578 31 ▶sub _cur_widget {
Subroutine _cur_widget: called CC=2 SCAR=6.9
579 31 my $self = shift;
580 31 ▶ return undef unless @{$self->{focus}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"focus"};} 4 27 31
581 27 return $self->{widgets}[ $self->{focus}[$self->{fi}] ];
582 }
583  
584 # Handle one event. $ev is either ['key', NAME] or ['char', BYTES].
585 35 ▶sub _handle_event {
Subroutine _handle_event: called CC=40 SCAR=55.0
586 35 my ($self, $ev) = @_;
587 35 my ($type, $data) = @$ev;
588  
589 # ESC is answered before anything else, because it is the way out and a
590 # form has to have one even when it holds nothing to focus. _read_key()
591 # also reports end of input as ESC, so this is what stops the run loop
592 # when STDIN closes.
593 35 ▶ if ($type eq 'key' && $data eq 'ESC') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "key" and $data eq "ESC") 4 31 35
Condition100%3/3
$type eq "key" and $data eq "ESC"
!ll&&!rl&&r
14174
MC/DC100%2/2
$type eq "key" and $data eq "ESC"
$type eq "key" $data eq "ESC"
Truth table100%3/3
A and B
A= $type eq "key"B= $data eq "ESC"
ABresult
0X0
100
111
594 4 $self->{aborted} = 1;
595 4 $self->{done} = 1;
596 4 return;
597 }
598  
599 31 my $w = $self->_cur_widget;
600 31 ▶ return unless $w;
Branch: 1 branch
BranchTrueFalseTotal
unless $w 4 27 31
601  
602 27 ▶ if ($type eq 'key') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "key") 15 12 27
603 15 ▶ if ($data eq 'TAB' || $data eq 'DOWN') { $self->_focus_next; return; }
Branch: 2 branches
BranchTrueFalseTotal
if ($data eq 'TAB' or $data eq 'DOWN') { } 11 4 15
elsif ($data eq 'STAB' or $data eq 'UP') { } 1 3 4
Condition66%2/3
$data eq "TAB" or $data eq "DOWN"
l!l&&r!l&&!r
1104
MC/DC50%1/2
$data eq "TAB" or $data eq "DOWN"
$data eq "TAB" $data eq "DOWN"
Truth table66%2/3
A or B
A= $data eq "TAB"B= $data eq "DOWN"
ABresult
1X1
011
000
604 1 ▶ elsif ($data eq 'STAB' || $data eq 'UP') { $self->_focus_prev; return; }
Condition66%2/3
$data eq "STAB" or $data eq "UP"
l!l&&r!l&&!r
103
MC/DC50%1/2
$data eq "STAB" or $data eq "UP"
$data eq "STAB" $data eq "UP"
Truth table66%2/3
A or B
A= $data eq "STAB"B= $data eq "UP"
ABresult
1X1
011
000
605  
606 3 ▶ if ($w->{kind} eq 'text') {
Branch: 4 branches
BranchTrueFalseTotal
if ($w->{'kind'} eq 'text') { } 1 2 3
elsif ($w->{'kind'} eq 'check') { } 0 2 2
elsif ($w->{'kind'} eq 'radio') { } 0 2 2
elsif ($w->{'kind'} eq 'button') { } 2 0 2
607 0 ▶ if ($data eq 'ENTER') { $self->_focus_next; }
Branch: 6 branches
BranchTrueFalseTotal
if ($data eq 'ENTER') { } 0 1 1
elsif ($data eq 'BS') { } 1 0 1
elsif ($data eq 'LEFT') { } 0 0 0
elsif ($data eq 'RIGHT') { } 0 0 0
elsif ($data eq 'HOME') { } 0 0 0
elsif ($data eq 'END') { } 0 0 0
608 1 elsif ($data eq 'BS') { $self->_backspace($w); }
609 elsif ($data eq 'LEFT') {
610 0 ▶ if ($w->{just} eq 'R') { $self->_focus_prev; }
Branch: 1 branch
BranchTrueFalseTotal
if ($w->{'just'} eq 'R') { } 0 0 0
611 0 ▶ else { $w->{caret}-- if $w->{caret} > 0; }
Branch: 1 branch
BranchTrueFalseTotal
if $w->{"caret"} > 0 0 0 0
612 }
613 elsif ($data eq 'RIGHT') {
614 0 ▶ if ($w->{just} eq 'R') { $self->_focus_next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($w->{'just'} eq 'R') { } 0 0 0
615 0 ▶ else { $w->{caret}++ if $w->{caret} < @{$w->{chars}}; }
Branch: 1 branch
BranchTrueFalseTotal
if $w->{"caret"} < @{$w->{"chars"};} 0 0 0
616 }
617 0 elsif ($data eq 'HOME') { $w->{caret} = 0; }
618 0 elsif ($data eq 'END') { $w->{caret} = scalar(@{$w->{chars}}); }
619 }
620 elsif ($w->{kind} eq 'check') {
621 0 ▶ if ($data eq 'ENTER') { $self->_toggle($w); }
Branch: 3 branches
BranchTrueFalseTotal
if ($data eq 'ENTER') { } 0 0 0
elsif ($data eq 'LEFT') { } 0 0 0
elsif ($data eq 'RIGHT') { } 0 0 0
622 0 elsif ($data eq 'LEFT') { $self->_focus_prev; }
623 0 elsif ($data eq 'RIGHT') { $self->_focus_next; }
624 }
625 elsif ($w->{kind} eq 'radio') {
626 0 ▶ if ($data eq 'ENTER') { $self->_select_radio($w); }
Branch: 3 branches
BranchTrueFalseTotal
if ($data eq 'ENTER') { } 0 0 0
elsif ($data eq 'LEFT') { } 0 0 0
elsif ($data eq 'RIGHT') { } 0 0 0
627 0 elsif ($data eq 'LEFT') { $self->_focus_prev; }
628 0 elsif ($data eq 'RIGHT') { $self->_focus_next; }
629 }
630 elsif ($w->{kind} eq 'button') {
631 2 ▶ if ($data eq 'ENTER') { $self->{done} = 1 if $self->_press($w); }
Branch: 4 branches
BranchTrueFalseTotal
if ($data eq 'ENTER') { } 2 0 2
elsif ($data eq 'LEFT') { } 0 0 0
elsif ($data eq 'RIGHT') { } 0 0 0
if $self->_press($w) 2 0 2
632 0 elsif ($data eq 'LEFT') { $self->_focus_prev; }
633 0 elsif ($data eq 'RIGHT') { $self->_focus_next; }
634 }
635 3 return;
636 }
637  
638 # $type eq 'char'
639 12 ▶ if ($data eq ' ') { # Space acts as toggle/select/press off text.
Branch: 1 branch
BranchTrueFalseTotal
if ($data eq " ") 7 5 12
640 0 ▶ if ($w->{kind} eq 'text') { $self->_insert($w, ' '); }
Branch: 4 branches
BranchTrueFalseTotal
if ($w->{'kind'} eq 'text') { } 0 7 7
elsif ($w->{'kind'} eq 'check') { } 1 6 7
elsif ($w->{'kind'} eq 'radio') { } 1 5 6
elsif ($w->{'kind'} eq 'button') { } 5 0 5
641 1 elsif ($w->{kind} eq 'check') { $self->_toggle($w); }
642 1 elsif ($w->{kind} eq 'radio') { $self->_select_radio($w); }
643 5 ▶ elsif ($w->{kind} eq 'button') { $self->{done} = 1 if $self->_press($w); }
Branch: 1 branch
BranchTrueFalseTotal
if $self->_press($w) 2 3 5
644 7 return;
645 }
646 5 ▶ if ($w->{kind} eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($w->{"kind"} eq "text") 5 0 5
647 5 $self->_insert($w, $data);
648 }
649 }
650  
651 #--------------------------------------------------------------------------
652 # Keyboard input (ANSI driver, cbreak mode)
653 #--------------------------------------------------------------------------
654  
655 # Try to put the terminal into cbreak mode. Returns true on success. Signal
656 # handling (Ctrl-C) is left enabled on purpose.
657 0 ▶sub _raw_on {
Subroutine _raw_on: not called CC=5 SCAR=34.0
658 0 ▶ return 0 if $^O =~ /MSWin32/i; # bare Windows has no stty
Branch: 1 branch
BranchTrueFalseTotal
if $^O =~ /MSWin32/i 0 0 0
659 0 my $saved = `stty -g 2>/dev/null`;
660 0 ▶ return 0 unless defined $saved;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $saved 0 0 0
661 0 chomp $saved;
662 0 ▶ return 0 if $saved eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $saved eq "" 0 0 0
663 0 $SAVED_STTY = $saved;
664 0 system("stty -icanon -echo min 1 time 0 2>/dev/null");
665 0 $RAW_ON = 1;
666  
667 # A signal that is not trapped kills the process outright, without running
668 # END blocks, so the clean-up cannot be delegated to one: the terminal
669 # would be left in cbreak mode with the cursor hidden, and the user would
670 # have to type a blind "stty sane" to get it back.
671 0 $SAVED_SIG = {};
672 0 for my $sig ('INT', 'TERM', 'HUP') {
673 0 $SAVED_SIG->{$sig} = $SIG{$sig};
674 0 $SIG{$sig} = \&_signal_exit;
675 }
676 0 return 1;
677 }
678  
679 0 ▶sub _raw_off {
Subroutine _raw_off: not called CC=5 SCAR=34.0
680 0 ▶ return unless $RAW_ON;
Branch: 1 branch
BranchTrueFalseTotal
unless $TUI::Handy::RAW_ON 0 0 0
681 0 ▶ system("stty $SAVED_STTY 2>/dev/null") if $SAVED_STTY ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $TUI::Handy::SAVED_STTY ne "" 0 0 0
682 0 for my $sig (sort keys %$SAVED_SIG) {
683 0 ▶ if (defined $SAVED_SIG->{$sig}) { $SIG{$sig} = $SAVED_SIG->{$sig}; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $TUI::Handy::SAVED_SIG->{$sig}) { } 0 0 0
684 0 else { $SIG{$sig} = 'DEFAULT'; }
685 }
686 0 $SAVED_SIG = {};
687 0 $RAW_ON = 0;
688 }
689  
690 # Signal handler installed while the terminal is in cbreak mode. It puts the
691 # terminal back, then lets the signal do what it was going to do, so the exit
692 # status still says the process was killed rather than that it ended normally.
693 0 ▶sub _signal_exit {
Subroutine _signal_exit: not called CC=1 SCAR=6.9
694 0 my $sig = shift;
695 0 _raw_off();
696 0 print STDOUT $E . "[?25h" . $E . "[0m\r\n";
697 0 $SIG{$sig} = 'DEFAULT';
698 0 kill($sig, $$);
699 0 exit(1); # only reached if the signal is blocked or ignored
700 }
701  
702 # Last resort: a die outside run(), or an exit() from a button handler, would
703 # otherwise leave the terminal in cbreak mode. A no-op once _raw_off() has
704 # already run, which is the normal case.
705 END {
706 if ($RAW_ON) {
707 print STDOUT $E . "[?25h" . $E . "[0m\r\n";
708 _raw_off();
709 }
710 }
711  
712 # Read exactly one byte from STDIN (blocking). Returns undef at end of file.
713 0 ▶sub _getbyte {
Subroutine _getbyte: not called CC=3 SCAR=24.8
714 0 my $b;
715 0 my $n = sysread(STDIN, $b, 1);
716 0 ▶ return (defined $n && $n > 0) ? $b : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $n && $n > 0 ? : 0 0 0
Condition0%0/3
defined $n and $n > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $n and $n > 0
defined $n $n > 0
Truth table0%0/3
A and B
A= defined $nB= $n > 0
ABresult
0X0
100
111
717 }
718  
719 # Read one byte but give up after a short timeout. Used to tell a lone ESC
720 # apart from an escape sequence such as an arrow key.
721 0 ▶sub _getbyte_timeout {
Subroutine _getbyte_timeout: not called CC=2 SCAR=17.9
722 0 my $rin = '';
723 0 vec($rin, fileno(STDIN), 1) = 1;
724 0 my $ready = select($rin, undef, undef, 0.06);
725 0 ▶ return undef if $ready <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ready <= 0 0 0 0
726 0 return _getbyte();
727 }
728  
729 # Read one key and return it as ['key', NAME] or ['char', BYTES].
730 0 ▶sub _read_key {
Subroutine _read_key: not called CC=26 SCAR=65.5
731 0 my $self = shift;
732 0 my $c = _getbyte();
733 0 ▶ return ['key', 'ESC'] unless defined $c; # EOF -> treat as abort
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 0 0
734 0 my $o = ord $c;
735  
736 0 ▶ if ($o == 0x1b) { # ESC or escape sequence
Branch: 1 branch
BranchTrueFalseTotal
if ($o == 27) 0 0 0
737 0 my $n = _getbyte_timeout();
738 0 ▶ return ['key', 'ESC'] unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 0 0
739 0 ▶ if ($n eq '[' || $n eq 'O') {
Branch: 1 branch
BranchTrueFalseTotal
if ($n eq "[" or $n eq "O") 0 0 0
Condition0%0/3
$n eq "[" or $n eq "O"
l!l&&r!l&&!r
000
MC/DC0%0/2
$n eq "[" or $n eq "O"
$n eq "[" $n eq "O"
Truth table0%0/3
A or B
A= $n eq "["B= $n eq "O"
ABresult
1X1
011
000
740 0 my $m = _getbyte();
741 0 ▶ return ['key', 'ESC'] unless defined $m;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m 0 0 0
742 0 ▶ return ['key', 'UP'] if $m eq 'A';
Branch: 1 branch
BranchTrueFalseTotal
if $m eq "A" 0 0 0
743 0 ▶ return ['key', 'DOWN'] if $m eq 'B';
Branch: 1 branch
BranchTrueFalseTotal
if $m eq "B" 0 0 0
744 0 ▶ return ['key', 'RIGHT'] if $m eq 'C';
Branch: 1 branch
BranchTrueFalseTotal
if $m eq "C" 0 0 0
745 0 ▶ return ['key', 'LEFT'] if $m eq 'D';
Branch: 1 branch
BranchTrueFalseTotal
if $m eq "D" 0 0 0
746 0 ▶ return ['key', 'HOME'] if $m eq 'H';
Branch: 1 branch
BranchTrueFalseTotal
if $m eq "H" 0 0 0
747 0 ▶ return ['key', 'END'] if $m eq 'F';
Branch: 1 branch
BranchTrueFalseTotal
if $m eq "F" 0 0 0
748 0 ▶ return ['key', 'STAB'] if $m eq 'Z'; # Shift-Tab
Branch: 1 branch
BranchTrueFalseTotal
if $m eq "Z" 0 0 0
749 0 ▶ if ($m =~ /[0-9]/) { # e.g. ESC [ 3 ~
Branch: 1 branch
BranchTrueFalseTotal
if ($m =~ /[0-9]/) 0 0 0
750 0 my $x;
751 0 ▶ do { $x = _getbyte(); } while (defined $x && $x !~ /[~A-Za-z]/);
Branch: 1 branch
BranchTrueFalseTotal
defined $x and not $x =~ /[~A-Za-z]/ and do { do { $x = _getbyte() } } 0 0 0
Condition0%0/3
defined $x and not $x =~ /[~A-Za-z]/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $x and not $x =~ /[~A-Za-z]/
defined $x not $x =~ /[~A-Za-z]/
Truth table0%0/3
A and not(B)
A= defined $xB= not $x =~ /[~A-Za-z]/
ABresult
0X0
100
111
752 }
753 0 return ['key', 'IGN'];
754 }
755 0 return ['key', 'ESC'];
756 }
757  
758 0 ▶ return ['key', 'TAB'] if $o == 0x09;
Branch: 1 branch
BranchTrueFalseTotal
if $o == 9 0 0 0
759 0 ▶ return ['key', 'ENTER'] if $o == 0x0d || $o == 0x0a;
Branch: 1 branch
BranchTrueFalseTotal
if $o == 13 or $o == 10 0 0 0
Condition0%0/3
$o == 13 or $o == 10
l!l&&r!l&&!r
000
MC/DC0%0/2
$o == 13 or $o == 10
$o == 13 $o == 10
Truth table0%0/3
A or B
A= $o == 13B= $o == 10
ABresult
1X1
011
000
760 0 ▶ return ['key', 'BS'] if $o == 0x7f || $o == 0x08;
Branch: 1 branch
BranchTrueFalseTotal
if $o == 127 or $o == 8 0 0 0
Condition0%0/3
$o == 127 or $o == 8
l!l&&r!l&&!r
000
MC/DC0%0/2
$o == 127 or $o == 8
$o == 127 $o == 8
Truth table0%0/3
A or B
A= $o == 127B= $o == 8
ABresult
1X1
011
000
761 0 ▶ return ['key', 'IGN'] if $o < 0x20;
Branch: 1 branch
BranchTrueFalseTotal
if $o < 32 0 0 0
762  
763 0 ▶ if ($o >= 0x80) { # multibyte lead byte
Branch: 1 branch
BranchTrueFalseTotal
if ($o >= 128) 0 0 0
764 0 my ($bl) = _char_len($o, $ENCODING);
765 0 my $rest = '';
766 0 ▶ for (my $k = 1; $k < $bl; $k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $bl) 0 0 0
767 0 my $b = _getbyte();
768 0 ▶ last unless defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 0 0 0
769 0 $rest .= $b;
770 }
771 0 return ['char', $c . $rest];
772 }
773 0 return ['char', $c]; # ordinary printable ASCII
774 }
775  
776 #--------------------------------------------------------------------------
777 # Run
778 #--------------------------------------------------------------------------
779  
780 # Choose a driver and run the form. Returns the value hash reference.
781 6 ▶sub run {
Subroutine run: called CC=6 SCAR=28.6
Pod documented
782 6 my $self = shift;
783 6 ▶ my $mode = defined $ENV{'TUI_HANDY_MODE'} ? $ENV{'TUI_HANDY_MODE'} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'TUI_HANDY_MODE'} ? : 6 0 6
784 6 ▶ if ($mode eq 'line') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "line") 6 0 6
785 6 return $self->_run_line;
786 }
787 0 ▶ if ($mode ne 'ansi' && !_raw_on()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode ne "ansi" and not _raw_on()) 0 0 0
Condition0%0/3
$mode ne "ansi" and not _raw_on()
!ll&&!rl&&r
000
MC/DC0%0/2
$mode ne "ansi" and not _raw_on()
$mode ne "ansi" not _raw_on()
Truth table0%0/3
A and not(B)
A= $mode ne "ansi"B= not _raw_on()
ABresult
0X0
100
111
788 # Could not obtain a raw terminal - fall back to portable line mode.
789 0 return $self->_run_line;
790 }
791 0 ▶ _raw_on() if $mode eq 'ansi';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "ansi" 0 0 0
792 0 return $self->_run_ansi;
793 }
794  
795 0 ▶sub _run_ansi {
Subroutine _run_ansi: not called CC=3 SCAR=24.8
796 0 my $self = shift;
797 0 local $| = 1;
798 0 binmode(STDOUT);
799 0 binmode(STDIN);
800 0 print STDOUT $E . "[2J"; # clear once at the start
801  
802 # Guard so the terminal is always restored, even on die.
803 0 eval {
804 0 ▶ while (!$self->{done}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"done"}) 0 0 0
805 0 print STDOUT $self->_render_frame;
806 0 my $ev = $self->_read_key;
807 0 $self->_handle_event($ev);
808 }
809 };
810 0 my $err = $@;
811  
812 0 print STDOUT $E . "[?25h"; # show cursor
813 0 my $rows = scalar(@{$self->{widgets}});
814 0 print STDOUT $E . "[" . ($rows + 1) . ";1H" . $E . "[0m\r\n";
815 0 _raw_off();
816 0 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
817 0 return $self->{form};
818 }
819  
820 #--------------------------------------------------------------------------
821 # Portable line-oriented driver
822 #
823 # Used when no raw terminal is available (typically bare Windows cmd.exe).
824 # It walks the widgets top to bottom asking for one value per line, so it
825 # needs neither ANSI support nor single-key input.
826 #--------------------------------------------------------------------------
827 6 ▶sub _run_line {
Subroutine _run_line: called CC=6 SCAR=17.9
828 6 my $self = shift;
829 6 local $| = 1;
830  
831 # The button list does not change between passes, so it is collected
832 # once rather than rebuilt on every walk of the widgets.
833 6 my @buttons = grep { $_->{kind} eq 'button' } @{$self->{widgets}};
834  
835 6 my $pass = 0;
836 6 ▶ while (!$self->{done}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"done"}) 9 0 9
837 9 $pass++;
838 9 ▶ print "\n" if $pass > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $pass > 1 3 6 9
839 9 $self->_line_ask_fields;
840  
841 9 ▶ last unless @buttons;
Branch: 1 branch
BranchTrueFalseTotal
unless @buttons 0 9 9
842  
843 9 my $w = $self->_line_ask_button(@buttons);
844 9 ▶ unless (defined $w) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $w) 1 8 9
845 # End of input. The full-screen driver reaches the same state
846 # through ESC, so it is recorded the same way: aborted, with
847 # pressed() left undefined.
848 1 $self->{aborted} = 1;
849 1 last;
850 }
851 8 ▶ last if $self->_press($w);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_press($w) 5 3 8
852  
853 # _press() returned false: the handler wants the form to stay open,
854 # which is the contract the full-screen driver honours. The fields
855 # are walked again, showing everything already entered as the
856 # defaults, so the user retypes only what needs changing.
857 }
858 6 return $self->{form};
859 }
860  
861 # One walk over the widgets, asking for one value per line. Values already
862 # held are offered as defaults, so an empty answer keeps them; that is what
863 # makes a second pass cheap for the user.
864 9 ▶sub _line_ask_fields {
Subroutine _line_ask_fields: called CC=20 SCAR=30.1
865 9 my $self = shift;
866  
867 9 my %group_done; # radio groups already asked about
868  
869 9 for my $w (@{$self->{widgets}}) {
870 90 my $k = $w->{kind};
871  
872 90 ▶ if ($k eq 'label') {
Branch: 4 branches
BranchTrueFalseTotal
if ($k eq 'label') { } 27 63 90
elsif ($k eq 'text') { } 18 45 63
elsif ($k eq 'check') { } 9 36 45
elsif ($k eq 'radio') { } 18 18 36
873 27 print $w->{text}, "\n";
874  
875 }
876 elsif ($k eq 'text') {
877 18 my $cur = join('', @{$w->{chars}});
878 18 print $w->{label}, " [", $cur, "]: ";
879 18 my $in = <STDIN>;
880 18 ▶ if (defined $in) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $in) 16 2 18
881 16 chomp $in; $in =~ s/\r$//;
882 16 ▶ $self->_set_text($w, $in) if $in ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $in ne "" 13 3 16
883 }
884  
885 }
886 elsif ($k eq 'check') {
887 9 ▶ my $cur = $w->{value} ? 'Y' : 'N';
Branch: 1 branch
BranchTrueFalseTotal
$w->{'value'} ? : 9 0 9
888 9 print "[", $w->{label}, "] (y/n) [", $cur, "]: ";
889 9 my $in = <STDIN>;
890 9 ▶ if (defined $in) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $in) 8 1 9
891 8 chomp $in; $in =~ s/\r$//;
892 8 ▶ if ($in =~ /^y/i) { $w->{value} = 1; }
Branch: 2 branches
BranchTrueFalseTotal
if ($in =~ /^y/i) { } 6 2 8
elsif ($in =~ /^n/i) { } 0 2 2
893 0 elsif ($in =~ /^n/i) { $w->{value} = 0; }
894 8 $self->{form}{$w->{label}} = $w->{value};
895 }
896  
897 }
898 elsif ($k eq 'radio') {
899 18 ▶ next if $group_done{$w->{group}}++;
Branch: 1 branch
BranchTrueFalseTotal
if $group_done{$w->{"group"}}++ 9 9 18
900 my @members = grep {
901 $_->{kind} eq 'radio' && $_->{group} eq $w->{group}
902 9 ▶ } @{$self->{widgets}};
Condition66%2/3
$_->{'kind'} eq 'radio' && $_->{'group'} eq $w->{'group'}
!ll&&!rl&&r
72018
MC/DC50%1/2
$_->{'kind'} eq 'radio' && $_->{'group'} eq $w->{'group'}
$_->{'kind'} eq 'radio' $_->{'group'} eq $w->{'group'}
Truth table66%2/3
A && B
A= $_->{'kind'} eq 'radio'B= $_->{'group'} eq $w->{'group'}
ABresult
0X0
100
111
903 9 my $n = 0;
904 9 for my $m (@members) {
905 18 $n++;
906 18 ▶ my $flag = $m->{selected} ? '*' : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$m->{'selected'} ? : 9 9 18
907 18 print " ", $n, ") (", $flag, ") ", $m->{label}, "\n";
908 }
909 9 print " choose 1-", $n, ": ";
910 9 my $in = <STDIN>;
911 9 ▶ if (defined $in && $in =~ /(\d+)/ && $1 >= 1 && $1 <= $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $in and $in =~ /(\d+)/ and $1 >= 1 and $1 <= $n) 6 3 9
Condition77%7/9
defined $in and $in =~ /(\d+)/ and $1 >= 1 and $1 <= $n
!ll&&!rl&&r
306
defined $in and $in =~ /(\d+)/ and $1 >= 1
!ll&&!rl&&r
306
defined $in and $in =~ /(\d+)/
!ll&&!rl&&r
126
MC/DC50%2/4
defined $in and $in =~ /(\d+)/ and $1 >= 1 and $1 <= $n
defined $in $in =~ /(\d+)/ $1 >= 1 $1 <= $n
Truth table60%3/5
A and B and C and D
A= defined $inB= $in =~ /(\d+)/C= $1 >= 1D= $1 <= $n
ABCDresult
0XXX0
10XX0
110X0
11100
11111
912 6 $self->_select_radio($members[$1 - 1]);
913 }
914 }
915 }
916 9 return;
917 }
918  
919 # Ask which button to press. Returns the chosen widget, or undef at end of
920 # input. A choice outside the range only re-prompts: a typo here must not
921 # cost the user another walk through every field.
922 9 ▶sub _line_ask_button {
Subroutine _line_ask_button: called CC=7 SCAR=19.5
923 9 my ($self, @buttons) = @_;
924  
925 9 my $n = scalar(@buttons);
926 9 ▶ return undef unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 9 9
927  
928 9 while (1) {
929 11 print "\n";
930 11 my $i = 0;
931 11 for my $b (@buttons) {
932 22 $i++;
933 22 print " ", $i, ") [", $b->{label}, "]\n";
934 }
935 11 print " press 1-", $n, ": ";
936 11 my $in = <STDIN>;
937 11 ▶ return undef unless defined $in;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $in 1 10 11
938 10 ▶ if ($in =~ /(\d+)/ && $1 >= 1 && $1 <= $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /(\d+)/ and $1 >= 1 and $1 <= $n) 8 2 10
Condition83%5/6
$in =~ /(\d+)/ and $1 >= 1 and $1 <= $n
!ll&&!rl&&r
118
$in =~ /(\d+)/ and $1 >= 1
!ll&&!rl&&r
019
MC/DC66%2/3
$in =~ /(\d+)/ and $1 >= 1 and $1 <= $n
$in =~ /(\d+)/ $1 >= 1 $1 <= $n
Truth table75%3/4
A and B and C
A= $in =~ /(\d+)/B= $1 >= 1C= $1 <= $n
ABCresult
0XX0
10X0
1100
1111
939 8 return $buttons[$1 - 1];
940 }
941 }
942 }
943  
944 #--------------------------------------------------------------------------
945 # Command-line entry point (modulino)
946 #--------------------------------------------------------------------------
947 0 ▶sub _main {
Subroutine _main: not called CC=7 SCAR=40.3
948 0 my @args = @_;
949 0 my $file = shift @args;
950 0 ▶ unless (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 0 0
951 0 print STDERR "usage: perl Handy.pm FORMFILE\n";
952 0 return 2;
953 }
954 0 my $dsl = eval { _slurp($file) };
955 0 ▶ if (!defined $dsl) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dsl) 0 0 0
956 0 print STDERR $@;
957 0 return 2;
958 }
959 0 my $tui = TUI::Handy->new(dsl => $dsl);
960 0 my $form = $tui->run;
961  
962 # Print the collected data as TAB-separated key/value lines. Button
963 # entries (code references or undef) are skipped.
964 0 for my $k (@{$tui->{order_keys}}) {
965 0 my $v = $form->{$k};
966 0 ▶ next if ref($v) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "CODE" 0 0 0
967 0 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
968 0 print $k, "\t", $v, "\n";
969 }
970 0 ▶ print "__pressed__\t", (defined $tui->pressed ? $tui->pressed : ''), "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $tui->pressed ? : 0 0 0
971 0 return 0;
972 }
973  
974 # Run as a command when this file is executed directly; stay quiet when it is
975 # loaded as a module.
976 7 ▶unless (caller()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller()) 0 7 7
977 0 exit(_main(@ARGV));
978 }
979  
980 7 1;
981  
982 __END__
↑ Summary