blib/lib/Complete/File.pm

filter: Statementstmt 89.5% 129 / 144 Branchbran 71.7% 76 / 106 Conditioncond 68.2% 28 / 41 MC/DCmcdc 48.0% 12 / 25 Subroutinesub 100% 18 / 18 Podpod 100% 2 / 2 totaltotal 78.8% 265 / 336 CC 74 SCAR 28.3
CC
74
Cov
80%
complete_file
32.3
__ANON__
24.5
__ANON__
14.2
CRAP 17.0
↑ Summary
1 package Complete::File;
2  
3 3 our $DATE = '2021-02-08'; # DATE
4 3 our $VERSION = '0.443'; # VERSION
5  
6 3 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Complete::Common qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Complete::Util qw(hashify_answer);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 require Exporter;
14 3 our @ISA = qw(Exporter);
15 3 our @EXPORT_OK = qw(
16 complete_file
17 complete_dir
18 );
19  
20 3 our %SPEC;
21  
22 3 $SPEC{':package'} = {
23 v => 1.1,
24 summary => 'Completion routines related to files',
25 };
26  
27 $SPEC{complete_file} = {
28 3 v => 1.1,
29 summary => 'Complete file and directory from local filesystem',
30 args => {
31 %arg_word,
32 filter => {
33 summary => 'Only return items matching this filter',
34 description => <<'_',
35  
36 Filter can either be a string or a code.
37  
38 For string filter, you can specify a pipe-separated groups of sequences of these
39 characters: f, d, r, w, x. Dash can appear anywhere in the sequence to mean
40 not/negate. An example: `f` means to only show regular files, `-f` means only
41 show non-regular files, `drwx` means to show only directories which are
42 readable, writable, and executable (cd-able). `wf|wd` means writable regular
43 files or writable directories.
44  
45 For code filter, you supply a coderef. The coderef will be called for each item
46 with these arguments: `$name`. It should return true if it wants the item to be
47 included.
48  
49 _
50 schema => ['any*' => {of => ['str*', 'code*']}],
51 tags => ['category:filtering'],
52 },
53 file_regex_filter => {
54 summary => 'Filter shortcut for file regex',
55 description => <<'_',
56  
57 This is a shortcut for constructing a filter. So instead of using `filter`, you
58 use this option. This will construct a filter of including only directories or
59 regular files, and the file must match a regex pattern. This use-case is common.
60  
61 _
62 schema => 're*',
63 tags => ['category:filtering'],
64 },
65 exclude_dir => {
66 schema => 'bool*',
67 description => <<'_',
68  
69 This is also an alternative to specifying full `filter`. Set this to true if you
70 do not want directories.
71  
72 If you only want directories, take a look at `complete_dir()`.
73  
74 _
75 tags => ['category:filtering'],
76 },
77 file_ext_filter => {
78 schema => ['any*', of=>['re*', ['array*',of=>'str*']]],
79 description => <<'_',
80  
81 This is also an alternative to specifying full `filter` or `file_regex_filter`.
82 You can set this to a regex or a set of extensions to accept. Note that like in
83 `file_regex_filter`, directories of any name is also still allowed.
84  
85 _
86 tags => ['category:filtering'],
87 },
88 starting_path => {
89 schema => 'str*',
90 default => '.',
91 },
92 handle_tilde => {
93 schema => 'bool',
94 default => 1,
95 },
96 allow_dot => {
97 summary => 'If turned off, will not allow "." or ".." in path',
98 description => <<'_',
99  
100 This is most useful when combined with `starting_path` option to prevent user
101 going up/outside the starting path.
102  
103 _
104 schema => 'bool',
105 default => 1,
106 },
107 recurse => {
108 schema => 'bool*',
109 cmdline_aliases => {r=>{}},
110 },
111 recurse_matching => {
112 schema => ['str*', in=>['level-by-level', 'all-at-once']],
113 default => 'level-by-level',
114 },
115 exclude_leaf => {
116 schema => 'bool*',
117 },
118 exclude_dir => {
119 schema => 'bool*',
120 },
121 },
122 args_rels => {
123 dep_all => [recurse_matching => ['recurse']],
124 },
125 result_naked => 1,
126 result => {
127 schema => 'array',
128 },
129 };
130 13 ▶sub complete_file {
Subroutine complete_file: called CC=25 SCAR=34.7
Pod documented
131 13 require Complete::Path;
132 13 require Encode;
133 13 require File::Glob;
134  
135 13 my %args = @_;
136 13 ▶ my $word = $args{word} // "";
Condition50%1/2
$args{"word"} // ""
l!l
130
MC/DC0%0/1
$args{"word"} // ""
$args{"word"}
Truth table50%1/2
A
A= $args{"word"}
Aresult
11
00
137 13 ▶ my $handle_tilde = $args{handle_tilde} // 1;
Condition50%1/2
$args{"handle_tilde"} // 1
l!l
013
MC/DC0%0/1
$args{"handle_tilde"} // 1
$args{"handle_tilde"}
Truth table50%1/2
A
A= $args{"handle_tilde"}
Aresult
11
00
138 13 ▶ my $allow_dot = $args{allow_dot} // 1;
Condition50%1/2
$args{"allow_dot"} // 1
l!l
013
MC/DC0%0/1
$args{"allow_dot"} // 1
$args{"allow_dot"}
Truth table50%1/2
A
A= $args{"allow_dot"}
Aresult
11
00
139  
140 # if word is starts with "~/" or "~foo/" replace it temporarily with user's
141 # name (so we can restore it back at the end). this is to mimic bash
142 # support. note that bash does not support case-insensitivity for "foo".
143 13 my $result_prefix;
144 13 ▶ my $starting_path = $args{starting_path} // '.';
Condition50%1/2
$args{"starting_path"} // "."
l!l
013
MC/DC0%0/1
$args{"starting_path"} // "."
$args{"starting_path"}
Truth table50%1/2
A
A= $args{"starting_path"}
Aresult
11
00
145 13 ▶ if ($handle_tilde && $word =~ s!\A(~[^/]*)/!!) {
Branch: 2 branches
BranchTrueFalseTotal
if ($handle_tilde and $word =~ s[\A(~[^/]*)/][]) { } 0 13 13
elsif ($allow_dot and $word =~ s[\A((?:\.\.?/+)+|/+)][]) { } 0 13 13
Condition33%1/3
$handle_tilde and $word =~ s[\A(~[^/]*)/][]
!ll&&!rl&&r
0130
MC/DC0%0/2
$handle_tilde and $word =~ s[\A(~[^/]*)/][]
$handle_tilde $word =~ s[\A(~[^/]*)/][]
Truth table33%1/3
A and B
A= $handle_tildeB= $word =~ s[\A(~[^/]*)/][]
ABresult
0X0
100
111
146 0 $result_prefix = "$1/";
147 0 my @dir = File::Glob::bsd_glob($1); # glob will expand ~foo to /home/foo
148 0 ▶ return [] unless @dir;
Branch: 1 branch
BranchTrueFalseTotal
unless @dir 0 0 0
149 0 $starting_path = Encode::decode('UTF-8', $dir[0]);
150 ▶ } elsif ($allow_dot && $word =~ s!\A((?:\.\.?/+)+|/+)!!) {
Condition33%1/3
$allow_dot and $word =~ s[\A((?:\.\.?/+)+|/+)][]
!ll&&!rl&&r
0130
MC/DC0%0/2
$allow_dot and $word =~ s[\A((?:\.\.?/+)+|/+)][]
$allow_dot $word =~ s[\A((?:\.\.?/+)+|/+)][]
Truth table33%1/3
A and B
A= $allow_dotB= $word =~ s[\A((?:\.\.?/+)+|/+)][]
ABresult
0X0
100
111
151 # just an optimization to skip sequences of '../'
152 0 $starting_path = $1;
153 0 $result_prefix = $1;
154 0 ▶ $starting_path =~ s#/+\z## unless $starting_path =~ m!\A/!;
Branch: 1 branch
BranchTrueFalseTotal
unless $starting_path =~ m[\A/] 0 0 0
155 }
156  
157 # bail if we don't allow dot and the path contains dot
158 13 ▶ return [] if !$allow_dot &&
Branch: 1 branch
BranchTrueFalseTotal
if not $allow_dot and $word =~ m[(?:\A|/)\.\.?(?:\z|/)] 0 13 13
Condition33%1/3
not $allow_dot and $word =~ m[(?:\A|/)\.\.?(?:\z|/)]
!ll&&!rl&&r
1300
MC/DC0%0/2
not $allow_dot and $word =~ m[(?:\A|/)\.\.?(?:\z|/)]
not $allow_dot $word =~ m[(?:\A|/)\.\.?(?:\z|/)]
Truth table33%1/3
not(A) and B
A= not $allow_dotB= $word =~ m[(?:\A|/)\.\.?(?:\z|/)]
ABresult
0X0
100
111
159 $word =~ m!(?:\A|/)\.\.?(?:\z|/)!;
160  
161 # prepare list_func
162 24 ▶ my $list = sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
163 24 my ($path, $intdir, $isint) = @_;
164 24 ▶ opendir my($dh), $path or return undef;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $path or return undef 24 0 24
165 24 my @res;
166 24 for (sort readdir $dh) {
167 # skip . and .. if leaf is empty, like in bash
168 206 ▶ next if ($_ eq '.' || $_ eq '..') && $intdir eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "." || $_ eq ".." and $intdir eq "" 20 186 206
Condition100%6/6
$_ eq "." || $_ eq ".." and $intdir eq ""
!ll&&!rl&&r
1582820
$_ eq "." or $_ eq ".."
l!l&&r!l&&!r
2424158
MC/DC100%3/3
$_ eq "." || $_ eq ".." and $intdir eq ""
$_ eq "." $_ eq ".." $intdir eq ""
Truth table100%5/5
A or B and C
A= $_ eq "."B= $_ eq ".."C= $intdir eq ""
ABCresult
00X0
1X00
0100
1X11
0111
169 186 ▶ next if $isint && !(-d "$path/$_");
Branch: 1 branch
BranchTrueFalseTotal
if $isint and not -d "$path/$_" 28 158 186
Condition100%3/3
$isint and not -d "$path/$_"
!ll&&!rl&&r
1223628
MC/DC100%2/2
$isint and not -d "$path/$_"
$isint not -d "$path/$_"
Truth table100%3/3
A and not(B)
A= $isintB= not -d "$path/$_"
ABresult
0X0
100
111
170 158 push @res, Encode::decode('UTF-8', $_);
171 }
172 24 \@res;
173 13 };
174  
175 # prepare filter_func
176  
177 # from the filter option
178 13 my $filter;
179 13 ▶ if ($args{filter} && !ref($args{filter})) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'filter'} and not ref $args{'filter'}) { } 2 11 13
elsif ($args{'filter'} and ref $args{'filter'} eq 'CODE') { } 1 20 21
Condition100%3/3
$args{"filter"} and not ref $args{"filter"}
!ll&&!rl&&r
1012
MC/DC100%2/2
$args{"filter"} and not ref $args{"filter"}
$args{"filter"} not ref $args{"filter"}
Truth table100%3/3
A and not(B)
A= $args{"filter"}B= not ref $args{"filter"}
ABresult
0X0
100
111
180 2 my @seqs = split /\s*\|\s*/, $args{filter};
181 6 ▶ $filter = sub {
Subroutine __ANON__: called CC=15 SCAR=32.0
182 6 my $name = shift;
183 6 ▶ my @st = stat($name) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my(@st) = stat $name or return 0 6 0 6
184 6 my $mode = $st[2];
185 6 my $pass;
186 SEQ:
187 6 for my $seq (@seqs) {
188 7 ▶ my $neg = sub { $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
189 7 for my $c (split //, $seq) {
190 11 ▶ if ($c eq '-') { $neg = sub { $_[0] ? 0 : 1 } }
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 7 branches
BranchTrueFalseTotal
if ($c eq '-') { } 4 7 11
elsif ($c eq 'r') { } 0 7 7
elsif ($c eq 'w') { } 0 7 7
elsif ($c eq 'x') { } 0 7 7
elsif ($c eq 'f') { } 1 6 7
elsif ($c eq 'd') { } 6 0 6
$_[0] ? : 3 1 4
191 0 ▶ elsif ($c eq 'r') { next SEQ unless $neg->($mode & 0400) }
Branch: 1 branch
BranchTrueFalseTotal
unless &$neg($mode & 256) 0 0 0
192 0 ▶ elsif ($c eq 'w') { next SEQ unless $neg->($mode & 0200) }
Branch: 1 branch
BranchTrueFalseTotal
unless &$neg($mode & 128) 0 0 0
193 0 ▶ elsif ($c eq 'x') { next SEQ unless $neg->($mode & 0100) }
Branch: 1 branch
BranchTrueFalseTotal
unless &$neg($mode & 64) 0 0 0
194 1 ▶ elsif ($c eq 'f') { next SEQ unless $neg->($mode & 0100000)}
Branch: 1 branch
BranchTrueFalseTotal
unless &$neg($mode & 32768) 1 0 1
195 6 ▶ elsif ($c eq 'd') { next SEQ unless $neg->($mode & 0040000)}
Branch: 1 branch
BranchTrueFalseTotal
unless &$neg($mode & 16384) 3 3 6
196 else {
197 0 die "Unknown character in filter: $c (in $seq)";
198 }
199 }
200 3 $pass = 1; last SEQ;
201 }
202 6 $pass;
203 2 };
204 ▶ } elsif ($args{filter} && ref($args{filter}) eq 'CODE') {
Condition66%2/3
$args{"filter"} and ref $args{"filter"} eq "CODE"
!ll&&!rl&&r
1001
MC/DC50%1/2
$args{"filter"} and ref $args{"filter"} eq "CODE"
$args{"filter"} ref $args{"filter"} eq "CODE"
Truth table66%2/3
A and B
A= $args{"filter"}B= ref $args{"filter"} eq "CODE"
ABresult
0X0
100
111
205 1 $filter = $args{filter};
206 }
207  
208 # from the file_regex_filter option
209 13 my $filter_fregex;
210 13 ▶ if ($args{file_regex_filter}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"file_regex_filter"}) 1 12 13
211 11 ▶ $filter_fregex = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
212 11 my $name = shift;
213 11 ▶ return 1 if -d $name;
Branch: 1 branch
BranchTrueFalseTotal
if -d $name 4 7 11
214 7 ▶ return 0 unless -f _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f _ 0 7 7
215 7 ▶ return 1 if $name =~ $args{file_regex_filter};
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /$args{"file_regex_filter"}/ 2 5 7
216 5 0;
217 1 };
218 }
219  
220 # from the file_ext_filter option
221 13 my $filter_fext;
222 13 ▶ if ($args{file_ext_filter} && ref $args{file_ext_filter} eq 'Regexp') {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'file_ext_filter'} and ref $args{'file_ext_filter'} eq 'Regexp') { } 1 12 13
elsif ($args{'file_ext_filter'} and ref $args{'file_ext_filter'} eq 'ARRAY') { } 1 22 23
Condition100%3/3
$args{"file_ext_filter"} and ref $args{"file_ext_filter"} eq "Regexp"
!ll&&!rl&&r
1111
MC/DC100%2/2
$args{"file_ext_filter"} and ref $args{"file_ext_filter"} eq "Regexp"
$args{"file_ext_filter"} ref $args{"file_ext_filter"} eq "Regexp"
Truth table100%3/3
A and B
A= $args{"file_ext_filter"}B= ref $args{"file_ext_filter"} eq "Regexp"
ABresult
0X0
100
111
223 3 ▶ $filter_fext = sub {
Subroutine __ANON__: called CC=5 SCAR=16.6
224 3 my $name = shift;
225 3 ▶ return 1 if -d $name;
Branch: 1 branch
BranchTrueFalseTotal
if -d $name 0 3 3
226 3 ▶ return 0 unless -f _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f _ 0 3 3
227 3 ▶ my $ext = $name =~ /\.(\w+)\z/ ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\.(\w+)\z/ ? : 3 0 3
228 3 ▶ return 1 if $ext =~ $args{file_ext_filter};
Branch: 1 branch
BranchTrueFalseTotal
if $ext =~ /$args{"file_ext_filter"}/ 2 1 3
229 1 0;
230 1 };
231 ▶ } elsif ($args{file_ext_filter} && ref $args{file_ext_filter} eq 'ARRAY') {
Condition66%2/3
$args{"file_ext_filter"} and ref $args{"file_ext_filter"} eq "ARRAY"
!ll&&!rl&&r
1101
MC/DC50%1/2
$args{"file_ext_filter"} and ref $args{"file_ext_filter"} eq "ARRAY"
$args{"file_ext_filter"} ref $args{"file_ext_filter"} eq "ARRAY"
Truth table66%2/3
A and B
A= $args{"file_ext_filter"}B= ref $args{"file_ext_filter"} eq "ARRAY"
ABresult
0X0
100
111
232 3 ▶ $filter_fext = sub {
Subroutine __ANON__: called CC=9 SCAR=26.5
233 3 my $name = shift;
234 3 ▶ return 1 if -d $name;
Branch: 1 branch
BranchTrueFalseTotal
if -d $name 0 3 3
235 3 ▶ return 0 unless -f _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f _ 0 3 3
236 3 ▶ my $ext = $name =~ /\.(\w+)\z/ ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\.(\w+)\z/ ? : 3 0 3
237 3 ▶ if ($Complete::Common::OPT_CI) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Complete::Common::OPT_CI) { } 0 3 3
238 0 $ext = lc($ext);
239 0 for my $e (@{ $args{file_ext_filter} }) {
240 0 ▶ return 1 if $ext eq lc($e);
Branch: 1 branch
BranchTrueFalseTotal
if $ext eq lc $e 0 0 0
241 }
242 } else {
243 3 for my $e (@{ $args{file_ext_filter} }) {
244 5 ▶ return 1 if $ext eq $e;
Branch: 1 branch
BranchTrueFalseTotal
if $ext eq $e 2 3 5
245 }
246 }
247 1 0;
248 1 };
249 }
250  
251 # from _dir (used by complete_dir)
252 13 my $filter_dir;
253 13 ▶ if ($args{_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"_dir"}) 1 12 13
254 1 ▶ $filter_dir = sub { return 0 unless (-d $_[0]); 1 };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless -d $_[0] 7 3 10
255 }
256  
257 # from exclude_dir option
258 13 my $filter_xdir;
259 13 ▶ if ($args{exclude_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"exclude_dir"}) 1 12 13
260 1 ▶ $filter_xdir = sub { return 0 if (-d $_[0]); 1 };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if -d $_[0] 2 1 3
261 }
262  
263 # final filter sub
264 70 ▶ my $final_filter = sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
265 70 my $name = shift;
266 70 ▶ if ($filter_dir) { return 0 unless $filter_dir->($name) }
Branch: 2 branches
BranchTrueFalseTotal
if ($filter_dir) 10 60 70
unless &$filter_dir($name) 7 3 10
267 63 ▶ if ($filter_xdir) { return 0 unless $filter_xdir->($name) }
Branch: 2 branches
BranchTrueFalseTotal
if ($filter_xdir) 3 60 63
unless &$filter_xdir($name) 2 1 3
268 61 ▶ if ($filter) { return 0 unless $filter->($name) }
Branch: 2 branches
BranchTrueFalseTotal
if ($filter) 17 44 61
unless &$filter($name) 13 4 17
269 48 ▶ if ($filter_fregex) { return 0 unless $filter_fregex->($name) }
Branch: 2 branches
BranchTrueFalseTotal
if ($filter_fregex) 11 37 48
unless &$filter_fregex($name) 5 6 11
270 43 ▶ if ($filter_fext) { return 0 unless $filter_fext->($name) }
Branch: 2 branches
BranchTrueFalseTotal
if ($filter_fext) 6 37 43
unless &$filter_fext($name) 2 4 6
271 41 1;
272 13 };
273  
274 my $compres = Complete::Path::complete_path(
275 word => $word,
276 list_func => $list,
277 41 ▶ is_dir_func => sub { -d $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
278 filter_func => $final_filter,
279 starting_path => $starting_path,
280 result_prefix => $result_prefix,
281 recurse => $args{recurse},
282 recurse_matching => $args{recurse_matching},
283 exclude_leaf => $args{exclude_leaf},
284 exclude_nonleaf => $args{exclude_nonleaf} // $args{exclude_dir},
285 13 ▶ );
Condition66%2/3
$args{"exclude_nonleaf"} // $args{"exclude_dir"}
l!l&&r!l&&!r
0112
MC/DC50%1/2
$args{"exclude_nonleaf"} // $args{"exclude_dir"}
$args{"exclude_nonleaf"} $args{"exclude_dir"}
Truth table66%2/3
A // B
A= $args{"exclude_nonleaf"}B= $args{"exclude_dir"}
ABresult
1X1
011
000
286  
287 # XXX why doesn't Complete::Path return hash answer with path_sep? we add
288 # workaround here to enable path mode.
289 13 hashify_answer($compres, {path_sep=>'/'});
290 }
291  
292 3 $SPEC{complete_dir} = do {
293 3 my $spec = {%{ $SPEC{complete_file} }}; # shallow copy
294  
295 3 $spec->{summary} = 'Complete directory from local filesystem '.
296 '(wrapper for complete_dir() that only picks directories)';
297 3 $spec->{args} = { %{$spec->{args}} }; # shallow copy of args
298 3 delete $spec->{args}{file_regex_filter};
299 3 delete $spec->{args}{file_ext_filter};
300 3 delete $spec->{args}{exclude_dir};
301  
302 3 $spec;
303 };
304 1 ▶sub complete_dir {
Subroutine complete_dir: called CC=1 SCAR=0.0
Pod documented
305 1 my %args = @_;
306  
307 1 complete_file(%args, _dir=>1);
308 }
309  
310 3 1;
311 # ABSTRACT: Completion routines related to files
312  
313 __END__
↑ Summary