blib/lib/App/ProgUtils.pm

filter: Statementstmt 39.0% 16 / 41 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 37.5% 3 / 8 Podpod 100% 1 / 1 totaltotal 27.4% 20 / 73 CC 11 SCAR 35.6
CC
11
Cov
27%
list_all_programs_in_path
72.0
_search_program
12.0
__ANON__
6.0
CRAP 35.1
↑ Summary
1 package App::ProgUtils;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8 1 our $DATE = '2023-12-03'; # DATE
9 1 our $DIST = 'App-ProgUtils'; # DIST
10 1 our $VERSION = '0.204'; # VERSION
11  
12 1 our %SPEC;
13  
14 0 ▶our $_complete_program = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
15 0 require Complete::File;
16 0 require Complete::Program;
17 0 require Complete::Util;
18 0 require List::MoreUtils;
19  
20 0 my %args = @_;
21  
22 0 ▶ my $word = $args{word} // '';
Condition0%0/2
$args{"word"} // ""
l!l
00
MC/DC0%0/1
$args{"word"} // ""
$args{"word"}
Truth table0%0/2
A
A= $args{"word"}
Aresult
11
00
23  
24 # combine all executables (including dirs) and programs in PATH
25 my $c1 = Complete::Util::arrayify_answer(
26 Complete::File::complete_file(
27 word => $word,
28 0 ▶ filter => sub { -x $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 #ci => 1, # convenience, not yet supported by C::U
30 0 ),
31 );
32 0 my $c2 = Complete::Util::arrayify_answer(
33 Complete::Program::complete_program(
34 word => $word,
35 ci => 1, # convenience
36 ),
37 );
38  
39 {
40 0 words => [ List::MoreUtils::uniq(sort(@$c1, @$c2)) ],
41 path_sep => '/',
42 };
43 1 };
44  
45 0 ▶sub _search_program {
Subroutine _search_program: not called CC=3 SCAR=24.8
46 0 require File::Which;
47  
48 0 my $prog = shift;
49 0 ▶ if ($prog =~ m!/!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prog =~ m[/]) { } 0 0 0
50 0 return $prog;
51 } else {
52 0 ▶ return File::Which::which($prog) // $prog;
Condition0%0/3
File::Which::which($prog) // $prog
l!l&&r!l&&!r
000
MC/DC0%0/2
File::Which::which($prog) // $prog
File::Which::which($prog) $prog
Truth table0%0/3
A // B
A= File::Which::which($prog)B= $prog
ABresult
1X1
011
000
53 }
54 }
55  
56 $SPEC{list_all_programs_in_path} = {
57 v => 1.1,
58 summary => 'List all programs in PATH',
59 args => {
60 with_path => {
61 schema => 'bool*',
62 cmdline_aliases => {
63 0 ▶ 'x' => {is_flag=>1, summary => 'Show path of each program', code => sub { $_[0]{with_path} = 1 }},
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 },
65 },
66 },
67 1 links => [
68 {url=>'prog:proglist'},
69 ],
70 };
71 0 ▶sub list_all_programs_in_path {
Subroutine list_all_programs_in_path: not called CC=8 SCAR=42.8
Pod documented
72 0 my %args = @_;
73  
74 0 my $with_path = $args{with_path};
75  
76 0 ▶ my @dirs = split(($^O =~ /Win32/ ? qr/;/ : qr/:/), $ENV{PATH});
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /Win32/ ? : 0 0 0
77 0 my @all_progs;
78 0 for my $dir (@dirs) {
79 0 ▶ opendir my($dh), $dir or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or next 0 0 0
80 0 for (readdir($dh)) {
81 0 ▶ push @all_progs, ($with_path ? "$dir/$_" : $_)
Branch: 2 branches
BranchTrueFalseTotal
if not -d "$dir/$_" and -x _ 0 0 0
$with_path ? : 0 0 0
Condition0%0/3
not -d "$dir/$_" and -x _
!ll&&!rl&&r
000
MC/DC0%0/2
not -d "$dir/$_" and -x _
not -d "$dir/$_" -x _
Truth table0%0/3
not(A) and B
A= not -d "$dir/$_"B= -x _
ABresult
0X0
100
111
82 if !(-d "$dir/$_") && (-x _);
83 }
84 }
85  
86 0 [200, "OK", \@all_progs];
87 }
88  
89 1 1;
90 # ABSTRACT: Command line to manipulate programs in PATH
91  
92 __END__
↑ Summary