blib/lib/Tail/Tool/Plugin/Highlight.pm

filter: Statementstmt 84.2% 48 / 57 Branchbran 50.0% 7 / 14 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 81.8% 9 / 11 Podpod 100% 1 / 1 totaltotal 72.3% 68 / 94 CC 14 SCAR 19.2
CC
14
Cov
74%
_set_regex
12.0
process
10.7
_set_colourer
3.6
CRAP 6.8
« blib/lib/Tail/Tool/Plugin/GroupLines.pm ↑ Summary blib/lib/Tail/Tool/RegexList.pm »
1 package Tail::Tool::Plugin::Highlight;
2  
3 # Created on: 2010-10-06 14:16:20
4 # Create by: Ivan Wills
5 # $Id$
6 # $Revision$, $HeadURL$, $Date$
7 # $Revision$, $Source$, $Date$
8  
9 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 extends 'Tail::Tool::PostProcess';
18 2 with 'Tail::Tool::RegexList';
19  
20 2 our $VERSION = version->new('0.4.5');
21  
22 2 Readonly my @COLOURS => qw/
23 red
24 green
25 yellow
26 blue
27 magenta
28 cyan
29 on_red
30 on_green
31 on_yellow
32 on_blue
33 on_magenta
34 on_cyan
35 bold
36 /;
37  
38 has colourer => (
39 is => 'rw',
40 isa => 'CodeRef',
41 0 ▶ default => sub { \&colored },
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 2 trigger => \&_set_colourer,
43 );
44  
45 sub process {
46 1 ▶ my ($self, $line) = @_;
Subroutine process: called CC=10 SCAR=23.7
Pod documented
47 1 my $matches;
48 1 my @colours = @COLOURS;
49  
50 1 ▶ $self->colourer( \&colored ) if !$self->colourer;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colourer 0 1 1
51 1 $self->_set_colourer($self->colourer);
52  
53 1 for my $match ( @{ $self->regex } ) {
54 1 ▶ next if !$match->enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $match->enabled 0 1 1
55  
56 1 ▶ $match->colour( [ shift @colours || 'red' ] ) if !$match->colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $match->colour 1 0 1
Condition50%1/2
shift @colours || 'red'
l!l
10
MC/DC0%0/1
shift @colours || 'red'
shift @colours
Truth table50%1/2
A
A= shift @colours
Aresult
11
00
57  
58 # count the number of internal matches
59 1 ▶ my $count = $match->regex =~ /( [(] (?! [?] ) )/gxms || 0;
Condition50%1/2
$match->regex =~ /( [(] (?! [?] ) )/gmsx || 0
l!l
01
MC/DC0%0/1
$match->regex =~ /( [(] (?! [?] ) )/gmsx || 0
$match->regex =~ /( [(] (?! [?] ) )/gmsx
Truth table50%1/2
A
A= $match->regex =~ /( [(] (?! [?] ) )/gmsx
Aresult
11
00
60 1 my @parts = split /($match->{regex})/, $line;
61 1 $line = '';
62  
63 1 for my $i ( 0 .. @parts -1 ) {
64 3 ▶ if ( $i % ($count + 2) == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i % ($count + 2) == 0) { } 2 1 3
elsif ($i % ($count + 2) == 1) { } 1 0 1
65 # non matching text
66 2 $line .= $parts[$i];
67 }
68 elsif ( $i % ($count + 2) == 1 ) {
69 1 $line .= $self->colourer->( $match->colour, $parts[$i] );
70 }
71 }
72 }
73  
74 # return empty array if there were enabled matches else return the line
75 1 return ($line);
76 }
77  
78 sub _set_regex {
79 0 ▶ my ( $self, $regexs, $old_regexs ) = @_;
Subroutine _set_regex: not called CC=3 SCAR=24.8
80  
81 0 my $i = 0;
82 0 for my $regex ( @{ $regexs } ) {
83 0 ▶ $regex->colour( [ $COLOURS[$i % @COLOURS] ] ) if !$regex->has_colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $regex->has_colour 0 0 0
84 0 $i++;
85 }
86  
87 0 return;
88 }
89  
90 sub _set_colourer {
91 2 ▶ my ( $self, $new, $old ) = @_;
Subroutine _set_colourer: called CC=3 SCAR=12.7
92  
93 2 my $test = $new->( ['red'], 'thing' );
94  
95 2 ▶ if ( !$test || $test eq 'DUMMY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $test or $test eq "DUMMY") 0 2 2
Condition33%1/3
not $test or $test eq "DUMMY"
l!l&&r!l&&!r
002
MC/DC0%0/2
not $test or $test eq "DUMMY"
not $test $test eq "DUMMY"
Truth table33%1/3
not(A) or B
A= not $testB= $test eq "DUMMY"
ABresult
1X1
011
000
96 0 $self->colourer( \&colored );
97 }
98  
99 2 return;
100 }
101  
102 2 1;
103  
104 __END__
105  
106 =head1 NAME
107  
108 Tail::Tool::Plugin::Highlight - Highlights any text that matches the supplied regular expressions.
109  
110 =head1 VERSION
111  
112 This documentation refers to Tail::Tool::Plugin::Highlight version 0.4.5.
113  
114  
115 =head1 SYNOPSIS
116  
117 use Tail::Tool::Plugin::Highlight;
118  
119 # Brief but working code example(s) here showing the most common usage(s)
120 # This section will be as far as many users bother reading, so make it as
121 # educational and exemplary as possible.
122  
123 =head1 DESCRIPTION
124  
125 =head1 SUBROUTINES/METHODS
126  
127 =head2 C<new (%params)>
128  
129 Param: regex - ArrayRef - List of regular expressions that lines must match
130  
131 Param: colourer - CodeRef - A sub that takes an array ref of colour
132 specifications as the first argument and the text to be coloured as the second
133 argument. The default colourer is the colored function from L<Term::ANSIColor>
134  
135 =head2 C<process ($line)>
136  
137 Description: Checks if the line matches any of the regular expressions supplied
138 then colours the matched parts and returns the changed line.
139  
140 =head1 DIAGNOSTICS
141  
142 A list of every error and warning message that the module can generate (even
143 the ones that will "never happen"), with a full explanation of each problem,
144 one or more likely causes, and any suggested remedies.
145  
146 =head1 CONFIGURATION AND ENVIRONMENT
147  
148 A full explanation of any configuration system(s) used by the module, including
149 the names and locations of any configuration files, and the meaning of any
150 environment variables or properties that can be set. These descriptions must
151 also include details of any configuration language used.
152  
153 =head1 DEPENDENCIES
154  
155 A list of all of the other modules that this module relies upon, including any
156 restrictions on versions, and an indication of whether these required modules
157 are part of the standard Perl distribution, part of the module's distribution,
158 or must be installed separately.
159  
160 =head1 INCOMPATIBILITIES
161  
162 A list of any modules that this module cannot be used in conjunction with.
163 This may be due to name conflicts in the interface, or competition for system
164 or program resources, or due to internal limitations of Perl (for example, many
165 modules that use source code filters are mutually incompatible).
166  
167 =head1 BUGS AND LIMITATIONS
168  
169 A list of known problems with the module, together with some indication of
170 whether they are likely to be fixed in an upcoming release.
171  
172 Also, a list of restrictions on the features the module does provide: data types
173 that cannot be handled, performance issues and the circumstances in which they
174 may arise, practical limitations on the size of data sets, special cases that
175 are not (yet) handled, etc.
176  
177 The initial template usually just has:
178  
179 There are no known bugs in this module.
180  
181 Please report problems to Ivan Wills (ivan.wills@gamil.com).
182  
183 Patches are welcome.
184  
185 =head1 AUTHOR
186  
187 Ivan Wills - (ivan.wills@gamil.com)
188 <Author name(s)> (<contact address>)
189  
190 =head1 LICENSE AND COPYRIGHT
191  
192 Copyright (c) 2010 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW, Australia, 2077).
193 All rights reserved.
194  
195 This module is free software; you can redistribute it and/or modify it under
196 the same terms as Perl itself. See L<perlartistic>. This program is
197 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
198 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
199 PARTICULAR PURPOSE.
200  
201 =cut
« blib/lib/Tail/Tool/Plugin/GroupLines.pm ↑ Summary blib/lib/Tail/Tool/RegexList.pm »