blib/lib/Term/Choose/Util.pm

filter: Statementstmt 8.5% 50 / 584 Branchbran 0.0% 0 / 296 Conditioncond 0.0% 0 / 70 MC/DCmcdc 0.0% 0 / 42 Subroutinesub 41.0% 16 / 39 Podpod 53.8% 7 / 13 totaltotal 6.9% 73 / 1044 CC 191 SCAR 58.3
CC
191
Cov
5%
choose_a_number
702.0
choose_a_subset
702.0
settings_menu
600.0
CRAP 339.5
↑ Summary
1 package Term::Choose::Util;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.148';
8 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 our @EXPORT_OK = qw( choose_a_directory choose_a_file choose_directories choose_a_number choose_a_subset settings_menu
10 insert_sep get_term_size get_term_width get_term_height unicode_sprintf );
11  
12 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Cwd qw( realpath );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Encode qw( decode encode );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Basename qw( basename dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use File::Spec::Functions qw( catdir catfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use List::Util qw( sum any );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use Encode::Locale qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use File::HomeDir qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use Term::Choose qw( choose );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Term::Choose::Constants qw( SGR_ES PH );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Term::Choose::LineFold qw( cut_to_printwidth print_columns );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Term::Choose::ValidateOptions qw( validate_options );
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27  
28 sub new {
29 0 ▶ my $class = shift;
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
30 0 my ( $opt ) = @_;
31 0 my $instance_defaults = _defaults();
32 0 ▶ if ( defined $opt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt) 0 0 0
33 0 ▶ croak "Options have to be passed as a HASH reference." if ref $opt ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opt ne "HASH" 0 0 0
34 0 my $caller = 'new';
35 0 validate_options( _valid_options( $caller ), $opt, $caller );
36 0 for my $key ( keys %$opt ) {
37 0 ▶ $instance_defaults->{$key} = $opt->{$key} if defined $opt->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{$key} 0 0 0
38 }
39 }
40 0 my $self = bless $instance_defaults, $class;
41 0 $self->{backup_instance_defaults} = { %$instance_defaults };
42 0 return $self;
43 }
44  
45  
46 sub __restore_defaults {
47 0 ▶ my ( $self ) = @_;
Subroutine __restore_defaults: not called CC=5 SCAR=34.0
48 0 ▶ if ( exists $self->{backup_instance_defaults} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"backup_instance_defaults"}) 0 0 0
49 0 my $instance_defaults = $self->{backup_instance_defaults};
50 0 for my $key ( keys %$self ) {
51 0 ▶ if ( $key eq 'backup_instance_defaults' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'backup_instance_defaults') { } 0 0 0
elsif (exists $instance_defaults->{$key}) { } 0 0 0
52 0 next;
53 }
54 elsif ( exists $instance_defaults->{$key} ) {
55 0 $self->{$key} = $instance_defaults->{$key};
56 }
57 else {
58 0 delete $self->{$key};
59 }
60 }
61 }
62 }
63  
64  
65 sub __prepare_opt {
66 0 ▶ my ( $self, $opt, $subseq_tab ) = @_;
Subroutine __prepare_opt: not called CC=19 SCAR=59.4
67 0 ▶ if ( ! defined $opt ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opt) 0 0 0
68 0 $opt = {};
69 }
70 0 ▶ croak "Options have to be passed as a HASH reference." if ref $opt ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opt ne "HASH" 0 0 0
71 0 ▶ if ( %$opt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$opt) 0 0 0
72 0 my $caller = ( caller( 1 ) )[3];
73 0 $caller =~ s/^.+::(?:__)?([^:]+)\z/$1/;
74 0 validate_options( _valid_options( $caller ), $opt, $caller );
75 0 my $defaults = _defaults();
76 0 for my $key ( keys %$opt ) {
77 0 ▶ if ( ! defined $opt->{$key} && defined $defaults->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opt->{$key} and defined $defaults->{$key}) { } 0 0 0
Condition0%0/3
not defined $opt->{$key} and defined $defaults->{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $opt->{$key} and defined $defaults->{$key}
not defined $opt->{$key} defined $defaults->{$key}
Truth table0%0/3
not(A) and B
A= not defined $opt->{$key}B= defined $defaults->{$key}
ABresult
0X0
100
111
78 0 $self->{$key} = $defaults->{$key};
79 }
80 else {
81 0 $self->{$key} = $opt->{$key};
82 }
83 }
84 }
85 0 ▶ if ( ! defined $self->{tabs_info} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"tabs_info"}) 0 0 0
86 0 ▶ if ( defined $self->{margin} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"margin"}) 0 0 0
87 0 ▶ $self->{tabs_info} = [ $self->{margin}[3] // 0, $self->{margin}[3] // 0, $self->{margin}[1] // 0 ];
Condition0%0/6
$self->{"margin"}[3] // 0
l!l
00
$self->{"margin"}[3] // 0
l!l
00
$self->{"margin"}[1] // 0
l!l
00
MC/DC0%0/1
$self->{"margin"}[3] // 0
$self->{"margin"}[3]
MC/DC0%0/1
$self->{"margin"}[3] // 0
$self->{"margin"}[3]
MC/DC0%0/1
$self->{"margin"}[1] // 0
$self->{"margin"}[1]
Truth table0%0/2
A
A= $self->{"margin"}[3]
Aresult
11
00
Truth table0%0/2
A
A= $self->{"margin"}[3]
Aresult
11
00
Truth table0%0/2
A
A= $self->{"margin"}[1]
Aresult
11
00
88 }
89 }
90 0 ▶ if ( ! defined $self->{tabs_prompt} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"tabs_prompt"}) 0 0 0
91 0 ▶ if ( defined $self->{margin} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'margin'}) { } 0 0 0
elsif ($subseq_tab) { } 0 0 0
92 $self->{tabs_prompt} = [
93 $self->{margin}[3] // 0,
94 ( $self->{margin}[3] // 0 ) + ( $subseq_tab // 0 ),
95 0 ▶ $self->{margin}[1] // 0
Condition0%0/8
$self->{"margin"}[3] // 0
l!l
00
$self->{"margin"}[3] // 0
l!l
00
$subseq_tab // 0
l!l
00
$self->{"margin"}[1] // 0
l!l
00
MC/DC0%0/1
$self->{"margin"}[3] // 0
$self->{"margin"}[3]
MC/DC0%0/1
$self->{"margin"}[3] // 0
$self->{"margin"}[3]
MC/DC0%0/1
$subseq_tab // 0
$subseq_tab
MC/DC0%0/1
$self->{"margin"}[1] // 0
$self->{"margin"}[1]
Truth table0%0/2
A
A= $self->{"margin"}[3]
Aresult
11
00
Truth table0%0/2
A
A= $self->{"margin"}[3]
Aresult
11
00
Truth table0%0/2
A
A= $subseq_tab
Aresult
11
00
Truth table0%0/2
A
A= $self->{"margin"}[1]
Aresult
11
00
96 ];
97 }
98 elsif ( $subseq_tab ) {
99 0 $self->{tabs_prompt} = [ 0, $subseq_tab, 0 ];
100 }
101 }
102 }
103  
104  
105 sub _valid_options {
106 0 ▶ my ( $caller ) = @_;
Subroutine _valid_options: not called CC=2 SCAR=17.9
107 0 my %valid = (
108 all_by_default => '[ 0 1 ]',
109 clear_screen => '[ 0 1 ]',
110 decoded => '[ 0 1 ]',
111 hide_cursor => '[ 0 1 ]',
112 index => '[ 0 1 ]',
113 keep_chosen => '[ 0 1 ]',
114 mouse => '[ 0 1 ]',
115 order => '[ 0 1 ]',
116 show_hidden => '[ 0 1 ]',
117 small_first => '[ 0 1 ]',
118 alignment => '[ 0 1 2 ]',
119 color => '[ 0 1 2 ]',
120 layout => '[ 0 1 2 ]',
121 page => '[ 0 1 2 ]', # undocumented
122 keep => '[ 1-9 ][ 0-9 ]*', # undocumented
123 default_number => '[ 0-9 ]+',
124 margin => 'Array_Int',
125 mark => 'Array_Int_Idx',
126 tabs_info => 'Array_Int',
127 tabs_prompt => 'Array_Int',
128 busy_string => 'Str',
129 info => 'Str',
130 init_dir => 'Str',
131 back => 'Str',
132 filter => 'Str',
133 footer => 'Str', # undocumented
134 confirm => 'Str',
135 prefix => 'Str',
136 prompt => 'Str',
137 prompt2 => 'Str',
138 cs_begin => 'Str',
139 cs_end => 'Str',
140 cs_label => 'Str',
141 cs_separator => 'Str',
142 thousands_separator => 'Str',
143 );
144 0 my $options;
145 0 ▶ if ( $caller eq 'new' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($caller eq 'new') { } 0 0 0
146 0 $options = [ keys %valid ];
147 }
148 else {
149 0 $options = _routine_options( $caller );
150 }
151 0 return { map { $_ => $valid{$_} } @$options };
152 };
153  
154  
155 sub _defaults {
156 return {
157 0 ▶ alignment => 0,
Subroutine _defaults: not called CC=1 SCAR=6.9
158 all_by_default => 0,
159 back => 'BACK',
160 #busy_string => undef,
161 clear_screen => 0,
162 color => 0,
163 confirm => 'CONFIRM',
164 cs_begin => '',
165 cs_end => '',
166 #cs_label => undef,
167 cs_separator => ', ',
168 decoded => 1,
169 #default_number => undef,
170 #filter => undef,
171 #footer => undef,
172 hide_cursor => 1,
173 index => 0,
174 #info => undef,
175 #init_dir => undef,
176 keep_chosen => 0,
177 #keep => undef,
178 layout => 1,
179 #margin => undef,
180 #mark => undef,
181 mouse => 0,
182 order => 1,
183 #page => undef,
184 parent_dir => '..',
185 prefix => '',
186 prompt => 'Your choice: ',
187 #prompt2 => undef,
188 show_hidden => 1,
189 small_first => 0,
190 #tabs_info => undef,
191 #tabs_prompt => undef,
192 thousands_separator => ',',
193  
194 ## intern:
195 reset => 'reset',
196 };
197 };
198  
199  
200 sub _routine_options {
201 0 ▶ my ( $caller ) = @_;
Subroutine _routine_options: not called CC=7 SCAR=40.3
202 0 my @every = ( qw( back clear_screen color confirm cs_label footer hide_cursor info keep margin mouse page prompt tabs_info tabs_prompt ) );
203 0 my $options;
204 0 ▶ if ( $caller eq 'choose_directories' ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($caller eq 'choose_directories') { } 0 0 0
elsif ($caller eq 'choose_a_directory') { } 0 0 0
elsif ($caller eq 'choose_a_file') { } 0 0 0
elsif ($caller eq 'choose_a_number') { } 0 0 0
elsif ($caller eq 'choose_a_subset') { } 0 0 0
elsif ($caller eq 'settings_menu') { } 0 0 0
205 0 $options = [ @every, qw( init_dir layout order alignment show_hidden decoded prompt2 ) ];
206 }
207 elsif ( $caller eq 'choose_a_directory' ) {
208 0 $options = [ @every, qw( init_dir layout order alignment show_hidden decoded ) ];
209 }
210 elsif ( $caller eq 'choose_a_file' ) {
211 0 $options = [ @every, qw( init_dir layout order alignment show_hidden decoded filter prompt2 ) ];
212 }
213 elsif ( $caller eq 'choose_a_number' ) {
214 0 $options = [ @every, qw( small_first reset thousands_separator default_number ) ];
215 }
216 elsif ( $caller eq 'choose_a_subset' ) {
217 0 $options = [ @every, qw( layout order alignment keep_chosen index prefix all_by_default cs_begin cs_end cs_separator mark busy_string ) ];
218 }
219 elsif ( $caller eq 'settings_menu' ) {
220 0 $options = [ @every, qw( cs_begin cs_end cs_separator ) ];
221 }
222 0 return $options;
223 }
224  
225  
226 sub __prepare_path {
227 0 ▶ my ( $self ) = @_;
Subroutine __prepare_path: not called CC=5 SCAR=34.0
228 0 my $init_dir_fs;
229 0 ▶ if ( defined $self->{init_dir} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'init_dir'}) { } 0 0 0
230 0 $init_dir_fs = encode( 'locale_fs', $self->{init_dir} );
231 0 ▶ if ( ! -d $init_dir_fs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $init_dir_fs) 0 0 0
232 0 my $prompt = 'Could not find the directory "';
233 0 $prompt .= decode 'locale_fs', $init_dir_fs;
234 0 $prompt .= '". Falling back to the home directory.';
235 # Choose
236 choose(
237 [ 'Press ENTER to continue' ],
238 { prompt => $prompt, hide_cursor => $self->{hide_cursor}, mouse => $self->{mouse}, page => $self->{page},
239 footer => $self->{footer}, keep => $self->{keep}, margin => $self->{margin} }
240 0 );
241 0 $init_dir_fs = File::HomeDir->my_home();
242 }
243 }
244 else {
245 0 $init_dir_fs = File::HomeDir->my_home();
246 }
247 0 ▶ if ( ! -d $init_dir_fs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $init_dir_fs) 0 0 0
248 0 croak "Could not find the home directory.";
249 }
250 0 ▶ my $dir_fs = realpath( $init_dir_fs ) or die "$init_dir_fs: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $dir_fs = realpath($init_dir_fs) or die "${init_dir_fs}: $!" 0 0 0
251 0 my $dir = decode( 'locale_fs', $dir_fs );
252 0 return $dir;
253 }
254  
255  
256 sub __available_dirs {
257 0 ▶ my ( $self, $dir ) = @_;
Subroutine __available_dirs: not called CC=8 SCAR=42.8
258 0 my $dir_fs = encode( 'locale_fs', $dir );
259 0 my $dh;
260 0 ▶ if ( ! eval {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { opendir $dh, $dir_fs or croak("${dir_fs}: $!"); 1 } }) 0 0 0
261 0 ▶ opendir( $dh, $dir_fs ) or croak "$dir_fs: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir $dh, $dir_fs or croak("${dir_fs}: $!") 0 0 0
262 0 1 }
263 ) {
264 0 print "$@";
265 # Choose
266 choose(
267 [ 'Press Enter:' ],
268 { prompt => '', hide_cursor => $self->{hide_cursor}, mouse => $self->{mouse}, page => $self->{page},
269 footer => $self->{footer}, keep => $self->{keep}, margin => $self->{margin} }
270 0 );
271 0 return;
272 }
273 0 my @dirs;
274 0 ▶ while ( my $file_fs = readdir $dh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file_fs = readdir $dh)) 0 0 0
275 0 ▶ next if $file_fs =~ /^\.\.?\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $file_fs =~ /^\.\.?\z/ 0 0 0
276 0 ▶ next if $file_fs =~ /^\./ && ! $self->{show_hidden};
Branch: 1 branch
BranchTrueFalseTotal
if $file_fs =~ /^\./ and not $self->{"show_hidden"} 0 0 0
Condition0%0/3
$file_fs =~ /^\./ and not $self->{"show_hidden"}
!ll&&!rl&&r
000
MC/DC0%0/2
$file_fs =~ /^\./ and not $self->{"show_hidden"}
$file_fs =~ /^\./ not $self->{"show_hidden"}
Truth table0%0/3
A and not(B)
A= $file_fs =~ /^\./B= not $self->{"show_hidden"}
ABresult
0X0
100
111
277 0 ▶ if ( -d catdir $dir_fs, $file_fs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d catdir($dir_fs, $file_fs)) 0 0 0
278 0 push @dirs, decode( 'locale_fs', $file_fs );
279 }
280 }
281 0 closedir $dh;
282 0 return [ sort @dirs ];
283 }
284  
285  
286 sub choose_directories {
287 0 ▶ if ( ref $_[0] ne __PACKAGE__ ) {
Subroutine choose_directories: not called CC=18 SCAR=58.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] ne __PACKAGE__) 0 0 0
288 0 my $ob = __PACKAGE__->new();
289 0 delete $ob->{backup_instance_defaults};
290 0 return $ob->choose_directories( @_ );
291 }
292 0 my ( $self, $opt ) = @_;
293 0 my $subseq_tab = 2;
294 0 $self->__prepare_opt( $opt, $subseq_tab );
295 0 my $dir = $self->__prepare_path();
296 0 my $chosen_dirs = [];
297 0 my ( $confirm, $change_path, $add_dirs ) = ( $self->{confirm}, '- Change Location', '- Add Directories' );
298 0 my @bu;
299  
300 0 CHOOSE_MODE: while ( 1 ) {
301 0 ▶ my $key_dirs = $self->{cs_label} // 'Chosen Dirs: ';
Condition0%0/2
$self->{"cs_label"} // "Chosen Dirs: "
l!l
00
MC/DC0%0/1
$self->{"cs_label"} // "Chosen Dirs: "
$self->{"cs_label"}
Truth table0%0/2
A
A= $self->{"cs_label"}
Aresult
11
00
302 0 ▶ my $dirs_chosen = $key_dirs . ( @$chosen_dirs ? join( ', ', @$chosen_dirs ) : '---' );
Branch: 1 branch
BranchTrueFalseTotal
@$chosen_dirs ? : 0 0 0
303 0 my $key_path = 'Location: ';
304 0 my $path = $key_path . $dir;
305 0 my $prompt = $dirs_chosen . "\n" . $path;
306 # Choose
307 my $choice = choose(
308 [ undef, $confirm, $change_path, $add_dirs ],
309 { info => $self->{info}, prompt => $prompt, layout => 2, mouse => $self->{mouse}, margin => $self->{margin},
310 clear_screen => $self->{clear_screen}, hide_cursor => $self->{hide_cursor}, page => $self->{page},
311 footer => $self->{footer}, keep => $self->{keep}, undef => $self->{back}, color => $self->{color},
312 tabs_info => $self->{tabs_info}, tabs_prompt => $self->{tabs_prompt} }
313 0 );
314 0 ▶ if ( ! defined $choice ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $choice) { } 0 0 0
elsif ($choice eq $confirm) { } 0 0 0
elsif ($choice eq $change_path) { } 0 0 0
elsif ($choice eq $add_dirs) { } 0 0 0
315 0 ▶ if ( @bu ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bu) 0 0 0
316 0 ( $dir, $chosen_dirs ) = @{pop @bu};
317 0 next CHOOSE_MODE;
318 }
319 0 $self->__restore_defaults(); #
320 0 return;
321 }
322 elsif ( $choice eq $confirm ) {
323 0 my $decoded = $self->{decoded};
324 0 $self->__restore_defaults();
325 0 ▶ return $decoded ? $chosen_dirs : [ map { encode 'locale_fs', $_ } @$chosen_dirs ];
Branch: 1 branch
BranchTrueFalseTotal
$decoded ? : 0 0 0
326 }
327 elsif ( $choice eq $change_path ) {
328 0 my $prompt_fmt = $key_path . "%s";
329 0 ▶ if ( length $self->{prompt} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"prompt"}) 0 0 0
330 0 $prompt_fmt .= "\n" . $self->{prompt};
331 }
332 0 my $tmp_dir = $self->__choose_a_path( $dir, $prompt_fmt, '<<', 'OK' );
333 0 ▶ if ( defined $tmp_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tmp_dir) 0 0 0
334 0 $dir = $tmp_dir;
335 }
336 }
337 elsif ( $choice eq $add_dirs ) {
338 0 my $avail_dirs = $self->__available_dirs( $dir );
339 0 ▶ if ( ! defined $avail_dirs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $avail_dirs) 0 0 0
340 0 next CHOOSE_MODE;
341 }
342 0 my %bu_opt;
343 0 my $options = _routine_options( 'choose_directories' );
344 0 for my $o ( @$options ) {
345 0 $bu_opt{$o} = $self->{$o};
346 }
347 0 my $cs_label = $dirs_chosen . "\n" . $path . "\n" . 'Add: ';
348 0 ▶ my $prompt = $self->{prompt2} // $self->{prompt};
Condition0%0/3
$self->{"prompt2"} // $self->{"prompt"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"prompt2"} // $self->{"prompt"}
$self->{"prompt2"} $self->{"prompt"}
Truth table0%0/3
A // B
A= $self->{"prompt2"}B= $self->{"prompt"}
ABresult
1X1
011
000
349 # choose_a_subset
350 0 my $idxs = $self->choose_a_subset(
351 [ sort @$avail_dirs ],
352 { cs_label => $cs_label, back => '<<', confirm => 'OK', cs_begin => undef, index => 1, keep_chosen => 1,
353 prompt => $prompt, # $self->{option} alreday in $self:
354 #info => $self->{info}, page => $self->{page}, footer => $self->{footer}, keep => $self->{keep},
355 # margin => $self->{margin}, tabs_info => $self->{tabs_info}, tabs_prompt => $self->{tabs_prompt}
356 }
357 );
358 0 for my $o ( keys %bu_opt ) {
359 0 $self->{$o} = $bu_opt{$o};
360 }
361 0 ▶ if ( defined $idxs && @$idxs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $idxs and @$idxs) 0 0 0
Condition0%0/3
defined $idxs and @$idxs
!ll&&!rl&&r
000
MC/DC0%0/2
defined $idxs and @$idxs
defined $idxs @$idxs
Truth table0%0/3
A and B
A= defined $idxsB= @$idxs
ABresult
0X0
100
111
362 0 push @bu, [ $dir, [ @$chosen_dirs ] ];
363 0 push @$chosen_dirs, map { catdir $dir, $_ } @{$avail_dirs}[@$idxs];
364 }
365 }
366 }
367 }
368  
369  
370 sub choose_a_file {
371 0 ▶ if ( ref $_[0] ne __PACKAGE__ ) {
Subroutine choose_a_file: not called CC=6 SCAR=37.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] ne __PACKAGE__) 0 0 0
372 0 my $ob = __PACKAGE__->new();
373 0 delete $ob->{backup_instance_defaults};
374 0 return $ob->choose_a_file( @_ );
375 }
376 0 my ( $self, $opt ) = @_;
377 0 $self->__prepare_opt( $opt );
378 0 my $init_dir = $self->__prepare_path();
379 0 my $prompt_fmt = "Source directory: %s";
380 0 ▶ if ( length $self->{prompt} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"prompt"}) 0 0 0
381 0 $prompt_fmt .= "\n" . $self->{prompt};
382 }
383  
384 0 CHOOSE_DIR: while ( 1 ) {
385 0 my $chosen_dir = $self->__choose_a_path( $init_dir, $prompt_fmt, '<<', 'OK' );
386 0 ▶ if ( ! defined $chosen_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $chosen_dir) 0 0 0
387 0 $self->__restore_defaults(); #
388 0 return;
389 }
390 0 my $chosen_file = $self->__a_file( $chosen_dir );
391 0 ▶ if ( ! defined $chosen_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $chosen_file) 0 0 0
392 0 next CHOOSE_DIR;
393 }
394 0 my $decoded = $self->{decoded};
395 0 $self->__restore_defaults();
396 0 ▶ return $decoded ? $chosen_file : encode( 'locale_fs', $chosen_file );
Branch: 1 branch
BranchTrueFalseTotal
$decoded ? : 0 0 0
397 }
398 }
399  
400  
401 sub choose_a_directory {
402 0 ▶ if ( ref $_[0] ne __PACKAGE__ ) {
Subroutine choose_a_directory: not called CC=6 SCAR=37.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] ne __PACKAGE__) 0 0 0
403 0 my $ob = __PACKAGE__->new();
404 0 delete $ob->{backup_instance_defaults};
405 0 return $ob->choose_a_directory( @_ );
406 }
407 0 my ( $self, $opt ) = @_;
408 0 $self->__prepare_opt( $opt );
409 0 my $init_dir = $self->__prepare_path();
410 0 ▶ my $prompt_fmt = ( $self->{cs_label} // 'Directory: ' ) . "%s";
Condition0%0/2
$self->{"cs_label"} // "Directory: "
l!l
00
MC/DC0%0/1
$self->{"cs_label"} // "Directory: "
$self->{"cs_label"}
Truth table0%0/2
A
A= $self->{"cs_label"}
Aresult
11
00
411 0 ▶ if ( length $self->{prompt} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"prompt"}) 0 0 0
412 0 $prompt_fmt .= "\n" . $self->{prompt};
413 }
414 0 my $chosen_dir = $self->__choose_a_path( $init_dir, $prompt_fmt, $self->{back}, $self->{confirm} );
415 0 my $decoded = $self->{decoded};
416 0 $self->__restore_defaults();
417 0 ▶ if ( ! defined $chosen_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $chosen_dir) 0 0 0
418 0 return;
419 }
420 0 ▶ return $decoded ? $chosen_dir : encode( 'locale_fs', $chosen_dir );
Branch: 1 branch
BranchTrueFalseTotal
$decoded ? : 0 0 0
421 }
422  
423  
424 sub __choose_a_path {
425 0 ▶ my ( $self, $dir, $prompt_fmt, $back, $confirm ) = @_;
Subroutine __choose_a_path: not called CC=11 SCAR=48.8
426 0 my $prev_dir = $dir;
427  
428 0 while ( 1 ) {
429 0 my ( $dh, @dirs );
430 0 my $dir_fs = encode( 'locale_fs', $dir );
431 0 ▶ if ( ! eval {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { opendir $dh, $dir_fs or croak("${dir_fs}: $!"); 1 } }) 0 0 0
432 0 ▶ opendir( $dh, $dir_fs ) or croak "$dir_fs: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir $dh, $dir_fs or croak("${dir_fs}: $!") 0 0 0
433 0 1 }
434 ) {
435 0 print "$@";
436 # Choose
437 choose(
438 [ 'Press Enter:' ],
439 { prompt => '', hide_cursor => $self->{hide_cursor}, mouse => $self->{mouse}, page => $self->{page},
440 footer => $self->{footer}, keep => $self->{keep}, margin => $self->{margin} }
441 0 );
442 0 $dir = dirname $dir;
443 0 next;
444 }
445 0 ▶ while ( my $file_fs = readdir $dh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file_fs = readdir $dh)) 0 0 0
446 0 ▶ next if $file_fs =~ /^\.\.?\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $file_fs =~ /^\.\.?\z/ 0 0 0
447 0 ▶ next if $file_fs =~ /^\./ && ! $self->{show_hidden};
Branch: 1 branch
BranchTrueFalseTotal
if $file_fs =~ /^\./ and not $self->{"show_hidden"} 0 0 0
Condition0%0/3
$file_fs =~ /^\./ and not $self->{"show_hidden"}
!ll&&!rl&&r
000
MC/DC0%0/2
$file_fs =~ /^\./ and not $self->{"show_hidden"}
$file_fs =~ /^\./ not $self->{"show_hidden"}
Truth table0%0/3
A and not(B)
A= $file_fs =~ /^\./B= not $self->{"show_hidden"}
ABresult
0X0
100
111
448 0 ▶ if ( -d catdir $dir_fs, $file_fs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d catdir($dir_fs, $file_fs)) 0 0 0
449 0 push @dirs, decode( 'locale_fs', $file_fs );
450 }
451 }
452 0 closedir $dh;
453 0 my $parent_dir = $self->{parent_dir};
454 0 my @pre = ( undef, $confirm, $parent_dir );
455 0 my $prompt = sprintf $prompt_fmt, $prev_dir;
456 # Choose
457 my $choice = choose(
458 [ @pre, sort( @dirs ) ],
459 { info => $self->{info}, prompt => $prompt, alignment => $self->{alignment},
460 layout => $self->{layout}, order => $self->{order}, mouse => $self->{mouse},
461 clear_screen => $self->{clear_screen}, hide_cursor => $self->{hide_cursor}, margin => $self->{margin},
462 color => $self->{color}, tabs_info => $self->{tabs_info}, tabs_prompt => $self->{tabs_prompt},
463 0 page => $self->{page}, footer => $self->{footer}, keep => $self->{keep}, undef => $back }
464 );
465 0 ▶ if ( ! defined $choice ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $choice) { } 0 0 0
elsif ($choice eq $confirm) { } 0 0 0
elsif ($choice eq $parent_dir) { } 0 0 0
466 0 return;
467 }
468 elsif ( $choice eq $confirm ) {
469 0 return $prev_dir;
470 }
471 elsif ( $choice eq $parent_dir ) {
472 0 $dir = dirname $dir;
473 }
474 else {
475 0 $dir = catdir $dir, $choice;
476 }
477 0 $prev_dir = $dir;
478 }
479 }
480  
481  
482 sub __a_file {
483 0 ▶ my ( $self, $dir ) = @_;
Subroutine __a_file: not called CC=20 SCAR=60.4
484 0 my $prev_dir = '';
485 0 my $chosen_file;
486  
487 0 while ( 1 ) {
488 0 my @files_fs;
489 0 my $dir_fs = encode( 'locale_fs', $dir );
490 0 ▶ if ( ! eval {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { if ($self->{"filter"}) { @files_fs = map({basename($_);} grep({-e $_;} glob(&encode("locale_fs", scalar catfile($dir, $self->{"filter"}))))); } else { opendir my $dh, $dir_fs or croak("${dir_fs}: $!"); @files_fs = readdir $dh; closedir $dh; } ; 1 } }) 0 0 0
491 0 ▶ if ( $self->{filter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'filter'}) { } 0 0 0
492 0 @files_fs = map { basename $_} grep { -e $_ } glob( encode( 'locale_fs', catfile $dir, $self->{filter} ) );
493 }
494 else {
495 0 ▶ opendir( my $dh, $dir_fs ) or croak "$dir_fs: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir_fs or croak("${dir_fs}: $!") 0 0 0
496 0 @files_fs = readdir $dh;
497 0 closedir $dh;
498 }
499 0 1 }
500 ) {
501 0 print "$@";
502 # Choose
503 choose(
504 [ 'Press Enter:' ],
505 { prompt => '', hide_cursor => $self->{hide_cursor}, mouse => $self->{mouse}, page => $self->{page},
506 footer => $self->{footer}, keep => $self->{keep}, margin => $self->{margin} }
507 0 );
508 0 return;
509 }
510 0 my @files;
511 0 for my $file_fs ( @files_fs ) {
512 0 ▶ next if $file_fs =~ /^\.\.?\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $file_fs =~ /^\.\.?\z/ 0 0 0
513 0 ▶ next if $file_fs =~ /^\./ && ! $self->{show_hidden};
Branch: 1 branch
BranchTrueFalseTotal
if $file_fs =~ /^\./ and not $self->{"show_hidden"} 0 0 0
Condition0%0/3
$file_fs =~ /^\./ and not $self->{"show_hidden"}
!ll&&!rl&&r
000
MC/DC0%0/2
$file_fs =~ /^\./ and not $self->{"show_hidden"}
$file_fs =~ /^\./ not $self->{"show_hidden"}
Truth table0%0/3
A and not(B)
A= $file_fs =~ /^\./B= not $self->{"show_hidden"}
ABresult
0X0
100
111
514 0 ▶ next if -d catdir $dir_fs, $file_fs;
Branch: 1 branch
BranchTrueFalseTotal
if -d catdir($dir_fs, $file_fs) 0 0 0
515 0 push @files, decode( 'locale_fs', $file_fs );
516 }
517 0 my $chosen_dir = "Source directory: $dir";
518 0 my @tmp_prompt;
519 0 push @tmp_prompt, $chosen_dir;
520 0 ▶ push @tmp_prompt, ( $self->{cs_label} // 'File: ' ) . ( length $prev_dir ? $prev_dir : '' );
Branch: 1 branch
BranchTrueFalseTotal
length $prev_dir ? : 0 0 0
Condition0%0/2
$self->{"cs_label"} // "File: "
l!l
00
MC/DC0%0/1
$self->{"cs_label"} // "File: "
$self->{"cs_label"}
Truth table0%0/2
A
A= $self->{"cs_label"}
Aresult
11
00
521 0 ▶ my $prompt2 = $self->{prompt2} // $self->{prompt};
Condition0%0/3
$self->{"prompt2"} // $self->{"prompt"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"prompt2"} // $self->{"prompt"}
$self->{"prompt2"} $self->{"prompt"}
Truth table0%0/3
A // B
A= $self->{"prompt2"}B= $self->{"prompt"}
ABresult
1X1
011
000
522 0 ▶ if ( length $prompt2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $prompt2) 0 0 0
523 0 push @tmp_prompt, $prompt2;
524 }
525 0 my $prompt = join( "\n", @tmp_prompt );
526 0 ▶ if ( ! @files ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@files) 0 0 0
527 0 $prompt .= "\n";
528 0 ▶ if ( $self->{filter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'filter'}) { } 0 0 0
529 0 $prompt .= 'No matches for filter "' . $self->{filter} . '".';
530 }
531 else {
532 0 $prompt .= 'No files.';
533 }
534 # Choose
535 choose(
536 [ ' < ' ],
537 { info => $self->{info}, prompt => $prompt, hide_cursor => $self->{hide_cursor},
538 mouse => $self->{mouse}, color => $self->{color}, page => $self->{page}, footer => $self->{footer},
539 keep => $self->{keep}, margin => $self->{margin} }
540 0 );
541 0 return;
542 }
543 0 my @pre = ( undef );
544 0 ▶ if ( $chosen_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chosen_file) 0 0 0
545 0 push @pre, $self->{confirm}; ##
546 }
547 # Choose
548 $chosen_file = choose(
549 [ @pre, sort( @files ) ],
550 { info => $self->{info}, prompt => $prompt, alignment => $self->{alignment}, layout => $self->{layout},
551 order => $self->{order}, mouse => $self->{mouse}, clear_screen => $self->{clear_screen},
552 hide_cursor => $self->{hide_cursor}, color => $self->{color}, tabs_info => $self->{tabs_info},
553 tabs_prompt => $self->{tabs_prompt}, page => $self->{page}, footer => $self->{footer},
554 keep => $self->{keep}, undef => $self->{back}, margin => $self->{margin} }
555 0 );
556 0 ▶ if ( ! length $chosen_file ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not length $chosen_file) { } 0 0 0
elsif ($chosen_file eq $self->{'confirm'}) { } 0 0 0
557 0 ▶ if ( length $prev_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $prev_dir) 0 0 0
558 0 $prev_dir = '';
559 0 next;
560 }
561 0 return;
562 }
563 elsif ( $chosen_file eq $self->{confirm} ) {
564 0 ▶ return if ! length $prev_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless length $prev_dir 0 0 0
565 0 return catfile $dir, $prev_dir;
566 }
567 else {
568 0 $prev_dir = $chosen_file;
569 }
570 }
571 }
572  
573  
574 sub choose_a_number {
575 0 ▶ if ( ref $_[0] ne __PACKAGE__ ) {
Subroutine choose_a_number: not called CC=26 SCAR=65.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] ne __PACKAGE__) 0 0 0
576 0 my $ob = __PACKAGE__->new();
577 0 delete $ob->{backup_instance_defaults};
578 0 return $ob->choose_a_number( @_ );
579 }
580 0 my ( $self, $digits, $opt ) = @_;
581 0 my $default_digits = 7;
582 0 ▶ if ( ref $digits ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $digits) { } 0 0 0
elsif (not $digits) { } 0 0 0
583 0 $opt = $digits;
584 0 $digits = $default_digits;
585 }
586 elsif ( ! $digits ) {
587 0 $digits = $default_digits;
588 }
589 0 $self->__prepare_opt( $opt );
590 0 my $tab = ' - ';
591 0 my $tab_w = print_columns( $tab );
592 0 my $sep_w = print_columns_ext( $self->{thousands_separator}, $self->{color} );
593 0 my $longest = $digits + int( ( $digits - 1 ) / 3 ) * $sep_w;
594 0 my @ranges = ();
595 0 for my $di ( 0 .. $digits - 1 ) {
596 0 my $begin = 1 . '0' x $di;
597 0 ▶ $begin = 0 if $di == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $di == 0 0 0 0
598 0 $begin = insert_sep( $begin, $self->{thousands_separator} );
599 0 ( my $end = $begin ) =~ s/^[01]/9/;
600 unshift @ranges, unicode_sprintf( $begin, $longest, { right_justify => 1, color => $self->{color} } )
601 . $tab
602 0 . unicode_sprintf( $end, $longest, { right_justify => 1, color => $self->{color} } );
603 }
604 0 my $back_tmp = unicode_sprintf( $self->{back}, $longest * 2 + $tab_w + 1, { color => $self->{color} } );
605 0 my $confirm_tmp = unicode_sprintf( $self->{confirm}, $longest * 2 + $tab_w + 1, { color => $self->{color} } );
606 0 ▶ if ( print_columns_ext( $ranges[0], $self->{color} ) > get_term_width() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (print_columns_ext($ranges[0], $self->{"color"}) > get_term_width()) 0 0 0
607 0 @ranges = ();
608 0 for my $di ( 0 .. $digits - 1 ) {
609 0 my $begin = 1 . '0' x $di;
610 0 ▶ $begin = 0 if $di == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $di == 0 0 0 0
611 0 $begin = insert_sep( $begin, $self->{thousands_separator} );
612 0 unshift @ranges, unicode_sprintf( $begin, $longest, { color => $self->{color} } );
613 }
614 0 $confirm_tmp = $self->{confirm};
615 0 $back_tmp = $self->{back};
616 }
617 0 my %numbers;
618 my $result;
619 0 ▶ if ( defined $self->{default_number} && length $self->{default_number} <= $digits ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"default_number"} and length $self->{"default_number"} <= $digits) 0 0 0
Condition0%0/3
defined $self->{"default_number"} and length $self->{"default_number"} <= $digits
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"default_number"} and length $self->{"default_number"} <= $digits
defined $self->{"default_number"} length $self->{"default_number"} <= $digits
Truth table0%0/3
A and B
A= defined $self->{"default_number"}B= length $self->{"default_number"} <= $digits
ABresult
0X0
100
111
620 0 my $count_zeros = 0;
621 0 for my $d ( reverse split '', $self->{default_number} ) {
622 0 $numbers{$count_zeros} = $d * 10 ** $count_zeros;
623 0 $count_zeros++;
624 }
625 0 $result = sum( @numbers{keys %numbers} );
626 0 $result = insert_sep( $result, $self->{thousands_separator} );
627 }
628  
629 0 NUMBER: while ( 1 ) {
630 0 my $cs_row;
631 0 ▶ if ( defined $self->{cs_label} || length $result ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"cs_label"} or length $result) 0 0 0
Condition0%0/3
defined $self->{"cs_label"} or length $result
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $self->{"cs_label"} or length $result
defined $self->{"cs_label"} length $result
Truth table0%0/3
A or B
A= defined $self->{"cs_label"}B= length $result
ABresult
1X1
011
000
632 0 ▶ my $tmp_result = length $result ? $result : '';
Branch: 1 branch
BranchTrueFalseTotal
length $result ? : 0 0 0
633 0 ▶ my $tmp_cs_label = $self->{cs_label} // '';
Condition0%0/2
$self->{"cs_label"} // ""
l!l
00
MC/DC0%0/1
$self->{"cs_label"} // ""
$self->{"cs_label"}
Truth table0%0/2
A
A= $self->{"cs_label"}
Aresult
11
00
634 0 $cs_row = sprintf( "%s%*s", $tmp_cs_label, $longest, $tmp_result );
635 0 ▶ if ( print_columns( $cs_row ) > get_term_width() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (print_columns($cs_row) > get_term_width()) 0 0 0
636 0 $cs_row = $tmp_result;
637 }
638 }
639 0 my @tmp_prompt;
640 0 ▶ if ( defined $cs_row ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cs_row) 0 0 0
641 0 push @tmp_prompt, $cs_row;
642 }
643 0 ▶ if ( length $self->{prompt} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"prompt"}) 0 0 0
644 0 push @tmp_prompt, $self->{prompt};
645 }
646 0 my $prompt = join "\n", @tmp_prompt;
647 0 my @pre = ( undef, $confirm_tmp ); # confirm if $result ?
648 # Choose
649 my $range = choose(
650 $self->{small_first} ? [ @pre, reverse @ranges ] : [ @pre, @ranges ],
651 { info => $self->{info}, prompt => $prompt, layout => 2, alignment => 1, mouse => $self->{mouse},
652 clear_screen => $self->{clear_screen}, hide_cursor => $self->{hide_cursor}, color => $self->{color},
653 tabs_info => $self->{tabs_info}, tabs_prompt => $self->{tabs_prompt}, page => $self->{page},
654 footer => $self->{footer}, keep => $self->{keep}, undef => $back_tmp, margin => $self->{margin} }
655 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'small_first'} ? : 0 0 0
656 0 ▶ if ( ! defined $range ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $range) 0 0 0
657 0 ▶ if ( defined $result ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result) { } 0 0 0
658 0 $result = undef;
659 0 %numbers = ();
660 0 next NUMBER;
661 }
662 else {
663 0 $self->__restore_defaults();
664 0 return;
665 }
666 }
667 0 ▶ if ( $range eq $confirm_tmp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($range eq $confirm_tmp) 0 0 0
668 0 $result = _remove_thousands_separators( $result, $self->{thousands_separator} );
669 0 $self->__restore_defaults();
670 0 return $result;
671 }
672 0 my $zeros = ( split /\s*-\s*/, $range )[0];
673 0 $zeros =~ s/^\s*\d//;
674 0 my $zeros_no_sep = _remove_thousands_separators( $zeros, $self->{thousands_separator} );
675 0 my $count_zeros = length $zeros_no_sep;
676 0 ▶ my @choices = $count_zeros ? map( $_ . $zeros, 1 .. 9 ) : ( 0 .. 9 );
Branch: 1 branch
BranchTrueFalseTotal
$count_zeros ? : 0 0 0
677 # Choose
678 my $number = choose(
679 [ undef, @choices, $self->{reset} ],
680 { info => $self->{info}, prompt => $prompt, layout => 1, alignment => 2, order => 0,
681 mouse => $self->{mouse}, clear_screen => $self->{clear_screen}, hide_cursor => $self->{hide_cursor},
682 color => $self->{color}, tabs_info => $self->{tabs_info}, tabs_prompt => $self->{tabs_prompt},
683 page => $self->{page}, footer => $self->{footer}, keep => $self->{keep}, undef => '<<',
684 margin => $self->{margin} }
685 0 );
686 0 ▶ next if ! defined $number;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $number 0 0 0
687 0 ▶ if ( $number eq $self->{reset} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number eq $self->{'reset'}) { } 0 0 0
688 0 delete $numbers{$count_zeros};
689 }
690 else {
691 0 $numbers{$count_zeros} = _remove_thousands_separators( $number, $self->{thousands_separator} );
692 }
693 0 $result = sum( @numbers{keys %numbers} );
694 0 $result = insert_sep( $result, $self->{thousands_separator} );
695 }
696 }
697  
698  
699 sub _remove_thousands_separators {
700 0 ▶ my ( $str, $sep ) = @_;
Subroutine _remove_thousands_separators: not called CC=3 SCAR=24.8
701 # https://stackoverflow.com/questions/13119241/substitution-with-empty-string-unexpected-result
702 0 ▶ if ( defined $str && $sep ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str and $sep ne "") 0 0 0
Condition0%0/3
defined $str and $sep ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $str and $sep ne ""
defined $str $sep ne ""
Truth table0%0/3
A and B
A= defined $strB= $sep ne ""
ABresult
0X0
100
111
703 0 $str =~ s/\Q$sep\E//g;
704 }
705 0 return $str;
706 }
707  
708  
709 sub choose_a_subset {
710 0 ▶ if ( ref $_[0] ne __PACKAGE__ ) {
Subroutine choose_a_subset: not called CC=26 SCAR=65.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] ne __PACKAGE__) 0 0 0
711 0 my $ob = __PACKAGE__->new();
712 0 delete $ob->{backup_instance_defaults};
713 0 return $ob->choose_a_subset( @_ );
714 }
715 0 my ( $self, $available, $opt ) = @_;
716 0 ▶ my $subseq_tab = length $opt->{cs_label} && $opt->{cs_label} !~ /\R\z/ ? 2 : 0; ##
Branch: 1 branch
BranchTrueFalseTotal
length $opt->{'cs_label'} && !($opt->{'cs_label'} =~ /\R\z/) ? : 0 0 0
Condition0%0/3
length $opt->{"cs_label"} and not $opt->{"cs_label"} =~ /\R\z/
!ll&&!rl&&r
000
MC/DC0%0/2
length $opt->{"cs_label"} and not $opt->{"cs_label"} =~ /\R\z/
length $opt->{"cs_label"} not $opt->{"cs_label"} =~ /\R\z/
Truth table0%0/3
A and not(B)
A= length $opt->{"cs_label"}B= not $opt->{"cs_label"} =~ /\R\z/
ABresult
0X0
100
111
717 0 $self->__prepare_opt( $opt, $subseq_tab );
718 0 my $new_idx = [];
719 0 my $curr_avail = [ @$available ];
720 0 my $bu = [];
721 0 my @pre = ( undef, $self->{confirm} );
722 0 my $mark;
723 0 ▶ if ( defined $self->{mark} && @{$self->{mark}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"mark"} and @{$self->{"mark"};}) 0 0 0
Condition0%0/3
defined $self->{"mark"} and @{$self->{"mark"};}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"mark"} and @{$self->{"mark"};}
defined $self->{"mark"} @{$self->{"mark"};}
Truth table0%0/3
A and B
A= defined $self->{"mark"}B= @{$self->{"mark"};}
ABresult
0X0
100
111
724 0 $mark = [ map { $_ + @pre } @{$self->{mark}} ];
725 }
726  
727 0 while ( 1 ) {
728 0 my @tmp_prompt;
729 my $cs;
730 0 ▶ if ( defined $self->{cs_label} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"cs_label"}) 0 0 0
731 0 $cs .= $self->{cs_label};
732 }
733 0 ▶ if ( @$new_idx ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$new_idx) { } 0 0 0
elsif ($self->{'all_by_default'}) { } 0 0 0
734 0 ▶ $cs .= $self->{cs_begin} . join( $self->{cs_separator}, map { defined $_ ? $_ : '' } @{$available}[@$new_idx] ) . $self->{cs_end};
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
735 }
736 elsif ( $self->{all_by_default} ) {
737 0 $cs .= $self->{cs_begin} . '*' . $self->{cs_end};
738 }
739 0 ▶ if ( defined $cs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cs) 0 0 0
740 0 @tmp_prompt = ( $cs );
741 }
742 0 ▶ if ( length $self->{prompt} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"prompt"}) 0 0 0
743 0 push @tmp_prompt, $self->{prompt};
744 }
745 0 my $meta_items = [ 0 .. $#pre ];
746 0 my $prompt = join "\n", @tmp_prompt;
747 # Choose
748 my @idx = choose(
749 0 ▶ [ @pre, length( $self->{prefix} ) ? map { $self->{prefix} . ( defined $_ ? $_ : '' ) } @$curr_avail : @$curr_avail ],
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
750 { info => $self->{info}, prompt => $prompt, layout => $self->{layout}, index => 1,
751 alignment => $self->{alignment}, order => $self->{order}, mouse => $self->{mouse},
752 meta_items => $meta_items, mark => $mark, include_highlighted => 2,
753 clear_screen => $self->{clear_screen}, hide_cursor => $self->{hide_cursor},
754 color => $self->{color}, tabs_info => $self->{tabs_info}, tabs_prompt => $self->{tabs_prompt},
755 page => $self->{page}, footer => $self->{footer}, keep => $self->{keep}, undef => $self->{back},
756 busy_string => $self->{busy_string}, margin => $self->{margin} }
757 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
length $self->{'prefix'} ? : 0 0 0
758 0 $mark = undef;
759 0 ▶ if ( ! defined $idx[0] || $idx[0] == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $idx[0] or $idx[0] == 0) 0 0 0
Condition0%0/3
not defined $idx[0] or $idx[0] == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $idx[0] or $idx[0] == 0
not defined $idx[0] $idx[0] == 0
Truth table0%0/3
not(A) or B
A= not defined $idx[0]B= $idx[0] == 0
ABresult
1X1
011
000
760 0 ▶ if ( @$bu ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$bu) 0 0 0
761 0 ( $curr_avail, $new_idx ) = @{pop @$bu};
762 0 next;
763 }
764 0 $self->__restore_defaults();
765 0 return;
766 }
767 0 push @$bu, [ [ @$curr_avail ], [ @$new_idx ] ];
768 0 my $ok;
769 0 ▶ if ( $idx[0] == $#pre ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx[0] == $#pre) 0 0 0
770 0 $ok = shift @idx;
771 }
772 0 my @tmp_idx;
773 0 for my $i ( reverse @idx ) {
774 0 $i -= @pre;
775 0 ▶ if ( ! $self->{keep_chosen} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"keep_chosen"}) 0 0 0
776 0 splice( @$curr_avail, $i, 1 );
777 0 for my $used_i ( sort { $a <=> $b } @$new_idx ) {
778 0 ▶ last if $used_i > $i;
Branch: 1 branch
BranchTrueFalseTotal
if $used_i > $i 0 0 0
779 0 ++$i;
780 }
781 }
782 0 push @tmp_idx, $i;
783 }
784 0 push @$new_idx, reverse @tmp_idx;
785 0 ▶ if ( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
786 0 ▶ if ( ! @$new_idx && $self->{all_by_default} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @$new_idx and $self->{"all_by_default"}) 0 0 0
Condition0%0/3
not @$new_idx and $self->{"all_by_default"}
!ll&&!rl&&r
000
MC/DC0%0/2
not @$new_idx and $self->{"all_by_default"}
not @$new_idx $self->{"all_by_default"}
Truth table0%0/3
not(A) and B
A= not @$new_idxB= $self->{"all_by_default"}
ABresult
0X0
100
111
787 0 $new_idx = [ 0 .. $#{$available} ];
788 }
789 0 my $return_indexes = $self->{index}; # because __restore_defaults resets $self->{index}
790 0 $self->__restore_defaults();
791 0 ▶ return $return_indexes ? $new_idx : [ @{$available}[@$new_idx] ];
Branch: 1 branch
BranchTrueFalseTotal
$return_indexes ? : 0 0 0
792 }
793 }
794 }
795  
796  
797 sub settings_menu {
798 0 ▶ if ( ref $_[0] ne __PACKAGE__ ) {
Subroutine settings_menu: not called CC=24 SCAR=64.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] ne __PACKAGE__) 0 0 0
799 0 my $ob = __PACKAGE__->new();
800 0 delete $ob->{backup_instance_defaults};
801 0 return $ob->settings_menu( @_ );
802 }
803 0 my ( $self, $menu, $curr, $opt ) = @_;
804 0 $self->__prepare_opt( $opt );
805 0 my $longest = 0;
806 0 my $new = {};
807 0 my $name_w = {};
808 0 for my $sub ( @$menu ) {
809 0 my ( $key, $name, $values ) = @$sub;
810 0 $name_w->{$key} = print_columns_ext( $name, $self->{color} );
811 0 ▶ if ( $name_w->{$key} > $longest ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name_w->{$key} > $longest) 0 0 0
812 0 $longest = $name_w->{$key};
813 }
814 0 ▶ $curr->{$key} = 0 if ! defined $curr->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $curr->{$key} 0 0 0
815 0 ▶ $curr->{$key} = 0 if $curr->{$key} > $#$values;
Branch: 1 branch
BranchTrueFalseTotal
if $curr->{$key} > $values->$#* 0 0 0
816 0 ▶ $curr->{$key} = 0 if ! defined $values->[$curr->{$key}];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$values[$curr->{$key}] 0 0 0
817  
818 0 ▶ while ( ! defined $values->[$curr->{$key}] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$values[$curr->{$key}]) 0 0 0
819 0 ++$curr->{$key};
820 0 ▶ if ( $curr->{$key} > $#$values ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curr->{$key} > $values->$#*) 0 0 0
821 0 $curr->{$key} = 0;
822 0 last;
823 }
824 }
825 0 $new->{$key} = $curr->{$key};
826 }
827 0 my @print_keys;
828 0 for my $sub ( @$menu ) {
829 0 my ( $key, $name, $values ) = @$sub;
830 0 ▶ my $current = $values->[$new->{$key}] // '';
Condition0%0/2
$$values[$new->{$key}] // ""
l!l
00
MC/DC0%0/1
$$values[$new->{$key}] // ""
$$values[$new->{$key}]
Truth table0%0/2
A
A= $$values[$new->{$key}]
Aresult
11
00
831 0 push @print_keys, $name . ( ' ' x ( $longest - $name_w->{$key} ) ) . " [$current]";
832 }
833 0 my @pre = ( undef, $self->{confirm} );
834 0 $ENV{TC_RESET_AUTO_UP} = 0;
835 0 my $default = 0;
836 0 my $count = 0;
837  
838 0 while ( 1 ) {
839 0 my @tmp_prompt;
840 0 ▶ if ( defined $self->{cs_label} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"cs_label"}) 0 0 0
841 0 push @tmp_prompt, $self->{cs_label} . $self->{cs_begin} . join( $self->{cs_separator}, map { "$_=$new->{$_}" } keys %$new ) . $self->{cs_end};
842 }
843 0 ▶ if ( length $self->{prompt} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"prompt"}) 0 0 0
844 0 push @tmp_prompt, $self->{prompt};
845 }
846 0 my $prompt = join( "\n", @tmp_prompt );
847 # Choose
848 my $idx = choose(
849 [ @pre, @print_keys ],
850 { info => $self->{info}, prompt => $prompt, index => 1, default => $default, layout => 2, alignment => 0,
851 mouse => $self->{mouse}, clear_screen => $self->{clear_screen}, hide_cursor => $self->{hide_cursor},
852 color => $self->{color}, tabs_info => $self->{tabs_info}, tabs_prompt => $self->{tabs_prompt},
853 page => $self->{page}, footer => $self->{footer}, keep => $self->{keep}, undef => $self->{back},
854 margin => $self->{margin} }
855 0 );
856 0 ▶ if ( ! $idx ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $idx) { } 0 0 0
elsif ($idx == $#pre) { } 0 0 0
857 0 $self->__restore_defaults();
858 0 return;
859 }
860 elsif ( $idx == $#pre ) {
861 0 my $change = 0;
862 0 for my $sub ( @$menu ) {
863 0 my $key = $sub->[0];
864 0 ▶ if ( $curr->{$key} == $new->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curr->{$key} == $new->{$key}) 0 0 0
865 0 next;
866 }
867 0 $curr->{$key} = $new->{$key};
868 0 $change++;
869 }
870 0 $self->__restore_defaults();
871 0 return $change; #
872 }
873 0 my $i = $idx - @pre;
874 0 my $key = $menu->[$i][0];
875 0 my $values = $menu->[$i][2];
876 0 ▶ if ( $default == $idx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default == $idx) { } 0 0 0
877 0 ▶ if ( $ENV{TC_RESET_AUTO_UP} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ENV{'TC_RESET_AUTO_UP'}) { } 0 0 0
elsif ($count == @$values) { } 0 0 0
878 0 $count = 0;
879 }
880 elsif ( $count == @$values ) {
881 0 $default = 0;
882 0 $count = 0;
883 0 next;
884 }
885 }
886 else {
887 0 $count = 0;
888 0 $default = $idx;
889 }
890 0 ▶ my $curr_value = $values->[$new->{$key}] // '';
Condition0%0/2
$$values[$new->{$key}] // ""
l!l
00
MC/DC0%0/1
$$values[$new->{$key}] // ""
$$values[$new->{$key}]
Truth table0%0/2
A
A= $$values[$new->{$key}]
Aresult
11
00
891 0 my $new_value;
892  
893 0 while ( 1 ) {
894 0 ++$count;
895 0 ▶ if ( ++$new->{$key} > $#$values ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$new->{$key} > $values->$#*) 0 0 0
896 0 $new->{$key} = 0;
897 }
898 0 $new_value = $values->[$new->{$key}];
899 0 ▶ if ( defined $new_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_value) 0 0 0
900 0 last;
901 }
902 0 ▶ if ( $count == @$values ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == @$values) 0 0 0
903 0 $new_value = '';
904 0 last;
905 }
906 }
907 0 $print_keys[$i] =~ s/ \[ \Q$curr_value\E \] \z /[$new_value]/x;
908 }
909 }
910  
911  
912  
913 sub insert_sep {
914 0 ▶ my ( $number, $separator ) = @_;
Subroutine insert_sep: not called CC=5 SCAR=34.0
Pod undocumented
915 0 ▶ return $number if ! length $number;
Branch: 1 branch
BranchTrueFalseTotal
unless length $number 0 0 0
916 0 ▶ $separator = ',' if ! defined $separator;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $separator 0 0 0
917 0 ▶ return $number if $separator eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $separator eq "" 0 0 0
918 0 ▶ return $number if $number =~ /\Q$separator\E/;
Branch: 1 branch
BranchTrueFalseTotal
if $number =~ /\Q$separator\E/ 0 0 0
919 0 $number =~ s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1$separator/g;
920 # http://perldoc.perl.org/perlfaq5.html#How-can-I-output-my-numbers-with-commas-added?
921 0 return $number;
922 }
923  
924  
925 sub get_term_size {
926 0 ▶ require Term::Choose::Screen;
Subroutine get_term_size: not called CC=1 SCAR=6.9
Pod undocumented
927 0 return Term::Choose::Screen::get_term_size();
928 }
929  
930  
931 sub get_term_width {
932 0 ▶ require Term::Choose::Screen;
Subroutine get_term_width: not called CC=1 SCAR=6.9
Pod undocumented
933 0 my $term_width = ( Term::Choose::Screen::get_term_size() )[0];
934 0 return $term_width;
935 }
936  
937 sub get_term_height {
938 0 ▶ require Term::Choose::Screen;
Subroutine get_term_height: not called CC=1 SCAR=6.9
Pod undocumented
939 0 my $term_height = ( Term::Choose::Screen::get_term_size() )[1];
940 0 return $term_height;
941 }
942  
943  
944 sub unicode_sprintf {
945 0 ▶ my ( $str, $avail_w, $opt ) = @_;
Subroutine unicode_sprintf: not called CC=11 SCAR=48.8
Pod undocumented
946 0 ▶ $opt ||= {};
Condition0%0/2
$opt ||= {}
l!l
00
MC/DC0%0/1
$opt ||= {}
$opt
Truth table0%0/2
A
A= $opt
Aresult
11
00
947 0 my @color;
948 0 ▶ if ( $opt->{color} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"color"}) 0 0 0
949 0 $str =~ s/${\PH}//g;
950 0 ▶ $str =~ s/(${\SGR_ES})/push( @color, $1 ) && ${\PH}/ge;
Branch: 1 branch
BranchTrueFalseTotal
push @color, $1 and ${\"\x{feff}";} 0 0 0
951 }
952 0 my $str_w = print_columns( $str );
953 0 ▶ if ( $str_w > $avail_w ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str_w > $avail_w) 0 0 0
954 0 ▶ if ( @{$opt->{suffix_on_truncate}||[]} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$opt->{"suffix_on_truncate"} or [];}) 0 0 0
$opt->{"suffix_on_truncate"} or [] 0 0 0
955 0 $str = cut_to_printwidth( $str, $avail_w - $opt->{suffix_on_truncate}[1] ) . $opt->{suffix_on_truncate}[0];
956 }
957 0 $str = cut_to_printwidth( $str, $avail_w );
958 }
959 0 ▶ if ( @color ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@color) 0 0 0
960 0 $str =~ s/${\PH}/shift @color/ge;
961 0 ▶ if ( @color ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@color) 0 0 0
962 0 $str .= join '', @color;
963 }
964 }
965 0 ▶ if ( $str_w < $avail_w ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str_w < $avail_w) { } 0 0 0
966 0 ▶ if ( $opt->{right_justify} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'right_justify'}) { } 0 0 0
967 0 return " " x ( $avail_w - $str_w ) . $str;
968 }
969 else {
970 0 return $str . " " x ( $avail_w - $str_w );
971 }
972 }
973 else {
974 0 return $str;
975 }
976 }
977  
978  
979 sub print_columns_ext {
980 0 ▶ my ( $str, $color ) = @_;
Subroutine print_columns_ext: not called CC=2 SCAR=17.9
Pod undocumented
981 0 ▶ if ( $color ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($color) 0 0 0
982 0 $str =~ s/${\SGR_ES}//g;
983 }
984 0 return print_columns( $str );
985 }
986  
987  
988  
989  
990 1 1;
991  
992 __END__
↑ Summary