File Coverage

blib/lib/Tail/Tool/Plugin/Highlight.pm
Criterion Covered Total %
statement 42 50 84.0
branch 7 14 50.0
condition 3 7 42.8
subroutine 9 10 90.0
pod 1 1 100.0
total 62 82 75.6


line stmt bran cond sub pod time code
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     2   2312 use Moose;
  2         382654  
  2         13  
10 2     2   12268 use warnings;
  2         4  
  2         55  
11 2     2   558 use version;
  2         1681  
  2         13  
12 2     2   125 use Carp;
  2         5  
  2         150  
13 2     2   399 use English qw/ -no_match_vars /;
  2         1503  
  2         13  
14 2     2   1167 use Term::ANSIColor;
  2         6975  
  2         103  
15 2     2   1021 use Readonly;
  2         6510  
  2         880  
16              
17             extends 'Tail::Tool::PostProcess';
18             with 'Tail::Tool::RegexList';
19              
20             our $VERSION = version->new('0.4.8');
21              
22             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             default => sub { \&colored },
42             trigger => \&_set_colourer,
43             );
44              
45             sub process {
46 1     1 1 478 my ($self, $line) = @_;
47 1         3 my $matches;
48 1         6 my @colours = @COLOURS;
49              
50 1 50       87 $self->colourer( \&colored ) if !$self->colourer;
51 1         23 $self->_set_colourer($self->colourer);
52              
53 1         2 for my $match ( @{ $self->regex } ) {
  1         22  
54 1 50       22 next if !$match->enabled;
55              
56 1 50 50     22 $match->colour( [ shift @colours || 'red' ] ) if !$match->colour;
57              
58             # count the number of internal matches
59 1   50     24 my $count = $match->regex =~ /( [(] (?! [?] ) )/gxms || 0;
60 1         21 my @parts = split /($match->{regex})/, $line;
61 1         3 $line = '';
62              
63 1         5 for my $i ( 0 .. @parts -1 ) {
64 3 100       12 if ( $i % ($count + 2) == 0 ) {
    50          
65             # non matching text
66 2         7 $line .= $parts[$i];
67             }
68             elsif ( $i % ($count + 2) == 1 ) {
69 1         26 $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         22 return ($line);
76             }
77              
78             sub _set_regex {
79 0     0   0 my ( $self, $regexs, $old_regexs ) = @_;
80              
81 0         0 my $i = 0;
82 0         0 for my $regex ( @{ $regexs } ) {
  0         0  
83 0 0       0 $regex->colour( [ $COLOURS[$i % @COLOURS] ] ) if !$regex->has_colour;
84 0         0 $i++;
85             }
86              
87 0         0 return;
88             }
89              
90             sub _set_colourer {
91 2     2   96 my ( $self, $new, $old ) = @_;
92              
93 2         6 my $test = $new->( ['red'], 'thing' );
94              
95 2 50 33     19 if ( !$test || $test eq 'DUMMY' ) {
96 0         0 $self->colourer( \&colored );
97             }
98              
99 2         5 return;
100             }
101              
102             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.8.
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