blib/lib/Term/Shell.pm

filter: Statementstmt 41.9% 227 / 541 Branchbran 26.1% 58 / 222 Conditioncond 16.0% 22 / 137 MC/DCmcdc 0.0% 0 / 81 Subroutinesub 44.5% 37 / 83 Podpod 3.3% 2 / 60 totaltotal 30.7% 346 / 1124 SCAR 91.2
CC
177
Cov
34%
line_parsed
156.0
termsize
154.7
page_internal
90.0
CRAP 9138.5
↑ Summary Build »
1 package Term::Shell;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Term::ReadLine ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our $VERSION = '0.10';
12  
13 #=============================================================================
14 # Term::Shell API methods
15 #=============================================================================
16 sub new
17 {
18 3 ▶ my $cls = shift;
Subroutine new: called CC=8 SCAR=24.6
Pod undocumented
19 my $o = bless {
20 3 ▶ term => eval {
Condition50%1/2
eval { do { local $SIG{'__WARN__'} = sub { } ; 'Term::ReadLine'->new('shell') } } || undef
l!l
30
MC/DC0%0/1
eval { do { local $SIG{'__WARN__'} = sub { } ; 'Term::ReadLine'->new('shell') } } || undef
eval { do { local $SIG{'__WARN__'} = sub { } ; 'Term::ReadLine'->new('shell') } }
Truth table50%1/2
A
A= eval { do { local $SIG{'__WARN__'} = sub { } ; 'Term::ReadLine'->new('shell') } }
Aresult
11
00
21  
22 # Term::ReadKey throws ugliness all over the place if we're not
23 # running in a terminal, which we aren't during "make test", at
24 # least on FreeBSD. Suppress warnings here.
25 3 ▶ local $SIG{__WARN__} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=0.0
26 3 ▶ Term::ReadLine->new('shell');
Condition33%1/3
ref $cls || $cls
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $cls || $cls
ref $cls $cls
Truth table33%1/3
A || B
A= ref $clsB= $cls
ABresult
1X1
011
000
27 }
28 || undef,
29 },
30 ref($cls)
31 || $cls;
32  
33 # Set up the API hash:
34 3 $o->{command} = {};
35 $o->{API} = {
36 args => \@_,
37 case_ignore => ( $^O eq 'MSWin32' ? 1 : 0 ),
38 check_idle => 0, # changing this isn't supported
39 class => $cls,
40 command => $o->{command},
41 cmd => $o->{command}, # shorthand
42 match_uniq => 1,
43 pager => $ENV{PAGER} || 'internal',
44 3 ▶ readline => eval { $o->{term}->ReadLine } || 'none',
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 3 3
Condition50%2/4
$ENV{'PAGER'} || 'internal'
l!l
03
eval { do { $o->{'term'}->ReadLine } } || 'none'
l!l
30
MC/DC0%0/1
$ENV{'PAGER'} || 'internal'
$ENV{'PAGER'}
MC/DC0%0/1
eval { do { $o->{'term'}->ReadLine } } || 'none'
eval { do { $o->{'term'}->ReadLine } }
Truth table50%1/2
A
A= $ENV{'PAGER'}
Aresult
11
00
Truth table50%1/2
A
A= eval { do { $o->{'term'}->ReadLine } }
Aresult
11
00
45 script => ( caller(0) )[1],
46 version => $VERSION,
47 };
48  
49 # Note: the rl_completion_function doesn't pass an object as the first
50 # argument, so we have to use a closure. This has the unfortunate effect
51 # of preventing two instances of Term::ReadLine from coexisting.
52 my $completion_handler = sub {
53 0 ▶ $o->rl_complete(@_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 3 };
55 3 ▶ if ( $o->{API}{readline} eq 'Term::ReadLine::Gnu' )
Branch: 2 branches
BranchTrueFalseTotal
if ($o->{'API'}{'readline'} eq 'Term::ReadLine::Gnu') { } 0 3 3
elsif ($o->{'API'}{'readline'} eq 'Term::ReadLine::Perl') { } 0 3 3
56 {
57 0 my $attribs = $o->{term}->Attribs;
58 0 $attribs->{completion_function} = $completion_handler;
59 }
60 elsif ( $o->{API}{readline} eq 'Term::ReadLine::Perl' )
61 {
62 0 $readline::rl_completion_function =
63 $readline::rl_completion_function = $completion_handler;
64 }
65 3 $o->find_handlers;
66 3 $o->init;
67 3 $o;
68 }
69  
70 sub DESTROY
71 {
72 3 ▶ my $o = shift;
Subroutine DESTROY: called CC=1 SCAR=0.0
73 3 $o->fini;
74 }
75  
76 sub cmd
77 {
78 0 ▶ my $o = shift;
Subroutine cmd: not called CC=5 SCAR=34.0
Pod undocumented
79 0 $o->{line} = shift;
80 0 ▶ if ( $o->line =~ /\S/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($o->line =~ /\S/u) { } 0 0 0
81 {
82 0 my ( $cmd, @args ) = $o->line_parsed;
83 0 $o->run( $cmd, @args );
84 0 ▶ unless ( $o->{command}{run}{found} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($o->{"command"}{"run"}{"found"}) 0 0 0
85 {
86 0 my @c = sort $o->possible_actions( $cmd, 'run' );
87 0 ▶ if ( @c and $o->{API}{match_uniq} )
Branch: 1 branch
BranchTrueFalseTotal
if (@c and $o->{'API'}{'match_uniq'}) { } 0 0 0
Condition0%0/3
@c and $o->{"API"}{"match_uniq"}
!ll&&!rl&&r
000
MC/DC0%0/2
@c and $o->{"API"}{"match_uniq"}
@c $o->{"API"}{"match_uniq"}
Truth table0%0/3
A and B
A= @cB= $o->{"API"}{"match_uniq"}
ABresult
0X0
100
111
88 {
89 0 print $o->msg_ambiguous_cmd( $cmd, @c );
90 }
91 else
92 {
93 0 print $o->msg_unknown_cmd($cmd);
94 }
95 }
96 }
97 else
98 {
99 0 $o->run('');
100 }
101 }
102  
103 0 ▶sub stoploop { $_[0]->{stop}++ }
Subroutine stoploop: not called CC=1 SCAR=6.9
Pod undocumented
104  
105 sub cmdloop
106 {
107 0 ▶ my $o = shift;
Subroutine cmdloop: not called CC=3 SCAR=24.8
Pod undocumented
108 0 $o->{stop} = 0;
109 0 $o->preloop;
110 0 ▶ while ( defined( my $line = $o->readline( $o->prompt_str ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $o->readline($o->prompt_str))) 0 0 0
111 {
112 0 $o->cmd($line);
113 0 ▶ last if $o->{stop};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"stop"} 0 0 0
114 }
115 0 $o->postloop;
116 }
117 4 *mainloop = \&cmdloop;
118  
119 sub readline
120 {
121 0 ▶ my $o = shift;
Subroutine readline: not called CC=3 SCAR=22.1
Pod documented
122 0 my $prompt = shift;
123 return $o->{term}->readline($prompt)
124 if $o->{API}{check_idle} == 0
125 0 ▶ or not defined $o->{term}->IN;
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"API"}{"check_idle"} == 0 or not defined $o->{"term"}->IN 0 0 0
Condition0%0/3
$o->{"API"}{"check_idle"} == 0 or not defined $o->{"term"}->IN
l!l&&r!l&&!r
000
MC/DC0%0/2
$o->{"API"}{"check_idle"} == 0 or not defined $o->{"term"}->IN
$o->{"API"}{"check_idle"} == 0 not defined $o->{"term"}->IN
Truth table0%0/3
A or not(B)
A= $o->{"API"}{"check_idle"} == 0B= not defined $o->{"term"}->IN
ABresult
1X1
011
000
126  
127 # They've asked for idle-time running of some user command.
128 0 local $Term::ReadLine::toloop = 1;
129 local *Tk::fileevent = sub {
130 0 ▶ my $cls = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 0 my ( $file, $boring, $callback ) = @_;
132 0 $o->{fh} = $file; # save the filehandle!
133 0 $o->{cb} = $callback; # save the callback!
134 0 };
135 local *Tk::DoOneEvent = sub {
136  
137 # We'll totally cheat and do a select() here -- the timeout will be
138 # $o->{API}{check_idle}; if the handle is ready, we'll call &$cb;
139 # otherwise we'll call $o->idle(), which can do some processing.
140 0 ▶ my $timeout = $o->{API}{check_idle};
Subroutine __ANON__: not called CC=2 SCAR=11.6
141 4 ▶ use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
142 0 ▶ if ( IO::Select->new( $o->{fh} )->can_read($timeout) )
Branch: 1 branch
BranchTrueFalseTotal
if ('IO::Select'->new($o->{'fh'})->can_read($timeout)) { } 0 0 0
143 {
144 # Input is ready: stop the event loop.
145 0 $o->{cb}->();
146 }
147 else
148 {
149 0 $o->idle;
150 }
151 0 };
152 0 $o->{term}->readline($prompt);
153 }
154  
155 0 ▶sub term { $_[0]->{term} }
Subroutine term: not called CC=1 SCAR=6.9
Pod undocumented
156  
157 # These are likely candidates for overriding in subclasses
158 ▶sub init { } # called last in the ctor
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
159 ▶sub fini { } # called first in the dtor
Subroutine fini: called CC=1 SCAR=0.0
Pod undocumented
160 ▶sub preloop { }
Subroutine preloop: not called CC=1 SCAR=0.0
Pod undocumented
161 ▶sub postloop { }
Subroutine postloop: not called CC=1 SCAR=0.0
Pod undocumented
162 ▶sub precmd { }
Subroutine precmd: called CC=1 SCAR=0.0
Pod undocumented
163 ▶sub postcmd { }
Subroutine postcmd: called CC=1 SCAR=0.0
Pod undocumented
164 0 ▶sub prompt_str { 'shell> ' }
Subroutine prompt_str: not called CC=1 SCAR=6.9
Pod undocumented
165 ▶sub idle { }
Subroutine idle: not called CC=1 SCAR=0.0
Pod undocumented
166 55 ▶sub cmd_prefix { '' }
Subroutine cmd_prefix: called CC=1 SCAR=0.0
Pod undocumented
167 55 ▶sub cmd_suffix { '' }
Subroutine cmd_suffix: called CC=1 SCAR=0.0
Pod undocumented
168  
169 #=============================================================================
170 # The pager
171 #=============================================================================
172 sub page
173 {
174 1 ▶ my $o = shift;
Subroutine page: called CC=10 SCAR=39.4
Pod undocumented
175 1 my $text = shift;
176 1 ▶ my $maxlines = shift || $o->termsize->{rows};
Condition33%1/3
shift() || $o->termsize->{'rows'}
l!l&&r!l&&!r
001
MC/DC0%0/2
shift() || $o->termsize->{'rows'}
shift() $o->termsize->{'rows'}
Truth table33%1/3
A || B
A= shift()B= $o->termsize->{'rows'}
ABresult
1X1
011
000
177 1 my $pager = $o->{API}{pager};
178  
179 # First, count the number of lines in the text:
180 1 my $lines = ( $text =~ tr/\n// );
181  
182 # If there are fewer lines than the page-lines, just print it.
183 1 ▶ if ( $lines < $maxlines or $maxlines == 0 or $pager eq 'none' )
Branch: 2 branches
BranchTrueFalseTotal
if ($lines < $maxlines or $maxlines == 0 or $pager eq 'none') { } 1 0 1
elsif ($pager and $pager ne 'internal') { } 0 0 0
Condition33%2/6
$lines < $maxlines or $maxlines == 0 or $pager eq "none"
l!l&&r!l&&!r
100
$lines < $maxlines or $maxlines == 0
l!l&&r!l&&!r
010
MC/DC0%0/3
$lines < $maxlines or $maxlines == 0 or $pager eq "none"
$lines < $maxlines $maxlines == 0 $pager eq "none"
Truth table25%1/4
A or B or C
A= $lines < $maxlinesB= $maxlines == 0C= $pager eq "none"
ABCresult
1XX1
01X1
0011
0000
184 {
185 1 print $text;
186 }
187  
188 # If there are more, page it, either using the external pager...
189 ▶ elsif ( $pager and $pager ne 'internal' )
Condition0%0/3
$pager and $pager ne "internal"
!ll&&!rl&&r
000
MC/DC0%0/2
$pager and $pager ne "internal"
$pager $pager ne "internal"
Truth table0%0/3
A and B
A= $pagerB= $pager ne "internal"
ABresult
0X0
100
111
190 {
191 0 require File::Temp;
192 0 my ( $handle, $name ) = File::Temp::tempfile();
193 0 select( ( select($handle), $| = 1 )[0] );
194 0 print $handle $text;
195 0 close $handle;
196 0 ▶ system( $pager, $name ) == 0
Branch: 1 branch
BranchTrueFalseTotal
system($pager, $name) == 0 or print "Warning: can't run external pager '${pager}': $!.\n" 0 0 0
197 or print <<END;
198 Warning: can't run external pager '$pager': $!.
199 END
200 0 unlink $name;
201 }
202  
203 # ... or the internal one
204 else
205 {
206 0 my $togo = $lines;
207 0 my $line = 0;
208 0 my @lines = split '^', $text;
209 0 ▶ while ( $togo > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($togo > 0) 0 0 0
210 {
211 0 my @text = @lines[ $line .. $#lines ];
212 0 my $ret = $o->page_internal( \@text, $maxlines, $togo, $line );
213 0 ▶ last if $ret == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $ret == -1 0 0 0
214 0 $line += $ret;
215 0 $togo -= $ret;
216 }
217 0 return $line;
218 }
219 1 return $lines;
220 }
221  
222 sub page_internal
223 {
224 0 ▶ my $o = shift;
Subroutine page_internal: not called CC=9 SCAR=45.0
Pod undocumented
225 0 my $lines = shift;
226 0 my $maxlines = shift;
227 0 my $togo = shift;
228 0 my $start = shift;
229  
230 0 my $line = 1;
231 0 ▶ while ( $_ = shift @$lines )
Branch: 1 branch
BranchTrueFalseTotal
if ($_ = shift @$lines) 0 0 0
232 {
233 0 print;
234 0 ▶ last if $line >= ( $maxlines - 1 ); # leave room for the prompt
Branch: 1 branch
BranchTrueFalseTotal
if $line >= $maxlines - 1 0 0 0
235 0 $line++;
236 }
237 0 my $lines_left = $togo - $line;
238 0 my $current_line = $start + $line;
239 0 my $total_lines = $togo + $start;
240  
241 0 my $instructions;
242 0 ▶ if ( $o->have_readkey )
Branch: 1 branch
BranchTrueFalseTotal
if ($o->have_readkey) { } 0 0 0
243 {
244 0 $instructions = "any key for more, or q to quit";
245 }
246 else
247 {
248 0 $instructions = "enter for more, or q to quit";
249 }
250  
251 0 ▶ if ( $lines_left > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($lines_left > 0) 0 0 0
252 {
253 0 local $| = 1;
254 0 my $l = "---line $current_line/$total_lines ($instructions)---";
255 0 my $b = ' ' x length($l);
256 0 print $l;
257 0 my $ans = $o->readkey;
258 0 ▶ print "\r$b\r" if $o->have_readkey;
Branch: 1 branch
BranchTrueFalseTotal
if $o->have_readkey 0 0 0
259 0 ▶ print "\n" if $ans =~ /q/i or not $o->have_readkey;
Branch: 1 branch
BranchTrueFalseTotal
if $ans =~ /q/iu or not $o->have_readkey 0 0 0
Condition0%0/3
$ans =~ /q/iu or not $o->have_readkey
l!l&&r!l&&!r
000
MC/DC0%0/2
$ans =~ /q/iu or not $o->have_readkey
$ans =~ /q/iu not $o->have_readkey
Truth table0%0/3
A or not(B)
A= $ans =~ /q/iuB= not $o->have_readkey
ABresult
1X1
011
000
260 0 ▶ $line = -1 if $ans =~ /q/i;
Branch: 1 branch
BranchTrueFalseTotal
if $ans =~ /q/iu 0 0 0
261 }
262 0 $line;
263 }
264  
265 #=============================================================================
266 # Run actions
267 #=============================================================================
268 sub run
269 {
270 0 ▶ my $o = shift;
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
271 0 my $action = shift;
272 0 my @args = @_;
273 0 $o->do_action( $action, \@args, 'run' );
274 }
275  
276 sub complete
277 {
278 0 ▶ my $o = shift;
Subroutine complete: not called CC=2 SCAR=17.9
Pod undocumented
279 0 my $action = shift;
280 0 my @args = @_;
281 0 my @compls = $o->do_action( $action, \@args, 'comp' );
282 0 ▶ return () unless $o->{command}{comp}{found};
Branch: 1 branch
BranchTrueFalseTotal
unless $o->{"command"}{"comp"}{"found"} 0 0 0
283 0 return @compls;
284 }
285  
286 sub help
287 {
288 0 ▶ my $o = shift;
Subroutine help: not called CC=1 SCAR=6.9
Pod undocumented
289 0 my $topic = shift;
290 0 my @subtopics = @_;
291 0 $o->do_action( $topic, \@subtopics, 'help' );
292 }
293  
294 sub summary
295 {
296 3 ▶ my $o = shift;
Subroutine summary: called CC=1 SCAR=0.0
Pod undocumented
297 3 my $topic = shift;
298 3 $o->do_action( $topic, [], 'smry' );
299 }
300  
301 #=============================================================================
302 # Manually add & remove handlers
303 #=============================================================================
304 sub add_handlers
305 {
306 5 ▶ my $o = shift;
Subroutine add_handlers: called CC=9 SCAR=22.3
Pod undocumented
307 my $match = sub {
308 498 ▶ if ( my ($ret) = shift =~ /\A(run|help|smry|comp|catch|alias)_/ )
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (my($ret) = (shift()) =~ /\A(run|help|smry|comp|catch|alias)_/u) 64 434 498
309 {
310 64 return $ret;
311 }
312 434 return;
313 5 };
314 LOOP1:
315 5 for my $hnd (@_)
316 {
317 249 my $t = $match->($hnd);
318 249 ▶ next LOOP1 if !defined $t;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t 217 32 249
319 32 my $s = substr( $hnd, length($t) + 1 );
320  
321 # Add on the prefix and suffix if the command is defined
322 32 ▶ if ( length $s )
Branch: 1 branch
BranchTrueFalseTotal
if (length $s) 26 6 32
323 {
324 26 substr( $s, 0, 0 ) = $o->cmd_prefix;
325 26 $s .= $o->cmd_suffix;
326 }
327 32 $o->{handlers}{$s}{$t} = $hnd;
328 }
329 LOOP2:
330 5 for my $hnd (@_)
331 {
332 249 my $t = $match->($hnd);
333 249 ▶ next LOOP2 if !defined $t;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t 217 32 249
334 32 my $s = substr( $hnd, length($t) + 1 );
335  
336 # Add on the prefix and suffix if the command is defined
337 32 ▶ if ( length $s )
Branch: 1 branch
BranchTrueFalseTotal
if (length $s) 26 6 32
338 {
339 26 substr( $s, 0, 0 ) = $o->cmd_prefix;
340 26 $s .= $o->cmd_suffix;
341 }
342 32 ▶ if ( $o->has_aliases($s) )
Branch: 1 branch
BranchTrueFalseTotal
if ($o->has_aliases($s)) 0 32 32
343 {
344 0 my @s = $o->get_aliases($s);
345 0 for my $alias (@s)
346 {
347 0 substr( $alias, 0, 0 ) = $o->cmd_prefix;
348 0 $alias .= $o->cmd_suffix;
349 0 $o->{handlers}{$alias}{$t} = $hnd;
350 }
351 }
352 }
353 }
354  
355 sub add_commands
356 {
357 0 ▶ my $o = shift;
Subroutine add_commands: not called CC=2 SCAR=17.9
Pod undocumented
358 0 ▶ while (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
359 {
360 0 my ( $cmd, $hnd ) = ( shift, shift );
361 0 $o->{handlers}{$cmd} = $hnd;
362 }
363 }
364  
365 sub remove_handlers
366 {
367 0 ▶ my $o = shift;
Subroutine remove_handlers: not called CC=4 SCAR=30.0
Pod undocumented
368 0 for my $hnd (@_)
369 {
370 0 ▶ next unless $hnd =~ /^(run|help|smry|comp|catch|alias)_/o;
Branch: 1 branch
BranchTrueFalseTotal
unless $hnd =~ /^(run|help|smry|comp|catch|alias)_/ou 0 0 0
371 0 my $t = $1;
372 0 my $a = substr( $hnd, length($t) + 1 );
373  
374 # Add on the prefix and suffix if the command is defined
375 0 ▶ if ( length $a )
Branch: 1 branch
BranchTrueFalseTotal
if (length $a) 0 0 0
376 {
377 0 substr( $a, 0, 0 ) = $o->cmd_prefix;
378 0 $a .= $o->cmd_suffix;
379 }
380 0 delete $o->{handlers}{$a}{$t};
381 }
382 }
383  
384 sub remove_commands
385 {
386 0 ▶ my $o = shift;
Subroutine remove_commands: not called CC=2 SCAR=17.9
Pod undocumented
387 0 for my $name (@_)
388 {
389 0 delete $o->{handlers}{$name};
390 }
391 }
392  
393 4 *add_handler = \&add_handlers;
394 4 *add_command = \&add_commands;
395 4 *remove_handler = \&remove_handlers;
396 4 *remove_command = \&remove_commands;
397  
398 #=============================================================================
399 # Utility methods
400 #=============================================================================
401 sub termsize
402 {
403 3 ▶ my $o = shift;
Subroutine termsize: called CC=20 SCAR=50.4
Pod undocumented
404 3 my ( $rows, $cols ) = ( 24, 78 );
405  
406 # Try several ways to get the terminal size
407 TERMSIZE:
408 {
409 3 my $TERM = $o->{term};
410 3 ▶ last TERMSIZE unless $TERM;
Branch: 1 branch
BranchTrueFalseTotal
unless $TERM 0 3 3
411  
412 3 my $OUT = $TERM->OUT;
413  
414 3 ▶ if ( $TERM and $o->{API}{readline} eq 'Term::ReadLine::Gnu' )
Branch: 1 branch
BranchTrueFalseTotal
if ($TERM and $o->{"API"}{"readline"} eq "Term::ReadLine::Gnu") 0 3 3
Condition33%1/3
$TERM and $o->{"API"}{"readline"} eq "Term::ReadLine::Gnu"
!ll&&!rl&&r
030
MC/DC0%0/2
$TERM and $o->{"API"}{"readline"} eq "Term::ReadLine::Gnu"
$TERM $o->{"API"}{"readline"} eq "Term::ReadLine::Gnu"
Truth table33%1/3
A and B
A= $TERMB= $o->{"API"}{"readline"} eq "Term::ReadLine::Gnu"
ABresult
0X0
100
111
415 {
416 0 ( $rows, $cols ) = $TERM->get_screen_size;
417 0 last TERMSIZE;
418 }
419  
420 3 ▶ if ( $^O eq 'MSWin32' and eval { require Win32::Console } )
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32" and eval { do { require Win32::Console } }) 0 3 3
Condition33%1/3
$^O eq "MSWin32" and eval { do { require Win32::Console } }
!ll&&!rl&&r
300
MC/DC0%0/2
$^O eq "MSWin32" and eval { do { require Win32::Console } }
$^O eq "MSWin32" eval { do { require Win32::Console } }
Truth table33%1/3
A and B
A= $^O eq "MSWin32"B= eval { do { require Win32::Console } }
ABresult
0X0
100
111
421 {
422 0 Win32::Console->import;
423  
424 # Win32::Console's DESTROY does a CloseHandle(), so save the object:
425 0 ▶ $o->{win32_stdout} ||= Win32::Console->new( STD_OUTPUT_HANDLE() );
Condition0%0/3
$o->{"win32_stdout"} ||= "Win32::Console"->new(STD_OUTPUT_HANDLE())
l!l&&r!l&&!r
000
MC/DC0%0/2
$o->{"win32_stdout"} ||= "Win32::Console"->new(STD_OUTPUT_HANDLE())
$o->{"win32_stdout"} "Win32::Console"->new(STD_OUTPUT_HANDLE())
Truth table0%0/3
A ||= B
A= $o->{"win32_stdout"}B= "Win32::Console"->new(STD_OUTPUT_HANDLE())
ABresult
1X1
011
000
426 0 my @info = $o->{win32_stdout}->Info;
427 0 $cols = $info[7] - $info[5] + 1; # right - left + 1
428 0 $rows = $info[8] - $info[6] + 1; # bottom - top + 1
429 0 last TERMSIZE;
430 }
431  
432 3 ▶ if ( eval { require Term::Size } )
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Term::Size } }) 0 3 3
433 {
434 0 my @x = Term::Size::chars($OUT);
435 0 ▶ if ( @x == 2 and $x[0] )
Branch: 1 branch
BranchTrueFalseTotal
if (@x == 2 and $x[0]) 0 0 0
Condition0%0/3
@x == 2 and $x[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@x == 2 and $x[0]
@x == 2 $x[0]
Truth table0%0/3
A and B
A= @x == 2B= $x[0]
ABresult
0X0
100
111
436 {
437 0 ( $cols, $rows ) = @x;
438 0 last TERMSIZE;
439 }
440 }
441  
442 3 ▶ if ( eval { require Term::Screen } )
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Term::Screen } }) 0 3 3
443 {
444 0 my $screen = Term::Screen->new;
445 0 ( $rows, $cols ) = @$screen{qw(ROWS COLS)};
446 0 last TERMSIZE;
447 }
448  
449 3 ▶ if ( eval { require Term::ReadKey } )
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Term::ReadKey } }) 3 0 3
450 {
451 3 ( $cols, $rows ) = eval {
452 3 ▶ local $SIG{__WARN__} = sub { };
Subroutine __ANON__: called CC=1 SCAR=0.0
453 3 Term::ReadKey::GetTerminalSize($OUT);
454 };
455 3 ▶ last TERMSIZE unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 3 0 3
456 }
457  
458 0 ▶ if ( $ENV{LINES} or $ENV{ROWS} or $ENV{COLUMNS} )
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LINES"} or $ENV{"ROWS"} or $ENV{"COLUMNS"}) 0 0 0
Condition0%0/6
$ENV{"LINES"} or $ENV{"ROWS"} or $ENV{"COLUMNS"}
l!l&&r!l&&!r
000
$ENV{"LINES"} or $ENV{"ROWS"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$ENV{"LINES"} or $ENV{"ROWS"} or $ENV{"COLUMNS"}
$ENV{"LINES"} $ENV{"ROWS"} $ENV{"COLUMNS"}
Truth table0%0/4
A or B or C
A= $ENV{"LINES"}B= $ENV{"ROWS"}C= $ENV{"COLUMNS"}
ABCresult
1XX1
01X1
0011
0000
459 {
460 0 ▶ $rows = $ENV{LINES} || $ENV{ROWS} || $rows;
Condition0%0/6
$ENV{'LINES'} || $ENV{'ROWS'} || $rows
l!l&&r!l&&!r
000
$ENV{"LINES"} or $ENV{"ROWS"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$ENV{'LINES'} || $ENV{'ROWS'} || $rows
$ENV{"LINES"} $ENV{"ROWS"} $rows
Truth table0%0/4
A or B || C
A= $ENV{"LINES"}B= $ENV{"ROWS"}C= $rows
ABCresult
1XX1
01X1
0011
0000
461 0 ▶ $cols = $ENV{COLUMNS} || $cols;
Condition0%0/3
$ENV{'COLUMNS'} || $cols
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'COLUMNS'} || $cols
$ENV{'COLUMNS'} $cols
Truth table0%0/3
A || B
A= $ENV{'COLUMNS'}B= $cols
ABresult
1X1
011
000
462 0 last TERMSIZE;
463 }
464  
465 {
466 0 local $^W;
467 0 ▶ if ( open( my $STTY, "-|", "stty", "size" ) )
Branch: 1 branch
BranchTrueFalseTotal
if (open my $STTY, "-|", "stty", "size") 0 0 0
468 {
469 0 my $l = <$STTY>;
470 0 ( $rows, $cols ) = split /\s+/, $l;
471 0 close $STTY;
472 }
473 }
474 }
475  
476 3 return { rows => $rows, cols => $cols };
477 }
478  
479 sub readkey
480 {
481 0 ▶ my $o = shift;
Subroutine readkey: not called CC=2 SCAR=17.9
Pod undocumented
482 0 ▶ $o->have_readkey unless $o->{readkey};
Branch: 1 branch
BranchTrueFalseTotal
unless $o->{"readkey"} 0 0 0
483 0 $o->{readkey}->();
484 }
485  
486 sub have_readkey
487 {
488 0 ▶ my $o = shift;
Subroutine have_readkey: not called CC=6 SCAR=37.4
Pod undocumented
489 0 ▶ return 1 if $o->{have_readkey};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"have_readkey"} 0 0 0
490 0 my $IN = $o->{term}->IN;
491 0 ▶ if ( eval { require Term::InKey } )
Branch: 3 branches
BranchTrueFalseTotal
if (eval { do { require Term::InKey } }) { } 0 0 0
elsif ($^O eq 'MSWin32' and eval { do { require Win32::Console } }) { } 0 0 0
elsif (eval { do { require Term::ReadKey } }) { } 0 0 0
492 {
493 0 $o->{readkey} = \&Term::InKey::ReadKey;
494 }
495 0 ▶ elsif ( $^O eq 'MSWin32' and eval { require Win32::Console } )
Condition0%0/3
$^O eq "MSWin32" and eval { do { require Win32::Console } }
!ll&&!rl&&r
000
MC/DC0%0/2
$^O eq "MSWin32" and eval { do { require Win32::Console } }
$^O eq "MSWin32" eval { do { require Win32::Console } }
Truth table0%0/3
A and B
A= $^O eq "MSWin32"B= eval { do { require Win32::Console } }
ABresult
0X0
100
111
496 {
497 $o->{readkey} = sub {
498 0 ▶ my $c;
Subroutine __ANON__: not called CC=8 SCAR=42.8
499  
500 # from Term::InKey:
501 0 eval {
502 # Win32::Console's DESTROY does a CloseHandle(), so save it:
503 0 Win32::Console->import;
504 0 ▶ $o->{win32_stdin} ||= Win32::Console->new( STD_INPUT_HANDLE() );
Condition0%0/3
$o->{"win32_stdin"} ||= "Win32::Console"->new(STD_INPUT_HANDLE())
l!l&&r!l&&!r
000
MC/DC0%0/2
$o->{"win32_stdin"} ||= "Win32::Console"->new(STD_INPUT_HANDLE())
$o->{"win32_stdin"} "Win32::Console"->new(STD_INPUT_HANDLE())
Truth table0%0/3
A ||= B
A= $o->{"win32_stdin"}B= "Win32::Console"->new(STD_INPUT_HANDLE())
ABresult
1X1
011
000
505 0 ▶ my $mode = my $orig = $o->{win32_stdin}->Mode or die $^E;
Branch: 1 branch
BranchTrueFalseTotal
my $mode = my $orig = $o->{"win32_stdin"}->Mode or die $^E 0 0 0
506 0 $mode &= ~( ENABLE_LINE_INPUT() | ENABLE_ECHO_INPUT() );
507 0 ▶ $o->{win32_stdin}->Mode($mode) or die $^E;
Branch: 1 branch
BranchTrueFalseTotal
$o->{"win32_stdin"}->Mode($mode) or die $^E 0 0 0
508  
509 0 ▶ $o->{win32_stdin}->Flush or die $^E;
Branch: 1 branch
BranchTrueFalseTotal
$o->{"win32_stdin"}->Flush or die $^E 0 0 0
510 0 $c = $o->{win32_stdin}->InputChar(1);
511 0 ▶ die $^E unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 0 0
512 0 ▶ $o->{win32_stdin}->Mode($orig) or die $^E;
Branch: 1 branch
BranchTrueFalseTotal
$o->{"win32_stdin"}->Mode($orig) or die $^E 0 0 0
513 };
514 0 ▶ die "Not implemented on $^O: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
515 0 $c;
516 0 };
517 }
518 0 elsif ( eval { require Term::ReadKey } )
519 {
520 $o->{readkey} = sub {
521 0 ▶ Term::ReadKey::ReadMode( 4, $IN );
Subroutine __ANON__: not called CC=1 SCAR=6.9
522 0 my $c = getc($IN);
523 0 Term::ReadKey::ReadMode( 0, $IN );
524 0 $c;
525 0 };
526 }
527 else
528 {
529 0 ▶ $o->{readkey} = sub { scalar <$IN> };
Subroutine __ANON__: not called CC=1 SCAR=6.9
530 0 return $o->{have_readkey} = 0;
531 }
532 0 return $o->{have_readkey} = 1;
533 }
534 4 *has_readkey = \&have_readkey;
535  
536 sub prompt
537 {
538 0 ▶ my $o = shift;
Subroutine prompt: not called CC=5 SCAR=34.0
Pod undocumented
539 0 my ( $prompt, $default, $completions, $casei ) = @_;
540 0 my $term = $o->{term};
541  
542 # A closure to read the line.
543 0 my $line;
544 my $readline = sub {
545 0 ▶ my ( $sh, $gh ) = @{ $term->Features }{qw(setHistory getHistory)};
Subroutine __ANON__: not called CC=7 SCAR=40.3
546 0 ▶ my @history = $gh ? $term->GetHistory : ();
Branch: 1 branch
BranchTrueFalseTotal
$gh ? : 0 0 0
547 0 ▶ $term->SetHistory() if $sh;
Branch: 1 branch
BranchTrueFalseTotal
if $sh 0 0 0
548 0 $line = $o->readline($prompt);
549 0 ▶ $line = $default
Branch: 1 branch
BranchTrueFalseTotal
if !defined($line) || $line =~ /^\s*$/u and defined $default 0 0 0
Condition0%0/6
!defined($line) || $line =~ /^\s*$/u and defined $default
!ll&&!rl&&r
000
not defined $line or $line =~ /^\s*$/u
l!l&&r!l&&!r
000
MC/DC0%0/3
!defined($line) || $line =~ /^\s*$/u and defined $default
not defined $line $line =~ /^\s*$/u defined $default
Truth table0%0/5
not(A) or B and C
A= not defined $lineB= $line =~ /^\s*$/uC= defined $default
ABCresult
00X0
1X00
0100
1X11
0111
550 if ( ( not defined $line or $line =~ /^\s*$/ )
551 and defined $default );
552  
553 # Restore the history
554 0 ▶ $term->SetHistory(@history) if $sh;
Branch: 1 branch
BranchTrueFalseTotal
if $sh 0 0 0
555 0 $line;
556 0 };
557  
558 # A closure to complete the line.
559 my $complete = sub {
560 0 ▶ my ( $word, $line, $start ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
561 0 return $o->completions( $word, $completions, $casei );
562 0 };
563  
564 0 ▶ if ( $term and $term->ReadLine eq 'Term::ReadLine::Gnu' )
Branch: 2 branches
BranchTrueFalseTotal
if ($term and $term->ReadLine eq 'Term::ReadLine::Gnu') { } 0 0 0
elsif ($term and $term->ReadLine eq 'Term::ReadLine::Perl') { } 0 0 0
Condition0%0/3
$term and $term->ReadLine eq "Term::ReadLine::Gnu"
!ll&&!rl&&r
000
MC/DC0%0/2
$term and $term->ReadLine eq "Term::ReadLine::Gnu"
$term $term->ReadLine eq "Term::ReadLine::Gnu"
Truth table0%0/3
A and B
A= $termB= $term->ReadLine eq "Term::ReadLine::Gnu"
ABresult
0X0
100
111
565 {
566 0 my $attribs = $term->Attribs;
567 0 local $attribs->{completion_function} = $complete;
568 0 &$readline;
569 }
570 ▶ elsif ( $term and $term->ReadLine eq 'Term::ReadLine::Perl' )
Condition0%0/3
$term and $term->ReadLine eq "Term::ReadLine::Perl"
!ll&&!rl&&r
000
MC/DC0%0/2
$term and $term->ReadLine eq "Term::ReadLine::Perl"
$term $term->ReadLine eq "Term::ReadLine::Perl"
Truth table0%0/3
A and B
A= $termB= $term->ReadLine eq "Term::ReadLine::Perl"
ABresult
0X0
100
111
571 {
572 0 local $readline::rl_completion_function = $complete;
573 0 &$readline;
574 }
575 else
576 {
577 0 &$readline;
578 }
579 0 $line;
580 }
581  
582 sub format_pairs
583 {
584 2 ▶ my $o = shift;
Subroutine format_pairs: called CC=19 SCAR=36.5
Pod undocumented
585 2 my @keys = @{ shift(@_) };
586 2 my @vals = @{ shift(@_) };
587 2 ▶ my $sep = shift || ": ";
Condition50%1/2
shift() || ': '
l!l
20
MC/DC0%0/1
shift() || ': '
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
588 2 ▶ my $left = shift || 0;
Condition50%1/2
shift() || 0
l!l
20
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
589 2 ▶ my $ind = shift || "";
Condition50%1/2
shift() || ''
l!l
02
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
590 2 ▶ my $len = shift || 0;
Condition50%1/2
shift() || 0
l!l
02
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
591 2 ▶ my $wrap = shift || 0;
Condition50%1/2
shift() || 0
l!l
02
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
592  
593 2 ▶ if ($wrap)
Branch: 1 branch
BranchTrueFalseTotal
if ($wrap) 0 2 2
594 {
595 0 eval {
596 0 require Text::Autoformat;
597 0 Text::Autoformat->import(qw(autoformat));
598 };
599 0 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
600 {
601 0 ▶ warn(
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
602 "Term::Shell::format_pairs(): Text::Autoformat is required "
603 . "for wrapping. Wrapping disabled" )
604 if $^W;
605 0 $wrap = 0;
606 }
607 }
608 2 ▶ my $cols = shift || $o->termsize->{cols};
Condition33%1/3
shift() || $o->termsize->{'cols'}
l!l&&r!l&&!r
002
MC/DC0%0/2
shift() || $o->termsize->{'cols'}
shift() $o->termsize->{'cols'}
Truth table33%1/3
A || B
A= shift()B= $o->termsize->{'cols'}
ABresult
1X1
011
000
609 2 ▶ $len < length($_) and $len = length($_) for @keys;
Branch: 1 branch
BranchTrueFalseTotal
$len < length $_ and $len = length $_ 1 2 3
610 2 my @text;
611 2 for my $i ( 0 .. $#keys )
612 {
613 3 ▶ next unless defined $vals[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vals[$i] 0 3 3
614 3 my $sz = ( $len - length( $keys[$i] ) );
615 3 ▶ my $lpad = $left ? "" : " " x $sz;
Branch: 1 branch
BranchTrueFalseTotal
$left ? : 3 0 3
616 3 ▶ my $rpad = $left ? " " x $sz : "";
Branch: 1 branch
BranchTrueFalseTotal
$left ? : 3 0 3
617 3 my $l = "$ind$lpad$keys[$i]$rpad$sep";
618 3 ▶ my $wrap = $wrap & ( $vals[$i] =~ /\s/ and $vals[$i] !~ /^\d/ );
Condition33%1/3
$vals[$i] =~ /\s/u && !($vals[$i] =~ /^\d/u)
!ll&&!rl&&r
003
MC/DC0%0/2
$vals[$i] =~ /\s/u && !($vals[$i] =~ /^\d/u)
$vals[$i] =~ /\s/u !($vals[$i] =~ /^\d/u)
Truth table33%1/3
A && not(B)
A= $vals[$i] =~ /\s/uB= !($vals[$i] =~ /^\d/u)
ABresult
0X0
100
111
619 3 ▶ my $form = (
Branch: 1 branch
BranchTrueFalseTotal
$wrap ? : 0 3 3
620 $wrap
621 ? autoformat(
622 "$vals[$i]", # force stringification
623 { left => length($l) + 1, right => $cols, all => 1 },
624 )
625 : "$l$vals[$i]\n"
626 );
627 3 substr( $form, 0, length($l), $l );
628 3 push @text, $form;
629 }
630 2 my $text = join '', @text;
631 2 ▶ return wantarray ? ( $text, $len ) : $text;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
632 }
633  
634 sub print_pairs
635 {
636 0 ▶ my $o = shift;
Subroutine print_pairs: not called CC=1 SCAR=6.9
Pod undocumented
637 0 my ( $text, $len ) = $o->format_pairs(@_);
638 0 $o->page($text);
639 0 return $len;
640 }
641  
642 # Handle backslash translation; doesn't do anything complicated yet.
643 sub process_esc
644 {
645 0 ▶ my $o = shift;
Subroutine process_esc: not called CC=3 SCAR=24.8
Pod undocumented
646 0 my $c = shift;
647 0 my $q = shift;
648 0 my $n;
649 0 ▶ return '\\' if $c eq '\\';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "\\" 0 0 0
650 0 ▶ return $q if $c eq $q;
Branch: 1 branch
BranchTrueFalseTotal
if $c eq $q 0 0 0
651 0 return "\\$c";
652 }
653  
654 # Parse a quoted string
655 sub parse_quoted
656 {
657 0 ▶ my $o = shift;
Subroutine parse_quoted: not called CC=4 SCAR=30.0
Pod undocumented
658 0 my $raw = shift;
659 0 my $quote = shift;
660 0 my $i = 1;
661 0 my $string = '';
662 0 my $c;
663 0 ▶ while ( $i <= length($raw) and ( $c = substr( $raw, $i, 1 ) ) ne $quote )
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= length $raw and ($c = substr($raw, $i, 1)) ne $quote) 0 0 0
Condition0%0/3
$i <= length $raw and ($c = substr($raw, $i, 1)) ne $quote
!ll&&!rl&&r
000
MC/DC0%0/2
$i <= length $raw and ($c = substr($raw, $i, 1)) ne $quote
$i <= length $raw ($c = substr($raw, $i, 1)) ne $quote
Truth table0%0/3
A and B
A= $i <= length $rawB= ($c = substr($raw, $i, 1)) ne $quote
ABresult
0X0
100
111
664 {
665  
666 0 ▶ if ( $c eq '\\' )
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq '\\') { } 0 0 0
667 {
668 0 $string .= $o->process_esc( substr( $raw, $i + 1, 1 ), $quote );
669 0 $i++;
670 }
671 else
672 {
673 0 $string .= substr( $raw, $i, 1 );
674 }
675 0 $i++;
676 }
677 0 return ( $string, $i );
678 }
679  
680 sub line
681 {
682 0 ▶ my $o = shift;
Subroutine line: not called CC=1 SCAR=6.9
Pod undocumented
683 0 $o->{line};
684 }
685  
686 sub line_args
687 {
688 0 ▶ my $o = shift;
Subroutine line_args: not called CC=3 SCAR=24.8
Pod undocumented
689 0 ▶ my $line = shift || $o->line;
Condition0%0/3
shift() || $o->line
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $o->line
shift() $o->line
Truth table0%0/3
A || B
A= shift()B= $o->line
ABresult
1X1
011
000
690 0 $o->line_parsed($line);
691 0 ▶ $o->{line_args} || '';
Branch: 1 branch
BranchTrueFalseTotal
$o->{"line_args"} or "" 0 0 0
692 }
693  
694 sub line_parsed
695 {
696 0 ▶ my $o = shift;
Subroutine line_parsed: not called CC=12 SCAR=50.5
Pod undocumented
697 0 ▶ my $args = shift || $o->line || return ();
Condition0%0/5
shift() || $o->line || (return ())
l!l
00
shift() or $o->line
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $o->line || (return ())
shift() $o->line
Truth table0%0/3
A or B
A= shift()B= $o->line
ABresult
1X1
011
000
698 0 my @args;
699  
700 # Parse an array of arguments. Whitespace separates, unless quoted.
701 0 my $arg = undef;
702 0 $o->{line_args} = undef;
703 0 ▶ for ( my $i = 0 ; $i < length($args) ; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $args) 0 0 0
704 {
705 0 my $c = substr( $args, $i, 1 );
706 0 ▶ if ( $c =~ /\S/ and @args == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /\S/u and @args == 1) 0 0 0
Condition0%0/3
$c =~ /\S/u and @args == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$c =~ /\S/u and @args == 1
$c =~ /\S/u @args == 1
Truth table0%0/3
A and B
A= $c =~ /\S/uB= @args == 1
ABresult
0X0
100
111
707 {
708 0 ▶ $o->{line_args} ||= substr( $args, $i );
Condition0%0/3
$o->{"line_args"} ||= substr($args, $i)
l!l&&r!l&&!r
000
MC/DC0%0/2
$o->{"line_args"} ||= substr($args, $i)
$o->{"line_args"} substr($args, $i)
Truth table0%0/3
A ||= B
A= $o->{"line_args"}B= substr($args, $i)
ABresult
1X1
011
000
709 }
710 0 ▶ if ( $c =~ /['"]/ )
Branch: 2 branches
BranchTrueFalseTotal
if ($c =~ /['"]/u) { } 0 0 0
elsif ($c =~ /\s/u) { } 0 0 0
711 {
712 0 my ( $str, $n ) = $o->parse_quoted( substr( $args, $i ), $c );
713 0 $i += $n;
714 0 ▶ $arg = ( defined($arg) ? $arg : '' ) . $str;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg ? : 0 0 0
715 }
716  
717 # We do not parse outside of strings
718 # elsif ($c eq '\\') {
719 # $arg = (defined($arg) ? $arg : '')
720 # . $o->process_esc(substr($args,$i+1,1));
721 # $i++;
722 # }
723 elsif ( $c =~ /\s/ )
724 {
725 0 ▶ push @args, $arg if defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg 0 0 0
726 0 $arg = undef;
727 }
728 else
729 {
730 0 $arg .= substr( $args, $i, 1 );
731 }
732 }
733 0 ▶ push @args, $arg if defined($arg);
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg 0 0 0
734 0 return @args;
735 }
736  
737 sub handler
738 {
739 3 ▶ my $o = shift;
Subroutine handler: called CC=7 SCAR=19.9
Pod documented
740 3 my ( $command, $type, $args, $preserve_args ) = @_;
741  
742 # First try finding the standard handler, then fallback to the
743 # catch_$type method. The columns represent "action", "type", and "push",
744 # which control whether the name of the command should be pushed onto the
745 # args.
746 3 my @tries = (
747 [ $command, $type, 0 ],
748 [ $o->cmd_prefix . $type . $o->cmd_suffix, 'catch', 1 ],
749 );
750  
751 # The user can control whether or not to search for "unique" matches,
752 # which means calling $o->possible_actions(). We always look for exact
753 # matches.
754 3 my @matches = qw(exact_action);
755 3 ▶ push @matches, qw(possible_actions) if $o->{API}{match_uniq};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"API"}{"match_uniq"} 3 0 3
756  
757 3 for my $try (@tries)
758 {
759 4 my ( $cmd, $type, $add_cmd_name ) = @$try;
760 4 for my $match (@matches)
761 {
762 6 my @handlers = $o->$match( $cmd, $type );
763 6 ▶ next unless @handlers == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @handlers == 1 4 2 6
764 2 ▶ unshift @$args, $command
Branch: 1 branch
BranchTrueFalseTotal
if $add_cmd_name and not $preserve_args 0 2 2
Condition33%1/3
$add_cmd_name and not $preserve_args
!ll&&!rl&&r
200
MC/DC0%0/2
$add_cmd_name and not $preserve_args
$add_cmd_name not $preserve_args
Truth table33%1/3
A and not(B)
A= $add_cmd_nameB= not $preserve_args
ABresult
0X0
100
111
765 if $add_cmd_name and not $preserve_args;
766 2 return $o->unalias( $handlers[0], $type );
767 }
768 }
769 1 return;
770 }
771  
772 sub completions
773 {
774 0 ▶ my $o = shift;
Subroutine completions: not called CC=4 SCAR=30.0
Pod undocumented
775 0 my $action = shift;
776 0 ▶ my $compls = shift || [];
Condition0%0/2
shift() || []
l!l
00
MC/DC0%0/1
shift() || []
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
777 0 my $casei = shift;
778 0 ▶ $casei = $o->{API}{case_ignore} unless defined $casei;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $casei 0 0 0
779 0 ▶ $casei = $casei ? '(?i)' : '';
Branch: 1 branch
BranchTrueFalseTotal
$casei ? : 0 0 0
780 0 return grep { $_ =~ /$casei^\Q$action\E/ } @$compls;
781 }
782  
783 #=============================================================================
784 # Term::Shell error messages
785 #=============================================================================
786 sub msg_ambiguous_cmd
787 {
788 0 ▶ my ( $o, $cmd, @c ) = @_;
Subroutine msg_ambiguous_cmd: not called CC=1 SCAR=6.9
Pod undocumented
789 0 local $" = "\n\t";
790 0 <<END;
791 Ambiguous command '$cmd': possible commands:
792 @c
793 END
794 }
795  
796 sub msg_unknown_cmd
797 {
798 0 ▶ my ( $o, $cmd ) = @_;
Subroutine msg_unknown_cmd: not called CC=1 SCAR=6.9
Pod undocumented
799 0 <<END;
800 Unknown command '$cmd'; type 'help' for a list of commands.
801 END
802 }
803  
804 #=============================================================================
805 # Term::Shell private methods
806 #=============================================================================
807 sub do_action
808 {
809 3 ▶ my $o = shift;
Subroutine do_action: called CC=5 SCAR=16.2
Pod undocumented
810 3 my $cmd = shift;
811 3 ▶ my $args = shift || [];
Condition50%1/2
shift() || []
l!l
30
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
812 3 ▶ my $type = shift || 'run';
Condition50%1/2
shift() || 'run'
l!l
30
MC/DC0%0/1
shift() || 'run'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
813 3 my ( $fullname, $cmdname, $handler ) = $o->handler( $cmd, $type, $args );
814 3 ▶ $o->{command}{$type} = {
Branch: 1 branch
BranchTrueFalseTotal
defined $handler ? : 2 1 3
815 cmd => $cmd,
816 name => $cmd,
817 found => defined $handler ? 1 : 0,
818 cmdfull => $fullname,
819 cmdreal => $cmdname,
820 handler => $handler,
821 };
822 3 ▶ if ( defined $handler )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $handler) 2 1 3
823 {
824 # We've found a handler. Set up a value which will call the postcmd()
825 # action as the subroutine leaves. Then call the precmd(), then return
826 # the result of running the handler.
827 2 $o->precmd( \$handler, \$cmd, $args );
828 my $postcmd = Term::Shell::OnScopeLeave->new(
829 sub {
830 2 ▶ $o->postcmd( \$handler, \$cmd, $args );
Subroutine __ANON__: called CC=1 SCAR=0.0
831 }
832 2 );
833 2 return $o->$handler(@$args);
834 }
835 }
836  
837 sub uniq
838 {
839 0 ▶ my $o = shift;
Subroutine uniq: not called CC=4 SCAR=30.0
Pod undocumented
840 0 my %seen;
841 0 $seen{$_}++ for @_;
842 0 my @ret;
843 0 ▶ for (@_) { push @ret, $_ if $seen{$_}-- == 1 }
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$_}-- == 1 0 0 0
844 0 @ret;
845 }
846  
847 sub possible_actions
848 {
849 8 ▶ my $o = shift;
Subroutine possible_actions: called CC=2 SCAR=6.9
Pod undocumented
850 8 my $action = shift;
851 8 my $type = shift;
852 8 ▶ my $casei = $o->{API}{case_ignore} ? '(?i)' : '';
Branch: 1 branch
BranchTrueFalseTotal
$o->{'API'}{'case_ignore'} ? : 0 8 8
853 26 my @keys = grep { $_ =~ /$casei^\Q$action\E/ }
854 38 grep { exists $o->{handlers}{$_}{$type} }
855 8 keys %{ $o->{handlers} };
856 8 return @keys;
857 }
858  
859 sub exact_action
860 {
861 4 ▶ my $o = shift;
Subroutine exact_action: called CC=3 SCAR=11.0
Pod undocumented
862 4 my $action = shift;
863 4 my $type = shift;
864 4 ▶ my $casei = $o->{API}{case_ignore} ? '(?i)' : '';
Branch: 1 branch
BranchTrueFalseTotal
$o->{'API'}{'case_ignore'} ? : 0 4 4
865 6 my @key = grep { $action =~ /$casei^\Q$_\E$/ }
866 16 grep { exists $o->{handlers}{$_}{$type} }
867 4 keys %{ $o->{handlers} };
868 4 ▶ return () unless @key == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @key == 1 2 2 4
869 2 return $key[0];
870 }
871  
872 sub is_alias
873 {
874 0 ▶ my $o = shift;
Subroutine is_alias: not called CC=2 SCAR=17.9
Pod undocumented
875 0 my $action = shift;
876 0 ▶ exists $o->{handlers}{$action}{alias} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $o->{'handlers'}{$action}{'alias'} ? : 0 0 0
877 }
878  
879 sub has_aliases
880 {
881 32 ▶ my $o = shift;
Subroutine has_aliases: called CC=2 SCAR=7.0
Pod undocumented
882 32 my $action = shift;
883 32 my @a = $o->get_aliases($action);
884 32 ▶ @a ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@a ? : 0 32 32
885 }
886  
887 sub get_aliases
888 {
889 32 ▶ my $o = shift;
Subroutine get_aliases: called CC=3 SCAR=11.2
Pod undocumented
890 32 my $action = shift;
891 32 my @a = eval {
892 32 my $hndlr = $o->{handlers}{$action}{alias};
893 32 ▶ return () unless $hndlr;
Branch: 1 branch
BranchTrueFalseTotal
unless $hndlr 32 0 32
894 0 $o->$hndlr();
895 };
896 32 $o->{aliases}{$_} = $action for @a;
897 32 @a;
898 }
899  
900 sub unalias
901 {
902 2 ▶ my $o = shift;
Subroutine unalias: called CC=4 SCAR=17.9
Pod undocumented
903 2 my $cmd = shift; # i.e 'foozle'
904 2 my $type = shift; # i.e 'run'
905 2 ▶ return () unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 2 2
906 return ( $cmd, $cmd, $o->{handlers}{$cmd}{$type} )
907 2 ▶ unless exists $o->{aliases}{$cmd};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $o->{"aliases"}{$cmd} 2 0 2
908 0 my $alias = $o->{aliases}{$cmd};
909  
910 # I'm allowing aliases to call handlers which have been removed. This
911 # means I can set up an alias of '!' for 'shell', then delete the 'shell'
912 # command, so that you can only access it through '!'. That's why I'm
913 # checking the {handlers} entry _and_ building a string.
914 0 ▶ my $handler = $o->{handlers}{$alias}{$type} || "${type}_${alias}";
Condition0%0/2
$o->{'handlers'}{$alias}{$type} || "${type}_$alias"
l!l
00
MC/DC0%0/1
$o->{'handlers'}{$alias}{$type} || "${type}_$alias"
$o->{'handlers'}{$alias}{$type}
Truth table0%0/2
A
A= $o->{'handlers'}{$alias}{$type}
Aresult
11
00
915 0 return ( $cmd, $alias, $handler );
916 }
917  
918 sub find_handlers
919 {
920 5 ▶ my $o = shift;
Subroutine find_handlers: called CC=3 SCAR=11.0
Pod undocumented
921 5 ▶ my $pkg = shift || $o->{API}{class};
Condition66%2/3
shift() || $o->{'API'}{'class'}
l!l&&r!l&&!r
230
MC/DC0%0/2
shift() || $o->{'API'}{'class'}
shift() $o->{'API'}{'class'}
Truth table66%2/3
A || B
A= shift()B= $o->{'API'}{'class'}
ABresult
1X1
011
000
922  
923 # Find the handlers in the given namespace:
924 5 my %handlers;
925 {
926 ## no critic
927 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
928 5 my @r = keys %{ $pkg . "::" };
929 5 $o->add_handlers(@r);
930 }
931  
932 # Find handlers in its base classes.
933 {
934 ## no critic
935 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
936 5 my @isa = @{ $pkg . "::ISA" };
937 5 for my $pkg (@isa)
938 {
939 2 $o->find_handlers($pkg);
940 }
941 }
942 }
943  
944 sub rl_complete
945 {
946 0 ▶ my $o = shift;
Subroutine rl_complete: not called CC=5 SCAR=34.0
Pod undocumented
947 0 my ( $word, $line, $start ) = @_;
948  
949 # If it's a command, complete 'run_':
950 0 ▶ if ( $start == 0 or substr( $line, 0, $start ) =~ /^\s*$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($start == 0 or substr($line, 0, $start) =~ /^\s*$/u) { } 0 0 0
Condition0%0/3
$start == 0 or substr($line, 0, $start) =~ /^\s*$/u
l!l&&r!l&&!r
000
MC/DC0%0/2
$start == 0 or substr($line, 0, $start) =~ /^\s*$/u
$start == 0 substr($line, 0, $start) =~ /^\s*$/u
Truth table0%0/3
A or B
A= $start == 0B= substr($line, 0, $start) =~ /^\s*$/u
ABresult
1X1
011
000
951 {
952 0 my @compls = $o->complete( '', $word, $line, $start );
953 0 ▶ return @compls if $o->{command}{comp}{found};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"command"}{"comp"}{"found"} 0 0 0
954 }
955  
956 # If it's a subcommand, send it to any custom completion function for the
957 # function:
958 else
959 {
960 0 my $command = ( $o->line_parsed($line) )[0];
961 0 my @compls = $o->complete( $command, $word, $line, $start );
962 0 ▶ return @compls if $o->{command}{comp}{found};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"command"}{"comp"}{"found"} 0 0 0
963 }
964  
965 0 ();
966 }
967  
968 #=============================================================================
969 # Two action handlers provided by default: help and exit.
970 #=============================================================================
971 1 ▶sub smry_exit { "exits the program" }
Subroutine smry_exit: called CC=1 SCAR=0.0
Pod undocumented
972  
973 sub help_exit
974 {
975 0 ▶ <<'END';
Subroutine help_exit: not called CC=1 SCAR=6.9
Pod undocumented
976 Exits the program.
977 END
978 }
979  
980 sub run_exit
981 {
982 0 ▶ my $o = shift;
Subroutine run_exit: not called CC=1 SCAR=6.9
Pod undocumented
983 0 $o->stoploop;
984 }
985  
986 1 ▶sub smry_help { "prints this screen, or help on 'command'" }
Subroutine smry_help: called CC=1 SCAR=0.0
Pod undocumented
987  
988 sub help_help
989 {
990 0 ▶ <<'END';
Subroutine help_help: not called CC=1 SCAR=6.9
Pod undocumented
991 Provides help on commands...
992 END
993 }
994  
995 sub comp_help
996 {
997 0 ▶ my ( $o, $word, $line, $start ) = @_;
Subroutine comp_help: not called CC=4 SCAR=30.0
Pod undocumented
998 0 my @words = $o->line_parsed($line);
999 0 ▶ return []
Branch: 1 branch
BranchTrueFalseTotal
if @words > 2 or @words == 2 and $start == length $line 0 0 0
Condition0%0/6
@words > 2 or @words == 2 and $start == length $line
l!l&&r!l&&!r
000
@words == 2 and $start == length $line
!ll&&!rl&&r
000
MC/DC0%0/3
@words > 2 or @words == 2 and $start == length $line
@words > 2 @words == 2 $start == length $line
Truth table0%0/4
A or B and C
A= @words > 2B= @words == 2C= $start == length $line
ABCresult
1XX1
0111
00X0
0100
1000 if ( @words > 2 or @words == 2 and $start == length($line) );
1001 0 sort $o->possible_actions( $word, 'help' );
1002 }
1003  
1004 sub run_help
1005 {
1006 1 ▶ my $o = shift;
Subroutine run_help: called CC=14 SCAR=31.8
Pod undocumented
1007 1 my $cmd = shift;
1008 1 ▶ if ($cmd)
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd) { } 0 1 1
1009 {
1010 0 my $txt = $o->help( $cmd, @_ );
1011 0 ▶ if ( $o->{command}{help}{found} )
Branch: 1 branch
BranchTrueFalseTotal
if ($o->{'command'}{'help'}{'found'}) { } 0 0 0
1012 {
1013 0 $o->page($txt);
1014 }
1015 else
1016 {
1017 0 my @c = sort $o->possible_actions( $cmd, 'help' );
1018 0 ▶ if ( @c and $o->{API}{match_uniq} )
Branch: 1 branch
BranchTrueFalseTotal
if (@c and $o->{'API'}{'match_uniq'}) { } 0 0 0
Condition0%0/3
@c and $o->{"API"}{"match_uniq"}
!ll&&!rl&&r
000
MC/DC0%0/2
@c and $o->{"API"}{"match_uniq"}
@c $o->{"API"}{"match_uniq"}
Truth table0%0/3
A and B
A= @cB= $o->{"API"}{"match_uniq"}
ABresult
0X0
100
111
1019 {
1020 0 local $" = "\n\t";
1021 0 print <<END;
1022 Ambiguous help topic '$cmd': possible help topics:
1023 @c
1024 END
1025 }
1026 else
1027 {
1028 0 print <<END;
1029 Unknown help topic '$cmd'; type 'help' for a list of help topics.
1030 END
1031 }
1032 }
1033 }
1034 else
1035 {
1036 1 print "Type 'help command' for more detailed help on a command.\n";
1037 1 my ( %cmds, %docs );
1038 1 my %done;
1039 1 my %handlers;
1040 1 for my $h ( keys %{ $o->{handlers} } )
1041 {
1042 4 ▶ next unless length($h);
Branch: 1 branch
BranchTrueFalseTotal
unless length $h 1 3 4
1043 next
1044 3 ▶ unless grep { defined $o->{handlers}{$h}{$_} }
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $o->{"handlers"}{$h}{$_};} "run", "smry", "help" 0 3 3
1045 qw(run smry help);
1046 3 ▶ my $dest = exists $o->{handlers}{$h}{run} ? \%cmds : \%docs;
Branch: 1 branch
BranchTrueFalseTotal
exists $o->{'handlers'}{$h}{'run'} ? : 3 0 3
1047 3 ▶ my $smry = do { my $x = $o->summary($h); $x ? $x : "undocumented" };
Branch: 1 branch
BranchTrueFalseTotal
$x ? : 2 1 3
1048 my $help =
1049 exists $o->{handlers}{$h}{help}
1050 ? (
1051 exists $o->{handlers}{$h}{smry}
1052 3 ▶ ? ""
Branch: 2 branches
BranchTrueFalseTotal
exists $o->{'handlers'}{$h}{'help'} ? : 2 1 3
exists $o->{'handlers'}{$h}{'smry'} ? : 2 0 2
1053 : " - but help available"
1054 )
1055 : " - no help available";
1056 3 $dest->{" $h"} = "$smry$help";
1057 }
1058 1 my @t;
1059 1 ▶ push @t, " Commands:\n" if %cmds;
Branch: 1 branch
BranchTrueFalseTotal
if %cmds 1 0 1
1060 push @t,
1061 scalar $o->format_pairs(
1062 [ sort keys %cmds ],
1063 1 [ map { $cmds{$_} } sort keys %cmds ],
1064 ' - ', 1
1065 );
1066 1 ▶ push @t, " Extra Help Topics: (not commands)\n" if %docs;
Branch: 1 branch
BranchTrueFalseTotal
if %docs 0 1 1
1067 push @t,
1068 scalar $o->format_pairs(
1069 [ sort keys %docs ],
1070 1 [ map { $docs{$_} } sort keys %docs ],
1071 ' - ', 1
1072 );
1073 1 $o->page( join '', @t );
1074 }
1075 }
1076  
1077 ▶sub run_ { }
Subroutine run_: not called CC=1 SCAR=0.0
Pod undocumented
1078  
1079 sub comp_
1080 {
1081 0 ▶ my ( $o, $word, $line, $start ) = @_;
Subroutine comp_: not called CC=1 SCAR=6.9
Pod undocumented
1082 0 my @comp = grep { length($_) } sort $o->possible_actions( $word, 'run' );
1083 0 return @comp;
1084 }
1085  
1086 package Term::Shell::OnScopeLeave;
1087  
1088 sub new
1089 {
1090 2 ▶ return bless [ @_[ 1 .. $#_ ] ], ref( $_[0] ) || $_[0];
Subroutine new: called CC=2 SCAR=9.2
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
1091 }
1092  
1093 sub DESTROY
1094 {
1095 2 ▶ my $o = shift;
Subroutine DESTROY: called CC=2 SCAR=6.9
1096 2 for my $c (@$o)
1097 {
1098 2 $c->();
1099 }
1100  
1101 2 return;
1102 }
1103  
1104 4 1;
1105  
1106 __END__
↑ Summary Build »