blib/lib/File/CodeSearch/Highlighter.pm

filter: Statementstmt 100% 85 / 85 Branchbran 84.3% 27 / 32 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 94.8% 130 / 137 CC 21 SCAR 25.3
CC
21
Cov
95%
highlight
19.1
__ANON__
2.1
make_highlight_re
2.0
CRAP 12.6
« blib/lib/File/CodeSearch.pm ↑ Summary blib/lib/File/CodeSearch/RegexBuilder.pm »
1 package File::CodeSearch::Highlighter;
2  
3 # Created on: 2009-08-07 18:42:16
4 # Create by: Ivan Wills
5 # $Id$
6 # $Revision$, $HeadURL$, $Date$
7 # $Revision$, $Source$, $Date$
8  
9 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Term::ANSIColor qw/:constants/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Term::Size::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 our $VERSION = version->new('0.7.5');
18  
19 4 extends 'File::CodeSearch::RegexBuilder';
20  
21 4 has highlight_re => (
22 is => 'rw',
23 );
24 4 has before_match => (
25 is => 'rw',
26 isa => 'Str',
27 default => BOLD . RED,
28 );
29 4 has after_match => (
30 is => 'rw',
31 isa => 'Str',
32 default => RESET,
33 );
34 4 has before_nomatch => (
35 is => 'rw',
36 isa => 'Str',
37 default => CYAN,
38 );
39 4 has after_nomatch => (
40 is => 'rw',
41 isa => 'Str',
42 default => RESET,
43 );
44 4 has before_snip => (
45 is => 'rw',
46 isa => 'Str',
47 default => RESET . RED . ON_BLACK,
48 );
49 4 has after_snip => (
50 is => 'rw',
51 isa => 'Str',
52 default => RESET,
53 );
54 has limit => (
55 is => 'rw',
56 isa => 'Int',
57 default => sub {
58 8 ▶ my ($cols, $rows) = Term::Size::Any::chars;
Subroutine __ANON__: called CC=2 SCAR=7.2
59 8 ▶ return $cols || 212;
Condition50%1/2
$cols || 212
l!l
08
MC/DC0%0/1
$cols || 212
$cols
Truth table50%1/2
A
A= $cols
Aresult
11
00
60 }
61 4 );
62 4 has snip => (
63 is => 'rw',
64 isa => 'Bool',
65 default => 1,
66 );
67  
68 sub make_highlight_re {
69 19 ▶ my ($self) = @_;
Subroutine make_highlight_re: called CC=2 SCAR=6.9
Pod documented
70  
71 19 ▶ return $self->highlight_re if $self->highlight_re;
Branch: 1 branch
BranchTrueFalseTotal
if $self->highlight_re 6 13 19
72  
73 13 my $re = $self->make_regex;
74  
75 # make sure that all brackets are for non capture groups
76 13 $re =~ s/ (?<! \\ | \[ ) [(] (?! [?] ) /(?:/gxms;
77  
78 13 return $self->highlight_re($re);
79 }
80  
81 sub highlight {
82 9 ▶ my ($self, $string) = @_;
Subroutine highlight: called CC=19 SCAR=29.5
Pod documented
83 9 my $re = $self->make_highlight_re;
84 9 my $out = '';
85  
86 9 my @parts = split /($re)/, $string;
87  
88 9 my $match_length = 0;
89 9 for my $i ( 0 .. @parts - 1 ) {
90 35 ▶ if ( $i % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i % 2) 13 22 35
91 13 $match_length += length $parts[$i];
92 }
93 }
94  
95 # 5 is the magic number of characters used to show the line number
96 9 my $limit = $self->limit - $match_length - 5;
97 9 my $joins = @parts - ( @parts - 1 ) / 2;
98 9 my $chars = ( $limit / $joins ) / 2 - 2;
99 9 my $chars_front = int $chars;
100 9 my $chars_back = int $chars;
101 9 my $total = $joins * ( $chars_front + $chars_back + 3 ) + 1;
102 9 ▶ if (length $parts[-1] < $chars * 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $parts[-1] < $chars * 2) 5 4 9
103 5 $total -= $chars_front + $chars_back - length $parts[-1];
104 }
105  
106 9 ▶ my $inc = $limit - $total > $joins * 2 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$limit - $total > $joins * 2 ? : 5 4 9
107 9 $chars += $inc;
108 9 $chars_front = int $chars;
109 9 $chars_back = int $chars;
110 9 $total = $joins * ( $chars_front + $chars_back + 3 ) + 1;
111 9 ▶ if (length $parts[-1] < $chars * 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $parts[-1] < $chars * 2) 5 4 9
112 5 $total -= $chars_front + $chars_back - length $parts[-1];
113 }
114 #warn "match = $match_length\nchars = $chars\nlimit = $limit ($total)\nparts = " . (scalar @parts) . "\njoins = $joins\n";
115  
116 9 for my $i ( 0 .. @parts - 1 ) {
117 35 ▶ if ( $i % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i % 2) { } 13 22 35
118 13 $out .= $self->before_match . $parts[$i] . $self->after_match;
119 }
120 else {
121 22 my $part = $parts[$i];
122 22 ▶ if ($self->snip && length $string > $self->limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->snip and length $string > $self->limit) 7 15 22
Condition100%3/3
$self->snip and length $string > $self->limit
!ll&&!rl&&r
2137
MC/DC100%2/2
$self->snip and length $string > $self->limit
$self->snip length $string > $self->limit
Truth table100%3/3
A and B
A= $self->snipB= length $string > $self->limit
ABresult
0X0
100
111
123 7 my $chars_front_tmp = $chars_front;
124 7 my $chars_back_tmp = $chars_back;
125 7 ▶ if ($total < $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total < $limit) 1 6 7
126 1 $chars_front_tmp++;
127 1 $total++;
128 }
129 7 ▶ if ($total < $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total < $limit) 1 6 7
130 1 $chars_back_tmp++;
131 1 $total++;
132 }
133  
134 # Check if
135 7 ▶ if ($chars_front_tmp + $chars_back_tmp < length $parts[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chars_front_tmp + $chars_back_tmp < length $parts[$i]) 7 0 7
136 7 ▶ my ($front) = $chars_front_tmp > 0 ? $parts[$i] =~ /\A(.{$chars_front_tmp})/xms : ('');
Branch: 1 branch
BranchTrueFalseTotal
$chars_front_tmp > 0 ? : 0 7 7
137 7 ▶ my ($back) = $chars_back_tmp > 0 ? $parts[$i] =~ /(.{$chars_back_tmp})\Z/xms : ('');
Branch: 1 branch
BranchTrueFalseTotal
$chars_back_tmp > 0 ? : 0 7 7
138 7 ▶ $part = (defined $front ? $front : '')
Branch: 2 branches
BranchTrueFalseTotal
defined $front ? : 7 0 7
defined $back ? : 7 0 7
139 . $self->before_snip . '...'
140 . $self->after_snip
141 . $self->before_nomatch
142 . (defined $back ? $back : '');
143 }
144 }
145 22 $out .= $self->before_nomatch . $part . $self->after_nomatch;
146 }
147 }
148  
149 9 $out .= RESET;
150 9 ▶ $out .= "\\N" if $string !~ /\n/xms;
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /\n/msx 8 1 9
151 9 ▶ $out .= "\n" if $out !~ /\n/xms;
Branch: 1 branch
BranchTrueFalseTotal
unless $out =~ /\n/msx 8 1 9
152  
153 9 return $out;
154 }
155  
156 4 1;
157  
158 __END__
159  
160 =head1 NAME
161  
162 File::CodeSearch::Highlighter - Highlights matched parts of a line.
163  
164 =head1 VERSION
165  
166 This documentation refers to File::CodeSearch::Highlighter version 0.7.5.
167  
168  
169 =head1 SYNOPSIS
170  
171 use File::CodeSearch::Highlighter;
172  
173 # Brief but working code example(s) here showing the most common usage(s)
174 # This section will be as far as many users bother reading, so make it as
175 # educational and exemplary as possible.
176  
177  
178 =head1 DESCRIPTION
179  
180 =head1 ATTRIBUTES
181  
182 =over 4
183  
184 =item C<highlight_re>
185  
186 The regular expression used to find what to highlight
187  
188 =item C<before_match (Str, BOLD RED)>
189  
190 A string put before a match
191  
192 =item C<after_match (Str RESET)>
193  
194 A string put after a match
195  
196 =item C<before_nomatch (Str, CYAN)>
197  
198 A string for before text that doesn't match
199  
200 =item C<after_nomatch (Str, RESET)>
201  
202 A string for after text that doesn't match
203  
204 =item C<before_snip (Str, RESET . RED . ON_BLACK)>
205  
206 A string for before snipped out text.
207  
208 =item C<after_snip (Str, RESET)>
209  
210 A string for after snipped out text.
211  
212 =item C<limit (Int, columns in terminal)>
213  
214 The size of the limit for line length of text that is extremely long.
215  
216 =item C<snip (Bool, 1)>
217  
218 Cut out non-matching text so that one line of text matches on line of output
219  
220 =back
221  
222 =head1 SUBROUTINES/METHODS
223  
224 =head3 C<highlight ( $search, )>
225  
226 Param: C<$search> - type (detail) - description
227  
228 Return: File::CodeSearch::Highlighter -
229  
230 Description:
231  
232 =head3 C<make_highlight_re ( $search, )>
233  
234 =head1 DIAGNOSTICS
235  
236 =head1 CONFIGURATION AND ENVIRONMENT
237  
238 =head1 DEPENDENCIES
239  
240 =head1 INCOMPATIBILITIES
241  
242 =head1 BUGS AND LIMITATIONS
243  
244 There are no known bugs in this module.
245  
246 Please report problems to Ivan Wills (ivan.wills@gmail.com).
247  
248 Patches are welcome.
249  
250 =head1 AUTHOR
251  
252 Ivan Wills - (ivan.wills@gmail.com)
253  
254 =head1 LICENSE AND COPYRIGHT
255  
256 Copyright (c) 2009 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
257 All rights reserved.
258  
259 This module is free software; you can redistribute it and/or modify it under
260 the same terms as Perl itself. See L<perlartistic>. This program is
261 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
262 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
263 PARTICULAR PURPOSE.
264  
265 =cut
« blib/lib/File/CodeSearch.pm ↑ Summary blib/lib/File/CodeSearch/RegexBuilder.pm »