blib/lib/App/Ack.pm

filter: Statementstmt 81.8% 198 / 242 Branchbran 76.6% 46 / 60 Conditioncond 75.0% 6 / 8 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 85.2% 29 / 34 Podpod 45.4% 10 / 22 totaltotal 78.9% 292 / 370 CC 43 SCAR 19.0
CC
43
Cov
81%
build_regex
17.2
build_all_regexes
7.0
set_up_pager
6.5
CRAP 6.7
« blib/script/ack ↑ Summary blib/lib/App/Ack/File.pm »
1 package App::Ack;
2  
3 594 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 594 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 App::Ack
9  
10 =head1 DESCRIPTION
11  
12 A container for functions for the ack program.
13  
14 =cut
15  
16 594 ▶no re 'eval'; # enforce default, block -Mre=eval to block RCE security fail
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 594 our $VERSION;
19 594 our $COPYRIGHT;
20 594 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
21 594 $VERSION = 'v3.10.0'; # Check https://beyondgrep.com/ for updates
22 594 $COPYRIGHT = 'Copyright 2005-2026 Andy Lester.';
23 }
24 594 our $STANDALONE = 0;
25 594 our $ORIGINAL_PROGRAM_NAME;
26  
27 594 our $fh;
28  
29 594 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
30 594 $fh = *STDOUT;
31 }
32  
33  
34 594 our %types;
35 594 our %type_wanted;
36 594 our %mappings;
37 594 our %ignore_dirs;
38  
39 594 our $is_filter_mode;
40 594 our $output_to_pipe;
41  
42 594 our $is_windows;
43  
44 594 our $debug_nopens = 0;
45  
46 # Line ending, changes to "\0" if --print0.
47 594 our $ors = "\n";
48  
49 594 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
50 # These have to be checked before any filehandle diddling.
51 594 $output_to_pipe = not -t *STDOUT;
52 594 $is_filter_mode = -p STDIN;
53  
54 594 $is_windows = ($^O eq 'MSWin32');
55 }
56  
57 =head1 SYNOPSIS
58  
59 If you want to know about the F<ack> program, see the F<ack> file itself.
60  
61 No user-serviceable parts inside. F<ack> is all that should use this.
62  
63 =head1 FUNCTIONS
64  
65 =head2 warn( @_ )
66  
67 Put out an ack-specific warning.
68  
69 =cut
70  
71 9 ▶sub warn {
Subroutine warn: called CC=1 SCAR=0.0
Pod documented
72 9 return CORE::warn( _my_program(), ': ', @_, "\n" );
73 }
74  
75 =head2 die( @msgs )
76  
77 Die in an ack-specific way.
78  
79 =cut
80  
81 245 ▶sub die {
Subroutine die: called CC=1 SCAR=0.0
Pod documented
82 245 return CORE::die( _my_program(), ': ', @_, "\n" );
83 }
84  
85 254 ▶sub _my_program {
Subroutine _my_program: called CC=1 SCAR=0.0
86 254 require File::Basename;
87 254 return File::Basename::basename( $0 );
88 }
89  
90  
91 1 ▶sub thpppt {
Subroutine thpppt: called CC=1 SCAR=0.0
Pod undocumented
92 1 my $y = q{_ /|,\\'!.x',=(www)=, U };
93 1 $y =~ tr/,x!w/\nOo_/;
94  
95 1 App::Ack::print( "$y ack $_[0]!\n" );
96 1 exit 0;
97 }
98  
99 1 ▶sub ackbar {
Subroutine ackbar: called CC=1 SCAR=0.0
Pod undocumented
100 1 my $x;
101 1 $x = <<'_BAR';
102 6?!I'7!I"?%+!
103 3~!I#7#I"7#I!?!+!="+"="+!:!
104 2?#I!7!I!?#I!7!I"+"=%+"=#
105 1?"+!?*+!=#~"=!+#?"="+!
106 0?"+!?"I"?&+!="~!=!~"=!+%="+"
107 /I!+!?)+!?!+!=$~!=!~!="+!="+"?!="?!
108 .?%I"?%+%='?!=#~$="
109 ,,!?%I"?(+$=$~!=#:"~$:!~!
110 ,I!?!I!?"I"?!+#?"+!?!+#="~$:!~!:!~!:!,!:!,":#~!
111 +I!?&+!="+!?#+$=!~":!~!:!~!:!,!:#,!:!,%:"
112 *+!I!?!+$=!+!=!+!?$+#=!~":!~":#,$:",#:!,!:!
113 *I!?"+!?!+!=$+!?#+#=#~":$,!:",!:!,&:"
114 )I!?$=!~!=#+"?!+!=!+!=!~!="~!:!~":!,'.!,%:!~!
115 (=!?"+!?!=!~$?"+!?!+!=#~"=",!="~$,$.",#.!:!=!
116 (I"+"="~"=!+&=!~"=!~!,!~!+!=!?!+!?!=!I!?!+"=!.",!.!,":!
117 %I$?!+!?!=%+!~!+#~!=!~#:#=!~!+!~!=#:!,%.!,!.!:"
118 $I!?!=!?!I!+!?"+!=!~!=!~!?!I!?!=!+!=!~#:",!~"=!~!:"~!=!:",&:" '-/
119 $?!+!I!?"+"=!+"~!,!:"+#~#:#,"=!~"=!,!~!,!.",!:".!:! */! !I!t!'!s! !a! !g!r!e!p!!! !/!
120 $+"=!+!?!+"~!=!:!~!:"I!+!,!~!=!:!~!,!:!,$:!~".&:"~!,# (-/
121 %~!=!~!=!:!.!+"~!:!,!.!,!~!=!:$.!,":!,!.!:!~!,!:!=!.#="~!,!:" ./!
122 %=!~!?!+"?"+!=!~",!.!:!?!~!.!:!,!:!,#.!,!:","~!:!=!~!=!:",!~! ./!
123 %+"~":!~!=#~!:!~!,!.!~!:",!~!=!~!.!:!,!.",!:!,":!=":!.!,!:!7! -/!
124 %~",!:".#:!=!:!,!:"+!:!~!:!.!,!~!,!.#,!.!,$:"~!,":"~!=! */!
125 &=!~!=#+!=!~",!.!:",#:#,!.",+:!,!.",!=!+!?!
126 &~!=!~!=!~!:"~#:",!.!,#~!:!.!+!,!.",$.",$.#,!+!I!?!
127 &~!="~!:!~":!~",!~!=!~":!,!:!~!,!:!,&.$,#."+!?!I!?!I!
128 &~!=!~!=!+!,!:!~!:!=!,!:!~&:$,!.!,".!,".!,#."~!+!?$I!
129 &~!=!~!="~!=!:!~":!,!~%:#,!:",!.!,#.",#I!7"I!?!+!?"I"
130 &+!I!7!:#~"=!~!:!,!:"~$.!=!.!,!~!,$.#,!~!7!I#?!+!?"I"7!
131 %7#?!+!~!:!=!~!=!~":!,!:"~":#.!,)7#I"?"I!7&
132 %7#I!=":!=!~!:"~$:"~!:#,!:!,!:!~!:#,!7#I!?#7)
133 $7$+!,!~!=#~!:!~!:!~$:#,!.!~!:!=!,":!7#I"?#7+=!?!
134 $7#I!~!,!~#=!~!:"~!:!,!:!,#:!=!~",":!7$I!?#I!7*+!=!+"
135 "I!7$I!,":!,!.!=":$,!:!,$:$7$I!+!?"I!7+?"I!7!I!7!,!
136 !,!7%I!:",!."~":!,&.!,!:!~!I!7$I!+!?"I!7,?!I!7',!
137 !7(,!.#~":!,%.!,!7%I!7!?#I"7,+!?!7*
138 7+:!,!~#,"=!7'I!?#I"7/+!7+
139 77I!+!7!?!7!I"71+!7,
140 _BAR
141  
142 1 return _pic_decode($x);
143 }
144  
145 1 ▶sub cathy {
Subroutine cathy: called CC=1 SCAR=0.0
Pod undocumented
146 1 my $x = <<'CATHY';
147 0+!--+!
148 0|! "C!H!O!C!O!L!A!T!E!!! !|!
149 0|! "C!H!O!C!O!L!A!T!E!!! !|!
150 0|! "C!H!O!C!O!L!A!T!E!!! !|!
151 0|! $A"C!K!!! $|!
152 0+!--+!
153 6\! 1:!,!.! !
154 7\! /.!M!~!Z!M!~!
155 8\! /~!D! "M! !
156 4.! $\! /M!~!.!8! +.!M# 4
157 0,!.! (\! .~!M!N! ,+!I!.!M!.! 3
158 /?!O!.!M!:! '\! .O!.! +~!Z!=!N!.! 4
159 ..! !D!Z!.!Z!.! '\! 9=!M".! 6
160 /.! !.!~!M".! '\! 8~! 9
161 4M!.! /.!7!N!M!.! F
162 4.! &:!M! !N"M# !M"N!M! #D!M&=! =
163 :M!7!M#:! !~!M!7!,!$!M!:! #.! !O!N!.!M!:!M# ;
164 8Z!M"~!N!$!D!.!N!?! !I!N!.! (?!M! !M!,!D!M".! 9
165 (?!Z!M!N!:! )=!M!O!8!.!M!+!M! !M!,! !O!M! +,!M!.!M!~!Z!N!M!:! &:!~! 0
166 &8!7!.!~!M"D!M!,! &M!?!=!8! !M!,!O! !M!+! !+!O!.!M! $M#~! !.!8!M!Z!.!M! !O!M"Z! %:!~!M!Z!M!Z!.! +
167 &:!M!7!,! *M!.!Z!M! !8"M!.!M!~! !.!M!.!=! #~!8!.!M! !7!M! "N!Z#I! !D!M!,!M!.! $."M!,! !M!.! *
168 2$!O! "N! !.!M!I! !7" "M! "+!O! !~!M! !d!O!.!7!I!M!.! !.!O!=!M!.! !M",!M!.! %.!$!O!D! +
169 1~!O! "M!+! !8!$! "M! "?!O! %Z!8!D!M!?!8!I!O!7!M! #M!.!M! "M",!M! 4
170 07!~! ".!8! !.!M! "I!+! !.!M! &Z!D!.!7!=!M! !:!.!M! #:!8"+! !.!+!8! !8! 3
171 /~!M! #N! !~!M!$! !.!M! !.!M" &~!M! "~!M!O! "D! $M! !8! "M!,!M!+!D!.! 1
172 #.! #?!M!N!.! #~!O! $M!.!7!$! "?" !?!~!M! '7!8!?!M!.!+!M"O! $?"$!D! !.!O! !$!7!I!.! 0
173 $,!M!:!O!?! ".! !?!=! $=!:!O! !M! "M! !M! !+!$! (.! +.!M! !M!.! !8! !+"Z!~! $:!M!$! !.! '
174 #.!8!.!I!$! $7!I! %M" !=!M! !~!M!D! "7!I! .I!O! %?!=!,!D! !,!M! !D!~!8!~! %D!M! (
175 #.!M"?! $=!O! %=!N! "8!.! !Z!M! #M!~! (M!:! #.!M" &O! !M!.! !?!,! !8!.!N!~! $8!N!M!,!.! %
176 *$!O! &M!,! "O! !.!M!.! #M! (~!M( &O!.! !7! "M! !.!M!.!M!,! #.!M! !M! &
177 )=!8!.! $.!M!O!.! "$!.!I!N! !I!M# (7!M(I! %D"Z!M! "=!I! "M! !M!:! #~!D! '
178 )D! &8!N!:! ".!O! !M!="M! "M! (7!M) %." !M!D!."M!.! !$!=! !M!,! +
179 (M! &+!.!M! #Z!7!O!M!.!~!8! +,!M#D!?!M#D! #.!Z!M#,!Z!?! !~!N! "N!.! !M! +
180 'D!:! %$!D! !?! #M!Z! !8!.! !M"?!7!?!7! '+!I!D! !?!O!:!M!:! ":!M!:! !M!7".!M! "8!+! !:!D! !.!M! *
181 %.!O!:! $.!O!+! !D!.! #M! "M!.!+!N!I!Z! "7!M!N!M!N!?!I!7!Z!=!M'D"~! #M!.!8!$! !:! !.!M! "N!?! !,!O! )
182 !.!?!M!:!M!I! %8!,! "M!.! #M! "N! !M!.! !M!.! !+!~! !.!M!.! ':!M! $M! $M!Z!$! !M!.! "D! "M! "?!M! (
183 !7!8! !+!I! ".! "$!=! ":!$! "+! !M!.! !O! !M!I!M".! !=!~! ",!O! '=!M! $$!,! #N!:! ":!8!.! !D!~! !,!M!.! !:!M!.! &
184 !:!,!.! &Z" #D! !.!8!."M!.! !8!?!Z!M!.!M! #Z!~! !?!M!Z!.! %~!O!.!8!$!N!8!O!I!:!~! !+! #M!.! !.!M!.! !+!M! ".!~!M!+! $
185 !.! 'D!I! #?!M!.!M!,! !.!Z! !.!8! #M&O!I!?! (~!I!M"." !M!Z!.! !M!N!.! "+!$!.! "M!.! !M!?!.! "8!M! $
186 (O!8! $M! !M!.! ".!:! !+!=! #M! #.!M! !+" *$!M":!.! !M!~! "M!7! #M! #7!Z! "M"$!M!.! !.! #
187 '$!Z! #.!7!+!M! $.!,! !+!:! #N! #.!M!.!+!M! +D!M! #=!N! ":!O! #=!M! #Z!D! $M!I! %
188 $,! ".! $.!M" %$!.! !?!~! "+!7!." !.!M!,! !M! *,!N!M!.$M!?! "D!,! #M!.! #N! +
189 ,M!Z! &M! "I!,! "M! %I!M! !?!=!.! (Z!8!M! $:!M!.! !,!M! $D! #.!M!.! )
190 +8!O! &.!8! "I!,! !~!M! &N!M! !M!D! '?!N!O!." $?!7! "?!~! #M!.! #I!D!.! (
191 3M!,! "N!.! !D" &.!+!M!.! !M":!.":!M!7!M!D! 'M!.! "M!.! "M!,! $I! )
192 3I! #M! "M!,! !:! &.!M" ".!,! !.!$!M!I! #.! !:! !.!M!?! "N!+! ".! /
193 1M!,! #.!M!8!M!=!.! +~!N"O!Z"~! *+!M!.! "M! 2
194 0.!M! &M!.! 8:! %.!M!Z! "M!=! *O!,! %
195 0?!$! &N! )." .,! %."M! ":!M!.! 0
196 0N!:! %?!O! #.! ..! &,! &.!D!,! "N!I! 0
197 CATHY
198 1 return _pic_decode($x);
199 }
200  
201 2 ▶sub _pic_decode {
Subroutine _pic_decode: called CC=1 SCAR=0.0
202 2 my($compressed) = @_;
203 2 $compressed =~ s/(.)(.)/$1x(ord($2)-32)/eg;
204 2 App::Ack::print( $compressed );
205 2 exit 0;
206 }
207  
208 =head2 show_help()
209  
210 Dumps the help page to the user.
211  
212 =cut
213  
214 3 ▶sub show_help {
Subroutine show_help: called CC=1 SCAR=0.0
Pod documented
215 3 App::Ack::print( <<"END_OF_HELP" );
216 Usage: ack [OPTION]... PATTERN [FILES OR DIRECTORIES]
217  
218 Search for PATTERN in each source file in the tree from the current
219 directory on down. If any files or directories are specified, then
220 only those files and directories are checked. ack may also search
221 STDIN, but only if no file or directory arguments are specified,
222 or if one of them is "-".
223  
224 Default switches may be specified in an .ackrc file. If you want no dependency
225 on the environment, turn it off with --noenv.
226  
227 File select actions:
228 -f Only print the files selected, without
229 searching. The PATTERN must not be specified.
230 -g Same as -f, but only select files matching
231 PATTERN.
232  
233 File listing actions:
234 -l, --files-with-matches Print filenames with at least one match
235 -L, --files-without-matches Print filenames with no matches
236 -c, --count Print filenames and count of matching lines
237  
238 Searching:
239 -i, --ignore-case Ignore case distinctions in PATTERN
240 -S, --[no]smart-case Ignore case distinctions in PATTERN,
241 only if PATTERN contains no upper case.
242 Ignored if -i or -I are specified.
243 -I, --no-ignore-case Turns on case-sensitivity in PATTERN.
244 Negates -i and --smart-case.
245 -v, --invert-match Invert match: select non-matching lines
246 -w, --word-regexp Force PATTERN to match only whole words
247 -Q, --literal Quote all metacharacters; PATTERN is literal
248 --range-start PATTERN Specify PATTERN as the start of a match range.
249 --range-end PATTERN Specify PATTERN as the end of a match range.
250 --match PATTERN Specify PATTERN explicitly. Typically omitted.
251 --and PATTERN Specifies PATTERN that MUST also be found on
252 the line for a match to occur. Repeatable.
253 --or PATTERN Specifies PATTERN that MAY also be found on
254 the line for a match to occur. Repeatable.
255 --not PATTERN Specifies PATTERN that must NOT be found on
256 the line for a match to occur. Repeatable.
257  
258 Search output:
259 --output=expr Output the evaluation of expr for each line
260 (turns off text highlighting)
261 -o Show only the part of a line matching PATTERN
262 Same as --output='\$&'
263 --passthru Print all lines, whether matching or not
264 -m, --max-count=NUM Stop searching in each file after NUM matches
265 -1 Stop searching after one match of any kind
266 -H, --with-filename Print the filename for each match (default:
267 on unless explicitly searching a single file)
268 -h, --no-filename Suppress the prefixing filename on output
269 --[no]column Show the column number of the first match
270  
271 -A NUM, --after-context=NUM Print NUM lines of trailing context after
272 matching lines.
273 -B NUM, --before-context=NUM Print NUM lines of leading context before
274 matching lines.
275 -C [NUM], --context[=NUM] Print NUM lines (default 2) of output context.
276  
277 --print0 Print null byte as separator between filenames,
278 only works with -f, -g, -l, -L or -c.
279  
280 -s Suppress error messages about nonexistent or
281 unreadable files.
282  
283  
284 File presentation:
285 --pager=COMMAND Pipes all ack output through COMMAND. For
286 example, --pager="less -R". Ignored if output
287 is redirected.
288 --nopager Do not send output through a pager. Cancels
289 any setting in ~/.ackrc, ACK_PAGER or
290 ACK_PAGER_COLOR.
291 --[no]heading Print a filename heading above each file's
292 results. (default: on when used interactively)
293 --[no]break Print a break between results from different
294 files. (default: on when used interactively)
295 --group Same as --heading --break
296 --nogroup Same as --noheading --nobreak
297 -p, --proximate=LINES Separate match output with blank lines unless
298 they are within LINES lines from each other.
299 -P, --proximate=0 Negates --proximate.
300 --[no]underline Print a line of carets under the matched text.
301 --[no]color, --[no]colour Highlight the matching text (default: on unless
302 output is redirected, or on Windows)
303 --color-filename=COLOR
304 --color-match=COLOR
305 --color-colno=COLOR
306 --color-lineno=COLOR Set the color for filenames, matches, line and
307 column numbers.
308 --help-colors Show a list of possible color combinations.
309 --help-rgb-colors Show a list of advanced RGB colors.
310 --flush Flush output immediately, even when ack is used
311 non-interactively (when output goes to a pipe or
312 file).
313  
314  
315 File finding:
316 --sort-files Sort the found files lexically.
317 --show-types Show which types each file has.
318 --files-from=FILE Read the list of files to search from FILE.
319 -x Read the list of files to search from STDIN.
320  
321 File inclusion/exclusion:
322 --[no]ignore-dir=name Add/remove directory from list of ignored dirs
323 --[no]ignore-directory=name Synonym for ignore-dir
324 --ignore-file=FILTER:ARGS Add filter for ignoring files.
325 -r, -R, --recurse Recurse into subdirectories (default: on)
326 -n, --no-recurse No descending into subdirectories
327 --[no]follow Follow symlinks. Default is off.
328  
329 File type inclusion/exclusion:
330 -t X, --type=X Include only X files, where X is a filetype,
331 e.g. python, html, markdown, etc
332 -T X, --type=noX Exclude X files, where X is a filetype.
333 -k, --known-types Include only files of types that ack recognizes.
334 --help-types Display all known types, and how they're defined.
335  
336 File type specification:
337 --type-set=TYPE:FILTER:ARGS Files with the given ARGS applied to the given
338 FILTER are recognized as being of type TYPE.
339 This replaces an existing definition for TYPE.
340 --type-add=TYPE:FILTER:ARGS Files with the given ARGS applied to the given
341 FILTER are recognized as being type TYPE.
342 --type-del=TYPE Removes all filters associated with TYPE.
343  
344 Miscellaneous:
345 --version Display version & copyright
346 --[no]env Ignore environment variables and global ackrc
347 files. --env is legal but redundant.
348 --ackrc=filename Specify an ackrc file to use
349 --ignore-ack-defaults Ignore default definitions included with ack.
350 --create-ackrc Outputs a default ackrc for your customization
351 to standard output.
352 --dump Dump information on which options are loaded
353 and where they're defined.
354 --[no]filter Force ack to treat standard input as a pipe
355 (--filter) or tty (--nofilter)
356 --help This help
357 --man Print the manual.
358 --help-types Display all known types, and how they're defined.
359 --help-colors Show a list of possible color combinations.
360 --help-rgb-colors Show a list of advanced RGB colors.
361 --thpppt Bill the Cat
362 --bar The warning admiral
363 --cathy Chocolate! Chocolate! Chocolate!
364  
365 Filter specifications:
366 If FILTER is "ext", ARGS is a list of extensions checked against the
367 file's extension.
368 If FILTER is "is", ARGS must match the file's name exactly.
369 If FILTER is "match", ARGS is matched as a case-insensitive regex
370 against the filename.
371 If FILTER is "firstlinematch", ARGS is matched as a regex the first
372 line of the file's contents.
373  
374 Exit status is 0 if match, 1 if no match.
375  
376 ack's home page is at https://beyondgrep.com/
377  
378 The full ack manual is available by running "ack --man".
379  
380 This is version $App::Ack::VERSION of ack. Run "ack --version" for full version info.
381 END_OF_HELP
382  
383 3 return;
384 }
385  
386  
387 =head2 show_help_types()
388  
389 Display the filetypes help subpage.
390  
391 =cut
392  
393 5 ▶sub show_help_types {
Subroutine show_help_types: called CC=6 SCAR=18.0
Pod documented
394 5 App::Ack::print( <<'END_OF_HELP' );
395 Usage: ack [OPTION]... PATTERN [FILES OR DIRECTORIES]
396  
397 The following is the list of filetypes supported by ack. You can specify a
398 filetype to include with -t TYPE or --type=TYPE. You can exclude a
399 filetype with -T TYPE or --type=noTYPE.
400  
401 Note that some files may appear in multiple types. For example, a file
402 called Rakefile is both Ruby (--type=ruby) and Rakefile (--type=rakefile).
403  
404 END_OF_HELP
405  
406 5 my @types = keys %App::Ack::mappings;
407 5 my $maxlen = 0;
408 5 for ( @types ) {
409 403 ▶ $maxlen = length if $maxlen < length;
Branch: 1 branch
BranchTrueFalseTotal
if $maxlen < length $_ 16 387 403
410 }
411 5 for my $type ( sort @types ) {
412 403 ▶ next if $type =~ /^-/; # Stuff to not show
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /^-/ 0 403 403
413 403 my $ext_list = $mappings{$type};
414  
415 403 ▶ if ( ref $ext_list ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ext_list) 403 0 403
416 403 $ext_list = join( '; ', map { $_->to_string } @{$ext_list} );
417 }
418 403 App::Ack::print( sprintf( " %-*.*s %s\n", $maxlen, $maxlen, $type, $ext_list ) );
419 }
420  
421 5 return;
422 }
423  
424  
425 =head2 show_help_colors()
426  
427 Display the colors help subpage.
428  
429 =cut
430  
431 0 ▶sub show_help_colors {
Subroutine show_help_colors: not called CC=1 SCAR=6.9
Pod documented
432 0 App::Ack::print( <<'END_OF_HELP' );
433 ack allows customization of the colors it uses when presenting matches
434 onscreen. See the "ACK COLORS" section of the ack manual (ack --man).
435  
436 Here is a chart of how various color combinations appear: Each of the eight
437 foreground colors, on each of the eight background colors or no background
438 color, with and without the bold modifier.
439  
440 Run ack --help-rgb-colors for a chart of the RGB colors.
441  
442 END_OF_HELP
443  
444 0 _show_color_grid();
445  
446 0 return;
447 }
448  
449  
450 =head2 show_help_rgb()
451  
452 Display the RGB help subpage.
453  
454 =cut
455  
456 0 ▶sub show_help_rgb {
Subroutine show_help_rgb: not called CC=1 SCAR=6.9
Pod documented
457 0 App::Ack::print( <<'END_OF_HELP' );
458 ack allows customization of the colors it uses when presenting matches
459 onscreen. See the "ACK COLORS" section of the ack manual (ack --man).
460  
461 Colors may be specified as "rgbNNN" where "NNN" is a triplet of digits
462 from 0 to 5 specifying the intensity of red, green and blue, respectively.
463  
464 Here is a grid of the 216 possible values for NNN.
465  
466 END_OF_HELP
467  
468 0 _show_rgb_grid();
469  
470 0 App::Ack::say( 'Here are the 216 possible colors with the "reverse" modifier applied.', "\n" );
471  
472 0 _show_rgb_grid( 'reverse' );
473  
474 0 return;
475 }
476  
477  
478 0 ▶sub _show_color_grid {
Subroutine _show_color_grid: not called CC=2 SCAR=17.9
479 0 my $cell_width = 7;
480  
481 0 my @fg_colors = qw( black red green yellow blue magenta cyan white );
482 0 my @bg_colors = map { "on_$_" } @fg_colors;
483  
484 App::Ack::say(
485 _color_cell( '' ),
486 0 map { _color_cell( $_ ) } @fg_colors
487 );
488  
489 App::Ack::say(
490 _color_cell( '' ),
491 0 map { _color_cell( '-' x $cell_width ) } @fg_colors
492 );
493  
494 0 for my $bg ( '', @bg_colors ) {
495 App::Ack::say(
496 _color_cell( '' ),
497 0 ( map { _color_cell( $_, "$_ $bg" ) } @fg_colors ),
498 $bg
499 );
500  
501 App::Ack::say(
502 _color_cell( 'bold' ),
503 0 ( map { _color_cell( $_, "bold $_ $bg" ) } @fg_colors ),
504 $bg
505 );
506 0 App::Ack::say();
507 }
508  
509 0 return;
510 }
511  
512  
513 0 ▶sub _color_cell {
Subroutine _color_cell: not called CC=2 SCAR=17.9
514 0 my $text = shift;
515 0 my $color = shift;
516  
517 0 my $cell_width = 7;
518 0 $text = sprintf( '%-*s', $cell_width, $text );
519  
520 0 ▶ return ($color ? Term::ANSIColor::colored( $text, $color ) : $text) . ' ';
Branch: 1 branch
BranchTrueFalseTotal
$color ? : 0 0 0
521 }
522  
523  
524 0 ▶sub _show_rgb_grid {
Subroutine _show_rgb_grid: not called CC=2 SCAR=17.9
525 0 ▶ my $modifier = shift // '';
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
526  
527 0 my $grid = <<'HERE';
528 544 544 544 544 544 554 554 554 554 554 454 454 454 454 454 455 455 455 455 455 445 445 445 445 445 545 545 545 545 545
529 533 533 533 543 543 553 553 553 453 453 353 353 353 354 354 355 355 355 345 345 335 335 335 435 435 535 535 535 534 534
530 511 521 531 531 541 551 451 451 351 251 151 152 152 153 154 155 145 145 135 125 115 215 215 315 415 515 514 514 513 512
531 500 510 520 530 540 550 450 350 250 150 050 051 052 053 054 055 045 035 025 015 005 105 205 305 405 505 504 503 502 501
532 400 410 410 420 430 440 340 340 240 140 040 041 041 042 043 044 034 034 024 014 004 104 104 204 304 404 403 403 402 401
533 300 300 310 320 320 330 330 230 130 130 030 030 031 032 032 033 033 023 013 013 003 003 103 203 203 303 303 302 301 301
534 200 200 200 210 210 220 220 220 120 120 020 020 020 021 021 022 022 022 012 012 002 002 002 102 102 202 202 202 201 201
535 100 100 100 100 100 110 110 110 110 110 010 010 010 010 010 011 011 011 011 011 001 001 001 001 001 101 101 101 101 101
536  
537 522 522 532 542 542 552 552 452 352 352 252 252 253 254 254 255 255 245 235 235 225 225 325 425 425 525 525 524 523 523
538  
539 411 411 421 431 431 441 441 341 241 241 141 141 142 143 143 144 144 134 124 124 114 114 214 314 314 414 414 413 412 412
540  
541 422 422 432 432 432 442 442 442 342 342 242 242 242 243 243 244 244 244 234 234 224 224 224 324 324 424 424 424 423 423
542  
543 311 311 311 321 321 331 331 331 231 231 131 131 131 132 132 133 133 133 123 123 113 113 113 213 213 313 313 313 312 312
544  
545 433 433 433 433 433 443 443 443 443 443 343 343 343 343 343 344 344 344 344 344 334 334 334 334 334 434 434 434 434 434
546 211 211 211 211 211 221 221 221 221 221 121 121 121 121 121 122 122 122 122 122 112 112 112 112 112 212 212 212 212 212
547  
548 322 322 322 322 322 332 332 332 332 332 232 232 232 232 232 233 233 233 233 233 223 223 223 223 223 323 323 323 323 323
549  
550 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555 555
551 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444 444
552 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333 333
553 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222 222
554 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111
555 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
556 HERE
557  
558 0 $grid =~ s/(\d\d\d)/Term::ANSIColor::colored( "$1", "$modifier rgb$1" )/eg;
559  
560 0 App::Ack::say( $grid );
561  
562 0 return;
563 }
564  
565  
566 2 ▶sub show_man {
Subroutine show_man: called CC=1 SCAR=0.4
Pod undocumented
567 2 require Pod::Usage;
568 2 Pod::Usage::pod2usage({
569 -input => $App::Ack::ORIGINAL_PROGRAM_NAME,
570 -verbose => 2,
571 -exitval => 0,
572 });
573  
574 0 return;
575 }
576  
577  
578 =head2 get_version_statement
579  
580 Returns the version information for ack.
581  
582 =cut
583  
584 3 ▶sub get_version_statement {
Subroutine get_version_statement: called CC=4 SCAR=14.2
Pod documented
585 3 require Config;
586  
587 3 my $copyright = $App::Ack::COPYRIGHT;
588 3 my $this_perl = $Config::Config{perlpath};
589 3 ▶ if ($^O ne 'VMS') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O ne "VMS") 3 0 3
590 3 my $ext = $Config::Config{_exe};
591 3 ▶ $this_perl .= $ext unless $this_perl =~ m/$ext$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $this_perl =~ /$ext$/i 0 3 3
592 }
593 3 my $perl_ver = sprintf( 'v%vd', $^V );
594  
595 3 ▶ my $build_type = $App::Ack::STANDALONE ? 'standalone version' : 'standard build';
Branch: 1 branch
BranchTrueFalseTotal
$App::Ack::STANDALONE ? : 0 3 3
596  
597 3 return <<"END_OF_VERSION";
598 ack $App::Ack::VERSION ($build_type)
599 Running under Perl $perl_ver at $this_perl
600  
601 $copyright
602  
603 This program is free software. You may modify or distribute it
604 under the terms of the Artistic License v2.0.
605 END_OF_VERSION
606 }
607  
608  
609 417 ▶sub print { print {$fh} @_; return; }
Subroutine print: called CC=1 SCAR=0.0
Pod undocumented
610 8038 ▶sub say { print {$fh} @_, $ors; return; }
Subroutine say: called CC=1 SCAR=0.0
Pod undocumented
611 5 ▶sub print_blank_line { print {$fh} "\n"; return; }
Subroutine print_blank_line: called CC=1 SCAR=0.0
Pod undocumented
612  
613 1 ▶sub set_up_pager {
Subroutine set_up_pager: called CC=3 SCAR=18.7
Pod undocumented
614 1 my $command = shift;
615  
616 1 ▶ return if App::Ack::output_to_pipe();
Branch: 1 branch
BranchTrueFalseTotal
if output_to_pipe() 1 0 1
617  
618 0 my $pager;
619 0 ▶ if ( not open( $pager, '|-', $command ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $pager, "|-", $command) 0 0 0
620 0 App::Ack::die( qq{Unable to pipe to pager "$command": $!} );
621 }
622 0 $fh = $pager;
623  
624 0 return;
625 }
626  
627 =head2 output_to_pipe()
628  
629 Returns true if ack's input is coming from a pipe.
630  
631 =cut
632  
633 997 ▶sub output_to_pipe {
Subroutine output_to_pipe: called CC=1 SCAR=0.0
Pod documented
634 997 return $output_to_pipe;
635 }
636  
637 =head2 exit_from_ack( $nmatches )
638  
639 Exit from the application with the correct exit code.
640  
641 Returns with 0 if a match was found, otherwise with 1. The number of matches is
642 handed in as the only argument.
643  
644 =cut
645  
646 330 ▶sub exit_from_ack {
Subroutine exit_from_ack: called CC=2 SCAR=6.9
Pod documented
647 330 my $nmatches = shift;
648  
649 330 ▶ my $rc = $nmatches ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$nmatches ? : 234 96 330
650 330 exit $rc;
651 }
652  
653 =head2 show_types( $file )
654  
655 Shows the filetypes associated with a given file.
656  
657 =cut
658  
659 2 ▶sub show_types {
Subroutine show_types: called CC=2 SCAR=7.0
Pod documented
660 2 my $file = shift;
661  
662 2 my @types = filetypes( $file );
663 2 ▶ my $arrow = @types ? ' => ' : ' =>';
Branch: 1 branch
BranchTrueFalseTotal
@types ? : 2 0 2
664 2 App::Ack::say( $file->name, $arrow, join( ',', @types ) );
665  
666 2 return;
667 }
668  
669  
670 2 ▶sub filetypes {
Subroutine filetypes: called CC=4 SCAR=13.9
Pod undocumented
671 2 my ( $file ) = @_;
672  
673 2 my @matches;
674  
675 2 foreach my $k (keys %App::Ack::mappings) {
676 162 my $filters = $App::Ack::mappings{$k};
677  
678 162 foreach my $filter (@{$filters}) {
679 # Clone the file.
680 200 my $clone = $file->clone;
681 200 ▶ if ( $filter->filter($clone) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter->filter($clone)) 4 196 200
682 4 push @matches, $k;
683 4 last;
684 }
685 }
686 }
687  
688 # https://metacpan.org/pod/distribution/Perl-Critic/lib/Perl/Critic/Policy/Subroutines/ProhibitReturnSort.pm
689 2 @matches = sort @matches;
690 2 return @matches;
691 }
692  
693  
694 443 ▶sub is_lowercase {
Subroutine is_lowercase: called CC=3 SCAR=11.0
Pod undocumented
695 443 my $pat = shift;
696  
697 # The simplest case.
698 443 ▶ return 1 if lc($pat) eq $pat;
Branch: 1 branch
BranchTrueFalseTotal
if lc $pat eq $pat 221 222 443
699  
700 # If we have capitals, then go clean up any metacharacters that might have capitals.
701  
702 # Get rid of any literal backslashes first to avoid confusion.
703 222 $pat =~ s/\\\\//g;
704  
705 222 my $metacharacter = qr{
706 |\\A # Beginning of string
707 |\\B # Not word boundary
708 |\\c[a-zA-Z] # Control characters
709 |\\D # Non-digit character
710 |\\G # End-of-match position of prior match
711 |\\H # Not horizontal whitespace
712 |\\K # Keep to the left
713 |\\N(\{.+?\})? # Anything but \n, OR Unicode sequence
714 |\\[pP]\{.+?\} # Named property and negation
715 |\\[pP][A-Z] # Named property and negation, single-character shorthand
716 |\\R # Linebreak
717 |\\S # Non-space character
718 |\\V # Not vertical whitespace
719 |\\W # Non-word character
720 |\\X # ???
721 |\\x[0-9A-Fa-f]{2} # Hex sequence
722 |\\Z # End of string
723 }x;
724 222 $pat =~ s/$metacharacter//g;
725  
726 222 my $name = qr/[_A-Za-z][_A-Za-z0-9]*?/;
727 # Eliminate named captures.
728 222 $pat =~ s/\(\?'$name'//g;
729 222 $pat =~ s/\(\?<$name>//g;
730  
731 # Eliminate named backreferences.
732 222 $pat =~ s/\\k'$name'//g;
733 222 $pat =~ s/\\k<$name>//g;
734 222 $pat =~ s/\\k\{$name\}//g;
735  
736 # Now with those metacharacters and named things removed, now see if it's lowercase.
737 222 ▶ return 1 if lc($pat) eq $pat;
Branch: 1 branch
BranchTrueFalseTotal
if lc $pat eq $pat 25 197 222
738  
739 197 return 0;
740 }
741  
742  
743 # Returns a regex object based on a string and command-line options.
744 # Dies when the regex $str is undefined (i.e. not given on command line).
745  
746 402 ▶sub build_regex {
Subroutine build_regex: called CC=17 SCAR=28.5
Pod undocumented
747 402 my $str = shift;
748 402 my $opt = shift;
749  
750 # Check for lowercaseness before we do any modifications.
751 402 my $regex_is_lc = App::Ack::is_lowercase( $str );
752  
753 402 ▶ if ( $opt->{Q} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'Q'}) { } 11 391 402
754 11 $str = quotemeta( $str );
755 }
756 else {
757 # Compile the regex to see if it dies or throws warnings.
758 391 ▶ local $SIG{__WARN__} = sub { CORE::die @_ }; # Anything that warns becomes a die.
Subroutine __ANON__: called CC=1 SCAR=0.0
759 391 my $scratch_regex = eval { qr/$str/ };
760 391 ▶ if ( not $scratch_regex ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($scratch_regex) 12 379 391
761 12 my $err = $@;
762 12 chomp $err;
763  
764 12 ▶ if ( $err =~ m{^(.+?); marked by <-- HERE in m/(.+?) <-- HERE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err =~ m[^(.+?); marked by <-- HERE in m/(.+?) <-- HERE]) { } 12 0 12
765 12 my ($why, $where) = ($1,$2);
766 12 my $pointy = ' ' x (6+length($where)) . '^---HERE';
767 12 App::Ack::die( "Invalid regex '$str'\nRegex: $str\n$pointy $why" );
768 }
769 else {
770 0 App::Ack::die( "Invalid regex '$str'\n$err" );
771 }
772 }
773 }
774  
775 390 my $scan_str = $str;
776  
777 # Whole words only.
778 390 ▶ if ( $opt->{w} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"w"}) 211 179 390
779 211 my $ok = 1;
780  
781 211 ▶ if ( $str =~ /^\\[wd]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^\\[wd]/) { } 0 211 211
782 # Explicit \w is good.
783 }
784 else {
785 # Can start with \w, (, [ or dot.
786 211 ▶ if ( $str !~ /^[\w\(\[\.]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /^[\w\(\[\.]/) 11 200 211
787 11 $ok = 0;
788 }
789 }
790  
791 # Can end with \w, }, ), ], +, *, or dot.
792 211 ▶ if ( $str !~ /[\w\}\)\]\+\*\?\.]$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $str =~ /[\w\}\)\]\+\*\?\.]$/) { } 7 204 211
elsif ($str =~ /\\[\}\)\]\+\*\?\.]$/) { } 6 198 204
793 7 $ok = 0;
794 }
795 # ... unless it's escaped.
796 elsif ( $str =~ /\\[\}\)\]\+\*\?\.]$/ ) {
797 6 $ok = 0;
798 }
799  
800 211 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 22 189 211
801 22 App::Ack::die( '-w will not do the right thing if your regex does not begin and end with a word character.' );
802 }
803  
804 189 ▶ if ( $str =~ /^\w+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^\w+$/) { } 169 20 189
805 # No need for fancy regex if it's a simple word.
806 169 $str = sprintf( '\b(?:%s)\b', $str );
807 }
808 else {
809 20 $str = sprintf( '(?:^|\b|\s)\K(?:%s)(?=\s|\b|$)', $str );
810 }
811 }
812  
813 368 ▶ if ( $opt->{i} || ($opt->{S} && $regex_is_lc) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"i"} or $opt->{"S"} and $regex_is_lc) 198 170 368
Condition100%6/6
$opt->{"i"} or $opt->{"S"} and $regex_is_lc
l!l&&r!l&&!r
1944170
$opt->{"S"} and $regex_is_lc
!ll&&!rl&&r
16644
MC/DC100%3/3
$opt->{"i"} or $opt->{"S"} and $regex_is_lc
$opt->{"i"} $opt->{"S"} $regex_is_lc
Truth table100%4/4
A or B and C
A= $opt->{"i"}B= $opt->{"S"}C= $regex_is_lc
ABCresult
1XX1
0111
00X0
0100
814 198 $_ = "(?i)$_" for ( $str, $scan_str );
815 }
816  
817 368 my $scan_regex = undef;
818 368 my $regex = eval { qr/$str/ };
819 368 ▶ if ( $regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($regex) { } 368 0 368
820 368 ▶ if ( $scan_str !~ /\$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($scan_str =~ /\$/) 352 16 368
821 # No line_scan is possible if there's a $ in the regex.
822 352 $scan_regex = eval { qr/$scan_str/m };
823 }
824 }
825 else {
826 0 my $err = $@;
827 0 chomp $err;
828 0 App::Ack::die( "Invalid regex '$str':\n $err" );
829 }
830  
831 368 return ($regex, $scan_regex);
832 }
833  
834  
835 271 ▶sub build_all_regexes {
Subroutine build_all_regexes: called CC=7 SCAR=19.5
Pod undocumented
836 271 my $opt_regex = shift;
837 271 my $opt = shift;
838  
839 271 my $re_match;
840 my $re_not;
841 271 my $re_hilite;
842 271 my $re_scan;
843  
844 271 my @parts;
845  
846 # AND: alpha AND beta
847 271 ▶ if ( @parts = @{$opt->{and}} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@parts = @{$opt->{'and'};}) { } 11 260 271
elsif (@parts = @{$opt->{'or'};}) { } 31 229 260
848 11 my @match_parts;
849 my @hilite_parts;
850  
851 11 for my $part ( @parts ) {
852 18 my ($match, undef) = build_regex( $part, $opt );
853 18 push @match_parts, "(?=.*$match)";
854 18 push @hilite_parts, $match;
855 }
856  
857 11 my ($match, $scan) = build_regex( $opt_regex, $opt );
858 11 push @match_parts, ".*$match";
859 11 push @hilite_parts, $match;
860  
861 11 $re_match = join( '', @match_parts );
862 11 $re_hilite = join( '|', @hilite_parts );
863 11 $re_scan = $scan;
864 }
865 # OR: alpha OR beta
866 260 elsif ( @parts = @{$opt->{or}} ) {
867 31 my @match_parts;
868 my @scan_parts;
869  
870 31 for my $part ( $opt_regex, @parts ) {
871 112 my ($match, $scan) = build_regex( $part, $opt );
872 112 push @match_parts, $match;
873 112 push @scan_parts, $scan;
874 }
875  
876 31 $re_match = join( '|', @match_parts );
877 31 $re_hilite = $re_match;
878 31 $re_scan = join( '|', @scan_parts );
879 }
880 else {
881 229 ($re_match, $re_scan) = build_regex( $opt_regex, $opt );
882 195 $re_hilite = $re_match;
883 }
884  
885 # The --not does not affect the main regex. It is checked separately.
886 # NOT: alpha NOT beta
887 237 ▶ if ( @parts = @{$opt->{not}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts = @{$opt->{"not"};}) 11 226 237
888 11 my @not_parts;
889 11 for my $part ( @parts ) {
890 18 (my $re, undef) = build_regex( $part, $opt );
891 18 push @not_parts, $re;
892 }
893 11 $re_not = join( '|', @not_parts );
894 }
895  
896 237 return ($re_match, $re_not, $re_hilite, $re_scan);
897 }
898  
899  
900 594 1; # End of App::Ack
« blib/script/ack ↑ Summary blib/lib/App/Ack/File.pm »