blib/lib/App/File/Grepper.pm

filter: Statementstmt 21.5% 17 / 79 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 55.5% 5 / 9 Podpod 0.0% 0 / 1 totaltotal 12.1% 22 / 181 CC 38 SCAR 58.2
CC
38
Cov
11%
__ANON__
494.0
main
256.4
__ANON__
2.0
CRAP 336.4
↑ Summary
1 #! perl
2  
3 package App::File::Grepper;
4  
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 App::File::Grepper - Greps files for pattern
11  
12 =cut
13  
14 1 our $VERSION = '1.04';
15  
16 =head1 SYNOPSIS
17  
18 Runs a File::Find on the specified directories, and greps all text
19 files for a pattern.
20  
21 use App::File::Grepper;
22 App::File::Grepper->main( $options, @dirs );
23  
24 =head1 RATIONALE
25  
26 There are many tools that can do this, e.g. C<ack>. However none of
27 these can call an editor when a file matches the search argument and
28 that is something B<I> often need.
29  
30 =head1 OPTIONS
31  
32 The first argument to the main() method is a reference to a hash with
33 options.
34  
35 =over 4
36  
37 =item pattern
38  
39 The pattern to grep. If it starts with a slash it is interpreted as a
40 perl pattern. Otherwise it is assumed to be a literal text to grep
41 for.
42  
43 If the text does not contain any uppercase letters, matching will be
44 done case-insensitive unless overridden by option ignorecase.
45  
46 =item ignorecase
47  
48 If defined, matching will be case-insensitive according to the value
49 of ignorecase.
50  
51 =item edit-with-emacs
52  
53 Pass each file where the pattern is found to the emacs editor client.
54  
55 =item edit-with-vi
56  
57 Pass each file where the pattern is found to the vi editor.
58  
59 =item view
60  
61 Pass each file where the pattern is found to the less viewer.
62  
63 =item filter
64  
65 A list of perl patterns to select which files must be processed. Note
66 that these patterns are applied to the basename of each file, not the
67 full path.
68  
69 =item exclude
70  
71 A list of perl patterns to select which files must be rejected. Note
72 that these patterns are applied to the basename of each file, not the
73 full path. Also, these pattern are applied before the filter patterns.
74  
75 Version control directories C<RCS>, C<CVS>, C<.svn>, C<.git> and
76 C<.hg> are always excluded, as are common editor backups.
77  
78 =back
79  
80 =cut
81  
82 1 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 1 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85 0 ▶sub main {
Subroutine main: not called CC=16 SCAR=55.5
Pod undocumented
86  
87 0 my $self = shift;
88 0 ▶ unshift(@_, $self) unless UNIVERSAL::isa( $self, __PACKAGE__ );
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($self, __PACKAGE__) 0 0 0
89 0 my $opts = shift;
90 0 my @dirs = @_;
91  
92 0 my $pat = $opts->{pattern};
93 0 my $edit = "";
94 0 ▶ if ( $opts->{'edit-with-emacs'} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($opts->{'edit-with-emacs'}) { } 0 0 0
elsif ($opts->{'edit-with-vi'}) { } 0 0 0
elsif ($opts->{'view'}) { } 0 0 0
95 0 $edit = 'emacs';
96 }
97 elsif ( $opts->{'edit-with-vi'} ) {
98 0 $edit = 'vi';
99 }
100 elsif ( $opts->{'view'} ) {
101 0 $edit = 'less';
102 }
103  
104 my $ignorecase =
105 defined($opts->{ignorecase})
106 ? $opts->{ignorecase}
107 0 ▶ : $pat !~ /[A-Z]/;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'ignorecase'} ? : 0 0 0
108  
109 0 my $opat = $pat;
110  
111 0 ▶ $pat = $pat =~ m;^/(.*);
Branch: 3 branches
BranchTrueFalseTotal
$pat =~ m[^/(.*)] ? : 0 0 0
$ignorecase ? : 0 0 0
$ignorecase ? : 0 0 0
112 ? $ignorecase ? qr/$1/i : qr/$1/
113 : $ignorecase ? qr/\Q$pat\E/i : qr/\Q$pat\E/;
114 0 ▶ warn("PAT: $pat\n") if $opts->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"debug"} 0 0 0
115  
116 *hilite = ( !$edit && -t STDOUT )
117 0 ▶ ? sub { color('red bold').$_[0].color('reset') }
Subroutine __ANON__: not called CC=1 SCAR=6.9
118 0 ▶ : sub { $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
!$edit && -t STDOUT ? : 0 0 0
Condition0%0/3
not $edit and -t STDOUT
!ll&&!rl&&r
000
MC/DC0%0/2
not $edit and -t STDOUT
not $edit -t STDOUT
Truth table0%0/3
not(A) and B
A= not $editB= -t STDOUT
ABresult
0X0
100
111
119  
120 0 my $filter;
121 0 ▶ if ( defined $opts->{filter} && @{$opts->{filter}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"filter"} and @{$opts->{"filter"};}) 0 0 0
Condition0%0/3
defined $opts->{"filter"} and @{$opts->{"filter"};}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts->{"filter"} and @{$opts->{"filter"};}
defined $opts->{"filter"} @{$opts->{"filter"};}
Truth table0%0/3
A and B
A= defined $opts->{"filter"}B= @{$opts->{"filter"};}
ABresult
0X0
100
111
122 0 $filter = '(?:' . join('|', @{$opts->{filter}}) . ')';
123 0 $filter = qr/$filter/;
124 }
125 0 my $exclude;
126 0 ▶ if ( defined $opts->{exclude} && @{$opts->{exclude}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{'exclude'} and @{$opts->{'exclude'};}) { } 0 0 0
Condition0%0/3
defined $opts->{"exclude"} and @{$opts->{"exclude"};}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts->{"exclude"} and @{$opts->{"exclude"};}
defined $opts->{"exclude"} @{$opts->{"exclude"};}
Truth table0%0/3
A and B
A= defined $opts->{"exclude"}B= @{$opts->{"exclude"};}
ABresult
0X0
100
111
127 0 $exclude = '(?:' . join('|', @{$opts->{exclude}}) . ')';
128 0 $exclude = qr/$exclude/;
129 }
130 else {
131 0 $exclude = qr{ (?: ^\# | ^\.(?>\.?)(?!/) | ~$ ) }x;
132 }
133  
134 0 binmode( STDOUT, ":utf8" );
135  
136 0 ▶ my $grepper = sub {
Subroutine __ANON__: not called CC=23 SCAR=62.0
137  
138 # Prune VC dirs. Always.
139 0 ▶ if ( -d $_ && $_ =~ /^(RCS|CVS|\.svn|\.git|\.hg)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $_ and $_ =~ /^(RCS|CVS|\.svn|\.git|\.hg)$/) 0 0 0
Condition0%0/3
-d $_ and $_ =~ /^(RCS|CVS|\.svn|\.git|\.hg)$/
!ll&&!rl&&r
000
MC/DC0%0/2
-d $_ and $_ =~ /^(RCS|CVS|\.svn|\.git|\.hg)$/
-d $_ $_ =~ /^(RCS|CVS|\.svn|\.git|\.hg)$/
Truth table0%0/3
A and B
A= -d $_B= $_ =~ /^(RCS|CVS|\.svn|\.git|\.hg)$/
ABresult
0X0
100
111
140 0 $File::Find::prune = 1;
141 0 return;
142 }
143  
144 # Files only.
145 0 ▶ return unless -f $_;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $_ 0 0 0
146  
147 # Handle include/exclude filters.
148 0 ▶ if ( $exclude && ( $_ =~ $exclude ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exclude and $_ =~ /$exclude/) 0 0 0
Condition0%0/3
$exclude and $_ =~ /$exclude/
!ll&&!rl&&r
000
MC/DC0%0/2
$exclude and $_ =~ /$exclude/
$exclude $_ =~ /$exclude/
Truth table0%0/3
A and B
A= $excludeB= $_ =~ /$exclude/
ABresult
0X0
100
111
149 0 ▶ warn("EXCL: $_ ($exclude)\n") if $opts->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"debug"} 0 0 0
150 0 return;
151 }
152 0 ▶ if ( $filter && ( $_ !~ $filter ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter and not $_ =~ /$filter/) 0 0 0
Condition0%0/3
$filter and not $_ =~ /$filter/
!ll&&!rl&&r
000
MC/DC0%0/2
$filter and not $_ =~ /$filter/
$filter not $_ =~ /$filter/
Truth table0%0/3
A and not(B)
A= $filterB= not $_ =~ /$filter/
ABresult
0X0
100
111
153 0 ▶ warn("FLTR: $_\n") if $opts->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"debug"} 0 0 0
154 0 return;
155 }
156 0 ▶ warn("FILE: $_\n") if $opts->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"debug"} 0 0 0
157  
158 0 my $file = $_;
159  
160 # Okay, we've got one.
161 0 ▶ open( my $fh, '<', $file )
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or warn("$File::Find::name: $!\n"), return 0 0 0
162 or warn("$File::Find::name: $!\n"), return;
163  
164 0 ▶ unless ( -T $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-T $fh) 0 0 0
165 0 ▶ warn("[Binary: $File::Find::name]\n") if $opts->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"verbose"} 0 0 0
166 0 return;
167 }
168  
169 0 binmode( $fh, 'raw' );
170  
171 1 ▶ use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
172  
173 0 ▶ while ( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
174  
175 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $_ = &decode("UTF-8", $_, 1) } } or $_ = &decode("iso-8859-1", $_) 0 0 0
176 0 $_ = decode( 'UTF-8', $_, 1 );
177 }
178 or $_ = decode( 'iso-8859-1', $_ );
179  
180 0 ▶ next unless s/^(.*?)($pat)/"$1".hilite($2)/ge;
Branch: 1 branch
BranchTrueFalseTotal
unless s/^(.*?)($pat)/"$1" . hilite($2);/eg 0 0 0
181  
182 0 ▶ if ( $edit eq 'vi') {
Branch: 3 branches
BranchTrueFalseTotal
if ($edit eq 'vi') { } 0 0 0
elsif ($edit eq 'emacs') { } 0 0 0
elsif ($edit eq 'view') { } 0 0 0
183 0 ▶ system( "vi",
Branch: 1 branch
BranchTrueFalseTotal
$ignorecase ? : 0 0 0
184 $ignorecase ? ( "+set ignorecase" ) : (),
185 "+/$opat",
186 $file );
187 }
188 elsif ( $edit eq 'emacs') {
189 0 system( "emacsclient",
190 "+$.:" . (1+length($1)),
191 $file );
192 }
193 elsif ( $edit eq 'view') {
194 0 ▶ system( "less",
Branch: 1 branch
BranchTrueFalseTotal
$ignorecase ? : 0 0 0
195 $ignorecase ? ( "-i" ) : (),
196 "+/$opat",
197 $file );
198 }
199 0 ▶ last if $edit;
Branch: 1 branch
BranchTrueFalseTotal
if $edit 0 0 0
200  
201 0 print( $File::Find::name, ':',
202 $., ':',
203 1+length($1), ':',
204 $_
205 );
206 }
207  
208 0 close($fh);
209 0 };
210  
211 0 find( { wanted => $grepper,
212 no_chdir => 1,
213 }, @dirs );
214 }
215  
216 =head1 AUTHOR
217  
218 Johan Vromans, C<< <JV at cpan.org> >>
219  
220 =head1 SUPPORT AND DOCUMENTATION
221  
222 Development of this module takes place on GitHub:
223 https://github.com/sciurius/afg.
224  
225 You can find documentation for this module with the perldoc command.
226  
227 perldoc App::File::Grepper
228  
229 Please report any bugs or feature requests using the issue tracker on
230 GitHub.
231  
232 =head1 ACKNOWLEDGEMENTS
233  
234 =head1 COPYRIGHT & LICENSE
235  
236 Copyright 2012,2016,2022,2026 Johan Vromans, all rights reserved.
237  
238 This program is free software; you can redistribute it and/or modify it
239 under the same terms as Perl itself.
240  
241 =cut
242  
243 1 1; # End of App::File::Grepper
↑ Summary