File Coverage

blib/lib/Bash/Completion/Utils.pm
Criterion Covered Total %
statement 47 47 100.0
branch 17 18 94.4
condition 8 9 88.8
subroutine 9 9 100.0
pod 3 3 100.0
total 84 86 97.6


line stmt bran cond sub pod time code
1             package Bash::Completion::Utils;
2             {
3             $Bash::Completion::Utils::VERSION = '0.008';
4             }
5              
6             # ABSTRACT: Set of utility functions that help writting plugins
7              
8 3     3   889 use strict;
  3         6  
  3         102  
9 3     3   17 use warnings;
  3         22  
  3         85  
10 3     3   34 use parent 'Exporter';
  3         6  
  3         20  
11 3     3   181 use File::Spec::Functions;
  3         15  
  3         304  
12 3     3   15 use Config;
  3         3  
  3         1957  
13              
14             @Bash::Completion::Utils::EXPORT_OK = qw(
15             command_in_path
16             match_perl_modules
17             prefix_match
18             );
19              
20              
21             sub command_in_path {
22 4     4 1 1898 my ($cmd) = @_;
23              
24 4         69 for my $path (grep {$_} split(/$Config{path_sep}/, $ENV{PATH})) {
  32         44  
25 11         44 my $file = catfile($path, $cmd);
26 11 100       364 return $file if -x $file;
27             }
28              
29 1         7 return;
30             }
31              
32              
33              
34             sub match_perl_modules {
35 11     11 1 14883 my ($pm) = @_;
36 11         27 my ($filler, %found) = ('');
37              
38 11 100       48 $pm .= $filler = ':' if $pm =~ /[^:]:$/;
39              
40 11         78 my ($ns, $filter) = $pm =~ m{^(.+::)?(.*)};
41 11 100       35 $ns = '' unless $ns;
42              
43 11         18 my $sdir = $ns;
44 11         43 $sdir =~ s{::}{/}g;
45              
46 11         26 for my $lib (@INC) {
47 104 100       251 next if $lib eq '.';
48 94         438 _scan_dir_for_perl_modules(catdir($lib, $sdir), $ns, $filter, \%found);
49             }
50              
51 11         45 my @found = keys %found;
52 11         26 map {s/^$ns/$filler/} @found;
  36         265  
53              
54 11 50 66     50 return if 1 == @found && $found[0] eq $filter; ## Exact match, ignore it
55 11         99 return @found;
56             }
57              
58             sub _scan_dir_for_perl_modules {
59 94     94   162 my ($dir, $ns, $name, $found) = @_;
60              
61 94 100       3427 return unless opendir(my $dh, $dir);
62              
63 31         574 while (my $entry = readdir($dh)) {
64 410 100       1332 next if $entry =~ /^[.]/;
65              
66 342         1334 my $path = catfile($dir, $entry);
67              
68 342 100 100     14454 if (-d $path && $entry =~ m/^$name/) {
    100 100        
69 11         79 $found->{"$ns${entry}::"} = 1;
70             }
71             elsif (-f _ && $entry =~ m/^($name.*)[.]pm$/) {
72 41         427 $found->{"$ns$1"} = 1;
73             }
74             }
75             }
76              
77              
78              
79             sub prefix_match {
80 4     4 1 23911 my $prefix = shift;
81              
82 4         12 return grep {/^$prefix/} @_;
  18         137  
83             }
84              
85             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