Bash/Completion/Utils.pm

filter: Statementstmt 100% 51 / 51 Branchbran 95.0% 19 / 20 Conditioncond 88.8% 8 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 95.9% 94 / 98 CC 16 SCAR 16.8
CC
16
Cov
98%
_scan_dir_for_perl_modules
8.0
match_perl_modules
7.0
command_in_path
3.0
CRAP 5.3
« App/BashComplete.pm ↑ Summary Bash/Completion/Plugins/BashComplete.pm »
1 package Bash::Completion::Utils;
2 {
3 3 $Bash::Completion::Utils::VERSION = '0.008';
4 }
5  
6 # ABSTRACT: Set of utility functions that help writting plugins
7  
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 @Bash::Completion::Utils::EXPORT_OK = qw(
15 command_in_path
16 match_perl_modules
17 prefix_match
18 );
19  
20  
21 4 ▶sub command_in_path {
Subroutine command_in_path: called CC=3 SCAR=11.0
Pod documented
22 4 my ($cmd) = @_;
23  
24 4 for my $path (grep {$_} split(/$Config{path_sep}/, $ENV{PATH})) {
25 14 my $file = catfile($path, $cmd);
26 14 ▶ return $file if -x $file;
Branch: 1 branch
BranchTrueFalseTotal
if -x $file 3 11 14
27 }
28  
29 1 return;
30 }
31  
32  
33  
34 11 ▶sub match_perl_modules {
Subroutine match_perl_modules: called CC=7 SCAR=19.5
Pod documented
35 11 my ($pm) = @_;
36 11 my ($filler, %found) = ('');
37  
38 11 ▶ $pm .= $filler = ':' if $pm =~ /[^:]:$/;
Branch: 1 branch
BranchTrueFalseTotal
if $pm =~ /[^:]:$/ 1 10 11
39  
40 11 my ($ns, $filter) = $pm =~ m{^(.+::)?(.*)};
41 11 ▶ $ns = '' unless $ns;
Branch: 1 branch
BranchTrueFalseTotal
unless $ns 1 10 11
42  
43 11 my $sdir = $ns;
44 11 $sdir =~ s{::}{/}g;
45  
46 11 for my $lib (@INC) {
47 83 ▶ next if $lib eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $lib eq "." 10 73 83
48 73 _scan_dir_for_perl_modules(catdir($lib, $sdir), $ns, $filter, \%found);
49 }
50  
51 11 my @found = keys %found;
52 11 map {s/^$ns/$filler/} @found;
53  
54 11 ▶ return if 1 == @found && $found[0] eq $filter; ## Exact match, ignore it
Branch: 1 branch
BranchTrueFalseTotal
if 1 == @found and $found[0] eq $filter 0 11 11
Condition66%2/3
1 == @found and $found[0] eq $filter
!ll&&!rl&&r
650
MC/DC0%0/2
1 == @found and $found[0] eq $filter
1 == @found $found[0] eq $filter
Truth table66%2/3
A and B
A= 1 == @foundB= $found[0] eq $filter
ABresult
0X0
100
111
55 11 return @found;
56 }
57  
58 73 ▶sub _scan_dir_for_perl_modules {
Subroutine _scan_dir_for_perl_modules: called CC=8 SCAR=20.8
59 73 my ($dir, $ns, $name, $found) = @_;
60  
61 73 ▶ return unless opendir(my $dh, $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless opendir my $dh, $dir 44 29 73
62  
63 29 ▶ while (my $entry = readdir($dh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = readdir $dh)) 439 29 468
64 439 ▶ next if $entry =~ /^[.]/;
Branch: 1 branch
BranchTrueFalseTotal
if $entry =~ /^[.]/ 62 377 439
65  
66 377 my $path = catfile($dir, $entry);
67  
68 377 ▶ if (-d $path && $entry =~ m/^$name/) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $path and $entry =~ /^$name/) { } 11 366 377
elsif (-f _ and $entry =~ /^($name.*)[.]pm$/) { } 40 510 550
Condition100%3/3
-d $path and $entry =~ /^$name/
!ll&&!rl&&r
18218411
MC/DC100%2/2
-d $path and $entry =~ /^$name/
-d $path $entry =~ /^$name/
Truth table100%3/3
A and B
A= -d $pathB= $entry =~ /^$name/
ABresult
0X0
100
111
69 11 $found->{"$ns${entry}::"} = 1;
70 }
71 ▶ elsif (-f _ && $entry =~ m/^($name.*)[.]pm$/) {
Condition100%3/3
-f _ and $entry =~ /^($name.*)[.]pm$/
!ll&&!rl&&r
18414240
MC/DC100%2/2
-f _ and $entry =~ /^($name.*)[.]pm$/
-f _ $entry =~ /^($name.*)[.]pm$/
Truth table100%3/3
A and B
A= -f _B= $entry =~ /^($name.*)[.]pm$/
ABresult
0X0
100
111
72 40 $found->{"$ns$1"} = 1;
73 }
74 }
75 }
76  
77  
78  
79 4 ▶sub prefix_match {
Subroutine prefix_match: called CC=1 SCAR=0.0
Pod documented
80 4 my $prefix = shift;
81  
82 4 return grep {/^$prefix/} @_;
83 }
84  
85 3 1;
86  
87  
88  
89 =pod
90  
91 =head1 NAME
92  
93 Bash::Completion::Utils - Set of utility functions that help writting plugins
94  
95 =head1 VERSION
96  
97 version 0.008
98  
99 =head1 SYNOPSIS
100  
101 use Bash::Completion::Utils qw(
102 command_in_path match_perl_modules prefix_match
103 );
104  
105 ...
106  
107 =head1 DESCRIPTION
108  
109 A library of utility functions usefull to plugin writers.
110  
111 =head1 FUNCTIONS
112  
113 =head2 command_in_path
114  
115 Given a command name, returns the full path if we find it in the PATH.
116  
117 =head2 match_perl_modules
118  
119 Given a partial module name, returns a list of all the possible completions.
120  
121 If a single exact match is found, it returns nothing.
122  
123 Some examples:
124  
125 =over 4
126  
127 =item (empty string)
128  
129 List all top level modules and namespaces.
130  
131 =item Template
132  
133 List C<Template>, the module, and C<Template::>, the namespace.
134  
135 =item Net::DNS::RR::
136  
137 Lists all type of Resource Records modules.
138  
139 =item File::Tempdir
140  
141 Returns an empty list.
142  
143 =back
144  
145 =head2 prefix_match
146  
147 Accepts a single word and a list of options.
148  
149 Returns the options that match the word.
150  
151 =head1 AUTHOR
152  
153 Pedro Melo <melo@cpan.org>
154  
155 =head1 COPYRIGHT AND LICENSE
156  
157 This software is Copyright (c) 2011 by Pedro Melo.
158  
159 This is free software, licensed under:
160  
161 The Artistic License 2.0 (GPL Compatible)
162  
163 =cut
164  
165  
166 __END__
« App/BashComplete.pm ↑ Summary Bash/Completion/Plugins/BashComplete.pm »