Ask/Caroline.pm

filter: Statementstmt 18.4% 31 / 168 Branchbran 2.2% 2 / 90 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 36.0% 9 / 25 Podpod 0.0% 0 / 12 totaltotal 12.1% 42 / 345 CC 61 SCAR 49.5
CC
61
Cov
11%
file_selection
342.0
multiple_choice
272.0
__ANON__
72.0
CRAP 141.1
« Ask/Question.pm ↑ Summary Ask/Clui.pm »
1 3 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Ask::Caroline;
6  
7 3 our $AUTHORITY = 'cpan:TOBYINK';
8 3 our $VERSION = '0.015';
9  
10 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Caroline ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Path::Tiny 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Term::ANSIColor 'colored';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 with 'Ask::API';
17  
18 has caroline => (
19 is => 'lazy',
20 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 0 my $self = shift;
22 0 Scalar::Util::weaken( my $weak = $self );
23 'Caroline'->new(
24 0 ▶ completion_callback => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
25 0 ▶ return unless $weak && $weak->has_completion;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak and $weak->has_completion 0 0 0
Condition0%0/3
$weak and $weak->has_completion
!ll&&!rl&&r
000
MC/DC0%0/2
$weak and $weak->has_completion
$weak $weak->has_completion
Truth table0%0/3
A and B
A= $weakB= $weak->has_completion
ABresult
0X0
100
111
26 0 $weak->completion->( @_ );
27 },
28 0 );
29 },
30 3 );
31  
32 3 has completion => ( is => 'rw', predicate => 1, clearer => 1 );
33  
34 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
35 0 STDOUT->autoflush( 1 );
36 }
37  
38 0 ▶sub is_usable {
Subroutine is_usable: not called CC=2 SCAR=17.9
Pod undocumented
39 0 my ( $self ) = @_;
40 0 ▶ -t STDIN and -t STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
-t STDIN and -t STDOUT 0 0 0
41 }
42  
43 9 ▶sub quality {
Subroutine quality: called CC=3 SCAR=12.0
Pod undocumented
44 9 my ( $self ) = ( shift );
45
46 9 ▶ ( ref( $self ) ? $self : 'Caroline'->new )->is_supported ? 90 : 30;
Branch: 2 branches
BranchTrueFalseTotal
(ref $self ? $self : 'Caroline'->new)->is_supported ? : 9 0 9
ref $self ? : 0 9 9
47 }
48  
49 0 ▶sub entry {
Subroutine entry: not called CC=7 SCAR=40.3
Pod undocumented
50 0 my ( $self, %opts ) = ( shift, @_ );
51 0 ▶ $opts{prompt} = 'entry> ' unless exists $opts{prompt};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"prompt"} 0 0 0
52
53 0 ▶ if ( exists $opts{completion} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{'completion'}) { } 0 0 0
54 0 $self->completion( $opts{completion} );
55 }
56 else {
57 $self->completion(
58 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 0 return $opts{default};
60 }
61 0 );
62 }
63
64 0 ▶ if ( exists $opts{text} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"text"}) 0 0 0
65 $self->info(
66 text => $opts{text},
67 0 ▶ colour => $opts{colour} || 'bright_cyan',
Condition0%0/2
$opts{'colour'} || 'bright_cyan'
l!l
00
MC/DC0%0/1
$opts{'colour'} || 'bright_cyan'
$opts{'colour'}
Truth table0%0/2
A
A= $opts{'colour'}
Aresult
11
00
68 );
69 }
70
71 0 my ( $line, $tio );
72
73 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'hide_text'} and do { require POSIX; $tio = 'POSIX::Termios'->new }) { } 0 0 0
Condition0%0/3
$opts{"hide_text"} and do { require POSIX; $tio = "POSIX::Termios"->new }
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"hide_text"} and do { require POSIX; $tio = "POSIX::Termios"->new }
$opts{"hide_text"} do { require POSIX; $tio = "POSIX::Termios"->new }
Truth table0%0/3
A and B
A= $opts{"hide_text"}B= do { require POSIX; $tio = "POSIX::Termios"->new }
ABresult
0X0
100
111
74 $opts{hide_text}
75 0 and do { require POSIX; $tio = 'POSIX::Termios'->new }
76 )
77 {
78 0 $tio->getattr( 0 );
79 0 $tio->setlflag( $tio->getlflag & ~POSIX::ECHO() );
80 0 $tio->setattr( 0 );
81 0 print STDOUT $opts{prompt}; # no new line;
82 0 STDOUT->flush;
83 0 chomp( $line = <STDIN> );
84 0 $tio->setlflag( $tio->getlflag | POSIX::ECHO() );
85 0 $tio->setattr( 0 );
86 0 print STDOUT "\r\n";
87 0 STDOUT->flush;
88 } #/ if ( $opts{hide_text} ...)
89 else {
90 0 chomp( $line = $self->caroline->readline( $opts{prompt} ) );
91 }
92
93 0 $self->clear_completion;
94
95 0 return $line;
96 } #/ sub entry
97  
98 0 ▶sub question {
Subroutine question: not called CC=2 SCAR=17.9
Pod undocumented
99 0 my ( $self, %opts ) = ( shift, @_ );
100 0 ▶ $opts{prompt} = 'y/n> ' unless exists $opts{prompt};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"prompt"} 0 0 0
101
102 0 my $response = $self->entry( %opts );
103 0 my $lang = $self->_lang_support( $opts{lang} );
104 0 $lang->boolean( $response );
105 }
106  
107 0 ▶sub print_findings {
Subroutine print_findings: not called CC=7 SCAR=40.3
Pod undocumented
108 0 my ( $self, $findings, $fh ) = ( shift, @_ );
109
110 0 ▶ if ( my @copy = @$findings ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@copy) = @$findings) 0 0 0
111 0 print {$fh} "\r\n";
112 0 my $longest = 0;
113 0 ▶ for ( @copy ) { $longest = length if length > $longest }
Branch: 1 branch
BranchTrueFalseTotal
if length $_ > $longest 0 0 0
114 0 ▶ my $per_line = int( 80 / ( $longest + 2 ) ) || 1;
Condition0%0/2
int 80 / ($longest + 2) || 1
l!l
00
MC/DC0%0/1
int 80 / ($longest + 2) || 1
int 80 / ($longest + 2)
Truth table0%0/2
A
A= int 80 / ($longest + 2)
Aresult
11
00
115 0 my $template = '%-' . ( $longest + 2 ) . 's';
116 0 ▶ while ( @copy ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@copy) 0 0 0
117 0 my @chunk = splice @copy, 0, $per_line;
118 0 ▶ while ( @chunk < $per_line ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chunk < $per_line) 0 0 0
119 0 push @chunk, '';
120 }
121 0 printf {$fh} $template x $per_line, @chunk;
122 0 print {$fh} "\r\n";
123 }
124 0 $fh->flush;
125 0 return 1;
126 } #/ if ( my @copy = @$findings)
127
128 0 return;
129 } #/ sub print_findings
130  
131 0 ▶sub file_selection {
Subroutine file_selection: not called CC=18 SCAR=58.3
Pod undocumented
132 0 my ( $self, %opts ) = ( shift, @_ );
133
134 0 my $single = !$opts{multiple};
135
136 $opts{prompt} = sprintf( '%s> ', $opts{directory} ? 'directory' : 'file' )
137 0 ▶ unless exists $opts{prompt};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $opts{"prompt"} 0 0 0
$opts{'directory'} ? : 0 0 0
138
139 0 ▶ unless ( $opts{text} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"text"}) 0 0 0
140 $opts{text} =
141 $single
142 ? (
143 $opts{directory} ? 'Please choose a directory.' : 'Please choose a file.' )
144 : (
145 $opts{directory}
146 0 ▶ ? 'Please choose some directories.'
Branch: 3 branches
BranchTrueFalseTotal
$single ? : 0 0 0
$opts{'directory'} ? : 0 0 0
$opts{'directory'} ? : 0 0 0
147 : 'Please choose some files.'
148 );
149 } #/ unless ( $opts{text} )
150
151 $self->info(
152 text => $opts{text},
153 0 ▶ colour => $opts{colour} || 'bright_cyan',
Condition0%0/2
$opts{'colour'} || 'bright_cyan'
l!l
00
MC/DC0%0/1
$opts{'colour'} || 'bright_cyan'
$opts{'colour'}
Truth table0%0/2
A
A= $opts{'colour'}
Aresult
11
00
154 );
155
156 $self->info(
157 text =>
158 'Please enter one choice per line; leave a blank line to stop choosing.',
159 0 ▶ colour => $opts{colour} || 'bright_cyan',
Branch: 1 branch
BranchTrueFalseTotal
unless $single 0 0 0
Condition0%0/2
$opts{'colour'} || 'bright_cyan'
l!l
00
MC/DC0%0/1
$opts{'colour'} || 'bright_cyan'
$opts{'colour'}
Truth table0%0/2
A
A= $opts{'colour'}
Aresult
11
00
160 ) unless $single;
161
162 $self->completion(
163 0 ▶ sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
164 0 my $raw = shift;
165 0 ▶ $raw = '.' unless length $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless length $raw 0 0 0
166 0 my $got = path( $raw );
167
168 0 my @kids;
169
170 0 ▶ if ( $got->is_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got->is_dir) { } 0 0 0
171 0 ▶ @kids = $opts{directory} ? grep( $_->is_dir, $got->children ) : $got->children;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'directory'} ? : 0 0 0
172 }
173 else {
174 0 my $dir = $got->parent;
175 0 my $stem = quotemeta( $got->basename );
176
177 0 ▶ @kids = $opts{directory} ? grep( $_->is_dir, $dir->children ) : $dir->children;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'directory'} ? : 0 0 0
178 0 @kids = grep $_->basename =~ /^$stem/, @kids;
179 }
180
181 0 ▶ my @printable = sort map $_->basename . ( $_->is_dir ? '/' : '' ), @kids;
Branch: 1 branch
BranchTrueFalseTotal
$_->is_dir ? : 0 0 0
182 0 ▶ unshift @printable, '.' if $got->is_dir;
Branch: 1 branch
BranchTrueFalseTotal
if $got->is_dir 0 0 0
183
184 0 $self->print_findings( \@printable, \*STDOUT );
185 0 ▶ return map "$_", ( $got->is_dir ? $got : () ), @kids;
Branch: 1 branch
BranchTrueFalseTotal
$got->is_dir ? : 0 0 0
186 }
187 0 );
188
189 0 my @chosen;
190
191 0 CHOICE: while ( 1 ) {
192
193 0 chomp( my $line = $self->caroline->readline( $opts{prompt} ) );
194
195 0 ▶ if ( $line eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "") 0 0 0
196 0 ▶ $single ? next( CHOICE ) : last( CHOICE );
Branch: 1 branch
BranchTrueFalseTotal
$single ? : 0 0 0
197 }
198
199 0 ▶ if ( $opts{existing} and not path( $line )->exists ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'existing'} and not path($line)->exists) { } 0 0 0
elsif ($opts{'directory'} and not path($line)->is_dir) { } 0 0 0
Condition0%0/3
$opts{"existing"} and not path($line)->exists
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"existing"} and not path($line)->exists
$opts{"existing"} not path($line)->exists
Truth table0%0/3
A and not(B)
A= $opts{"existing"}B= not path($line)->exists
ABresult
0X0
100
111
200 0 $self->error(
201 text => sprintf( 'Does not exist: %s. Please try again.', $line ),
202 );
203 }
204 ▶ elsif ( $opts{directory} and not path( $line )->is_dir ) {
Condition0%0/3
$opts{"directory"} and not path($line)->is_dir
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"directory"} and not path($line)->is_dir
$opts{"directory"} not path($line)->is_dir
Truth table0%0/3
A and not(B)
A= $opts{"directory"}B= not path($line)->is_dir
ABresult
0X0
100
111
205 0 $self->error(
206 text => sprintf( 'Not a directory: %s. Please try again.', $line ),
207 );
208 }
209 else {
210 0 push @chosen, $line;
211 0 ▶ last CHOICE if $single;
Branch: 1 branch
BranchTrueFalseTotal
if $single 0 0 0
212 }
213 } #/ CHOICE: while ( 1 )
214
215 0 ▶ return $chosen[0] if $single;
Branch: 1 branch
BranchTrueFalseTotal
if $single 0 0 0
216
217 0 $self->clear_completion;
218
219 0 return @chosen;
220 } #/ sub file_selection
221  
222 0 ▶sub single_choice {
Subroutine single_choice: not called CC=1 SCAR=6.9
Pod undocumented
223 0 shift->multiple_choice( @_, _single => 1 );
224 }
225  
226 0 ▶sub multiple_choice {
Subroutine multiple_choice: not called CC=16 SCAR=56.1
Pod undocumented
227 0 my ( $self, %opts ) = ( shift, @_ );
228
229 0 my $single = $opts{_single};
230
231 0 ▶ $opts{prompt} = 'choice> ' unless exists $opts{prompt};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"prompt"} 0 0 0
232
233 0 ▶ if ( exists $opts{text} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"text"}) 0 0 0
234 $self->info(
235 text => $opts{text},
236 0 ▶ colour => $opts{colour} || 'bright_cyan',
Condition0%0/2
$opts{'colour'} || 'bright_cyan'
l!l
00
MC/DC0%0/1
$opts{'colour'} || 'bright_cyan'
$opts{'colour'}
Truth table0%0/2
A
A= $opts{'colour'}
Aresult
11
00
237 );
238 }
239
240 0 my %allowed;
241 my @choices_list = map {
242 0 $allowed{ $_->[0] } = 1;
243 0 ▶ defined $_->[1] ? sprintf( '"%s" (%s)', @$_ ) : "\"$_->[0]\""
Branch: 1 branch
BranchTrueFalseTotal
defined $_->[1] ? : 0 0 0
244 0 } @{ $opts{choices} };
245
246 $self->info(
247 text => sprintf( 'Choices: %s.', join( q[, ], @choices_list ) ),
248 colour => $opts{colour} || 'bright_cyan',
249 0 ▶ ) unless $opts{hide_choices};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"hide_choices"} 0 0 0
Condition0%0/2
$opts{'colour'} || 'bright_cyan'
l!l
00
MC/DC0%0/1
$opts{'colour'} || 'bright_cyan'
$opts{'colour'}
Truth table0%0/2
A
A= $opts{'colour'}
Aresult
11
00
250
251 $self->info(
252 text =>
253 'Please enter one choice per line; leave a blank line to stop choosing.',
254 0 ▶ colour => $opts{colour} || 'bright_cyan',
Branch: 1 branch
BranchTrueFalseTotal
unless $single 0 0 0
Condition0%0/2
$opts{'colour'} || 'bright_cyan'
l!l
00
MC/DC0%0/1
$opts{'colour'} || 'bright_cyan'
$opts{'colour'}
Truth table0%0/2
A
A= $opts{'colour'}
Aresult
11
00
255 ) unless $single;
256
257 $self->completion(
258 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
259 0 my $got = quotemeta( shift );
260 0 my @found = grep /^$got/, map $_->[0], @{ $opts{choices} };
261 0 $self->print_findings( \@found, \*STDOUT );
262 0 return @found;
263 }
264 0 );
265
266 0 my @chosen;
267
268 0 CHOICE: while ( 1 ) {
269
270 0 chomp( my $line = $self->caroline->readline( $opts{prompt} ) );
271
272 0 ▶ if ( $line eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "") 0 0 0
273 0 ▶ $single ? next( CHOICE ) : last( CHOICE );
Branch: 1 branch
BranchTrueFalseTotal
$single ? : 0 0 0
274 }
275
276 0 ▶ if ( $allowed{$line} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allowed{$line}) { } 0 0 0
277 0 push @chosen, $line;
278 0 ▶ last CHOICE if $single;
Branch: 1 branch
BranchTrueFalseTotal
if $single 0 0 0
279 }
280 else {
281 0 $self->error(
282 text => sprintf( 'Not valid: %s. Please try again.', $line ),
283 );
284 $self->info(
285 text => sprintf( 'Choices: %s.', join( q[, ], @choices_list ) ),
286 colour => $opts{colour} || 'bright_cyan',
287 0 ▶ ) unless $opts{hide_choices};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"hide_choices"} 0 0 0
Condition0%0/2
$opts{'colour'} || 'bright_cyan'
l!l
00
MC/DC0%0/1
$opts{'colour'} || 'bright_cyan'
$opts{'colour'}
Truth table0%0/2
A
A= $opts{'colour'}
Aresult
11
00
288 }
289 } #/ CHOICE: while ( 1 )
290
291 0 ▶ return $chosen[0] if $single;
Branch: 1 branch
BranchTrueFalseTotal
if $single 0 0 0
292
293 0 $self->clear_completion;
294
295 0 return @chosen;
296 } #/ sub multiple_choice
297  
298 0 ▶sub info {
Subroutine info: not called CC=2 SCAR=17.9
Pod undocumented
299 0 my ( $self, %opts ) = ( shift, @_ );
300 0 chomp( my $text = $opts{text} );
301 0 ▶ if ( $opts{colour} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"colour"}) 0 0 0
302 0 $text = colored( [ $opts{colour} ], $text );
303 }
304 0 print STDOUT $text, "\n";
305 0 STDOUT->flush;
306 }
307  
308 0 ▶sub warning {
Subroutine warning: not called CC=2 SCAR=17.9
Pod undocumented
309 0 my ( $self, %opts ) = ( shift, @_ );
310 0 ▶ $opts{colour} ||= 'bright_yellow';
Condition0%0/2
$opts{"colour"} ||= "bright_yellow"
l!l
00
MC/DC0%0/1
$opts{"colour"} ||= "bright_yellow"
$opts{"colour"}
Truth table0%0/2
A
A= $opts{"colour"}
Aresult
11
00
311 0 $self->info( %opts );
312 }
313  
314 0 ▶sub error {
Subroutine error: not called CC=2 SCAR=17.9
Pod undocumented
315 0 my ( $self, %opts ) = ( shift, @_ );
316 0 ▶ $opts{colour} ||= 'bright_red';
Condition0%0/2
$opts{"colour"} ||= "bright_red"
l!l
00
MC/DC0%0/1
$opts{"colour"} ||= "bright_red"
$opts{"colour"}
Truth table0%0/2
A
A= $opts{"colour"}
Aresult
11
00
317 0 $self->info( %opts );
318 }
319  
320 3 1;
321  
322 __END__
« Ask/Question.pm ↑ Summary Ask/Clui.pm »