blib/lib/Term/Highlight.pm untested

filter: Statementstmt 0.0% 0 / 185 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 24 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 306 CC 38 SCAR 47.4
CC
38
Cov
0%
LoadArgs
210.0
InsertTags
182.0
ByPositions
30.0
CRAP 114.2
1 package Term::Highlight;
2  
3 require Exporter;
4  
5 use strict;
6  
7 our @ISA = qw( Exporter );
8  
9 our @EXPORT = qw( LoadArgs LoadPatterns ClearPatterns Process );
10  
11 our $VERSION = "1.5";
12  
13  
14 sub new
15 {
16 my $class = shift;
17 my $self = { @_ };
18 bless $self, ref $class || $class;
19 }
20  
21  
22 sub ByPositions
23 {
24 #comparison order:
25 #1. start position,
26 #2. start/end of the pattern flag (-1, 0 or 1)
27 #3. length of the found pattern
28 return $$a[ 0 ] <=> $$b[ 0 ] if $$a[ 0 ] != $$b[ 0 ];
29 return $$a[ 2 ] <=> $$b[ 2 ] if $$a[ 2 ] != $$b[ 2 ];
30 return - ( $$a[ 1 ] <=> $$b[ 1 ] ) if $$a[ 2 ] == 1;
31 return ( $$a[ 1 ] <=> $$b[ 1 ] ) if $$a[ 2 ] == 0 || $$a[ 2 ] == -1;
32 }
33  
34  
35 sub PrintPosition
36 {
37 my ( $position, $header ) = @_;
38 #start position, length, start(1) or end(0) of pattern,
39 #pattern number, pattern, fg color id,
40 #bold, bg color id
41 print "$header: $$position[ 0 ], $$position[ 1 ], $$position[ 2 ],
42 $$position[ 3 ], ${ $$position[ 4 ] }->[ 0 ], ${ $$position[ 4 ] }->[ 1 ],
43 ${ $$position[ 4 ] }->[ 2 ], ${ $$position[ 4 ] }->[ 3 ]\n";
44 }
45  
46  
47 sub LoadArgs
48 {
49 my ( $self, $args ) = @_;
50 my ( $ignorecase, $fgcolor, $bold, $bgcolor );
51 my $lastPatternOmitted = 0;
52 while ( my $arg = shift @$args )
53 {
54 SWITCH_ARGS:
55 {
56 if ( $arg =~ /^-(.+)/ )
57 {
58 SWITCH_OPTS:
59 {
60 if ( $1 eq "i" ) { $ignorecase = "i"; last SWITCH_OPTS }
61 if ( $1 eq "ni" ) { $ignorecase = ""; last SWITCH_OPTS }
62 if ( $1 eq "rfg" ) { $fgcolor = undef; last SWITCH_OPTS }
63 if ( $1 eq "rb" ) { $bold = undef; last SWITCH_OPTS }
64 if ( $1 eq "rbg" ) { $bgcolor = undef; last SWITCH_OPTS }
65 if ( $1 eq "r" ) { $bold = undef; $bgcolor = undef; last SWITCH_OPTS }
66 if ( $1 eq "ra" ) { $fgcolor = undef; $bold = undef; $bgcolor = undef;
67 last SWITCH_OPTS }
68 if ( $1 eq "b" ) { $bold = 1; last SWITCH_OPTS }
69 if ( ( my $orig = $1 ) =~ /^\d{1,3}(?=\.1$)/ )
70 { $bgcolor = substr $orig, $-[ 0 ], $+[ 0 ] - $-[ 0 ];
71 last SWITCH_OPTS }
72 if ( ( my $orig = $1 ) =~ /^\d{1,3}(?=\.0$|$)/ )
73 { $fgcolor = substr $orig, $-[ 0 ], $+[ 0 ] - $-[ 0 ];
74 last SWITCH_OPTS }
75 print __PACKAGE__, " : WARNING: unknown option '$arg'\n";
76 }
77 $lastPatternOmitted = 1;
78 last SWITCH_ARGS;
79 }
80 #populate patterns data
81 if ( $ignorecase )
82 {
83 push @{ $self->{ Patterns } }, [ qr/$arg/i, $fgcolor, $bold, $bgcolor ];
84 }
85 else
86 {
87 push @{ $self->{ Patterns } }, [ qr/$arg/, $fgcolor, $bold, $bgcolor ];
88 }
89 $lastPatternOmitted = 0;
90 }
91 }
92 #push undefined pattern for last options not followed by any pattern
93 #which will apply for the whole string
94 push @{ $self->{ Patterns } }, [ undef, $fgcolor, $bold ] if $lastPatternOmitted;
95 }
96  
97  
98 sub LoadPatterns
99 {
100 my ( $self, $patterns ) = @_;
101 push @{ $self->{ Patterns } }, @$patterns;
102 }
103  
104  
105 sub ClearPatterns
106 {
107 my $self = shift;
108 @{ $self->{ Patterns } } = ();
109 }
110  
111  
112 sub FindPositionsOfTags
113 {
114 my ( $positions, $patterns, $string_ref ) = @_;
115 my $result = 0;
116 for ( my $i = 0; $i < @$patterns; ++$i )
117 {
118 if ( defined $$patterns[ $i ][ 0 ] )
119 {
120 while ( $$string_ref =~ /$$patterns[ $i ][ 0 ]/g )
121 {
122 my $length = $+[ 0 ] - $-[ 0 ];
123 next if $length == 0;
124 push @$positions, ( [ $-[ 0 ], $length, 1, $i, \$$patterns[ $i ] ],
125 [ $+[ 0 ], $length, 0, $i, \$$patterns[ $i ] ] );
126 ++$result;
127 }
128 }
129 else
130 {
131 my $length = length $$string_ref;
132 next if $length == 0;
133 push @$positions, ( [ 0, $length, 1, $i, \$$patterns[ $i ] ],
134 [ $length, $length, 0, $i, \$$patterns[ $i ] ] );
135 ++$result;
136 }
137 }
138 $result;
139 }
140  
141  
142 sub RearrangePositionsOfTags
143 {
144 my ( $positions, $patterns ) = @_;
145 #@counts is a stack of not-ended pattern starts
146 my @counts;
147 for my $position( sort ByPositions @$positions )
148 {
149 #PrintPosition( $position, "Pos1" );
150 if ( $$position[ 2 ] ) #current start position
151 {
152 push @counts, [ $$position[ 3 ], $$position[ 1 ] ];
153 }
154 else #current end position
155 {
156 #remove first matching pattern count number from @counts
157 my $found_count;
158 for ( my $i = 0; $i < @counts; ++$i )
159 {
160 if ( $counts[ $i ][ 0 ] == $$position[ 3 ] )
161 {
162 $found_count = $i;
163 last;
164 }
165 }
166 if ( defined $found_count )
167 {
168 splice( @counts, $found_count, 1 );
169 #end of found pattern are changed by start of a pattern
170 #which corresponds to the last element in the @counts
171 if ( @counts )
172 {
173 $$position[ 1 ] = $counts[ $#counts ][ 1 ];
174 $$position[ 2 ] = -1; #not 1 for correct work of ByPositions()
175 $$position[ 3 ] = $counts[ $#counts ][ 0 ];
176 $$position[ 4 ] = \$$patterns[ $counts[ $#counts ][ 0 ] ];
177 }
178 }
179 }
180 #PrintPosition( $position, "Pos2" );
181 }
182 }
183  
184  
185 sub InsertTags
186 {
187 my ( $positions, $string_ref, $tagtype ) = @_;
188 my $offset = 0;
189 my $colortag = "";
190 for my $position( sort ByPositions @$positions )
191 {
192 #PrintPosition( $position, "Pos3" );
193 if ( $$position[ 2 ] ) #position of the starting tag
194 {
195 SWITCH_TAGTYPE:
196 {
197 if ( $tagtype eq "term" )
198 {
199 use integer;
200 my $bold = ${ $$position[ 4 ] }->[ 2 ] || 22;
201 # BEWARE: documentation says that escape sequences 39; and 49;
202 # are not supported everywhere
203 my ( $fgcolor, $bgcolor )= ( "39", "49" );
204 #color id in [0..15]
205 if ( ${ $$position[ 4 ] }->[ 1 ] < 16 && ${ $$position[ 4 ] }->[ 3 ] < 16 )
206 {
207 $fgcolor = "3" . ${ $$position[ 4 ] }->[ 1 ] % 8 if defined
208 ${ $$position[ 4 ] }->[ 1 ];
209 $bgcolor = "4" . ${ $$position[ 4 ] }->[ 3 ] % 8 if defined
210 ${ $$position[ 4 ] }->[ 3 ];
211 $bold = ${ $$position[ 4 ] }->[ 1 ] / 8 if defined
212 ${ $$position[ 4 ] }->[ 1 ];
213 $bold ||= ${ $$position[ 4 ] }->[ 2 ] || 22;
214 }
215 #color id in [16..255]
216 else
217 {
218 $fgcolor = "38;5;" . ${ $$position[ 4 ] }->[ 1 ] if defined
219 ${ $$position[ 4 ] }->[ 1 ];
220 $bgcolor = "48;5;" . ${ $$position[ 4 ] }->[ 3 ] if defined
221 ${ $$position[ 4 ] }->[ 3 ];
222 }
223 $bold .= ";" if defined $bold;
224 $colortag = qq/\033[$bold$fgcolor;${bgcolor}m/;
225 last SWITCH_TAGTYPE;
226 }
227 if ( $tagtype eq "debug" || $tagtype eq "debug-term" )
228 {
229 $colortag = "{_$$position[ 3 ]_"; last SWITCH_TAGTYPE;
230 }
231 }
232 }
233 else #position of the closing tag
234 {
235 SWITCH_TAGTYPE:
236 {
237 if ( $tagtype eq "term" ) { $colortag = qq/\033[0m/; last SWITCH_TAGTYPE; }
238 if ( $tagtype eq "debug" || $tagtype eq "debug-term" )
239 {
240 $colortag = "_$$position[ 3 ]_}"; last SWITCH_TAGTYPE;
241 }
242 }
243 }
244 substr( $$string_ref, $$position[ 0 ] + $offset, 0 ) = $colortag;
245 $offset += length( $colortag );
246 }
247 }
248  
249  
250 sub Process
251 {
252 #string to process
253 my ( $self, $String_ref ) = @_;
254 #@Positions contains the start and end positions of all found patterns in the current string
255 #and other data relative to the patterns (length of found pattern, start/end_of_pattern flag,
256 #pattern count number and a reference to pattern data)
257 my @Positions;
258  
259 #populate @Positions
260 my $found_matches = FindPositionsOfTags( \@Positions, \@{ $self->{ Patterns } }, $String_ref )
261 or return 0;
262  
263 #do not change original string and return found positions if an array is expected
264 return @Positions if wantarray;
265  
266 #replace all but the last ending tags from @Positions by one-before-the-last starting tag
267 #(crucial for terminal color escape sequences algorithm)
268 RearrangePositionsOfTags( \@Positions, \@{ $self->{ Patterns } } );
269  
270 #insert tags into current line
271 InsertTags( \@Positions, $String_ref, $$self{ tagtype } );
272  
273 $found_matches;
274 }
275  
276  
277 =head1 NAME
278  
279 Term::Highlight - Perl module to highlight regexp patterns on terminals
280  
281 =head1 SYNOPSIS
282  
283 =over
284  
285 =item use Term::Highlight;
286  
287 =item $obj = Term::Highlight->new( tagtype => $TAGTYPE );
288  
289 =item $obj->LoadArgs( \@args );
290  
291 =item $obj->LoadPatterns( \@ptns );
292  
293 =item $obj->ClearPatterns( );
294  
295 =item $obj->Process( \$string );
296  
297 =back
298  
299 Currently C<term> and C<term-debug> tagtypes are supported.
300 If tagtype is C<term> then boundaries of found patterns will be enclosed in
301 ANSI terminal color escape sequence tags, if tagtype is C<term-debug> then they
302 will be marked by special symbolic sequences.
303  
304 =head1 DESCRIPTION
305  
306 Term::Highlight is a Perl module aimed to support highlighting of regexp
307 patterns on color terminals.
308 It supports 256 color terminals a well as older 8 color terminals.
309  
310 =head1 EXPORTS
311  
312 =over
313  
314 =item B<LoadPatterns>
315  
316 expects a reference to an array of references to arrays of type
317 [ $pattern, $fg, $bold, $bg ].
318 Loads patterns to be processed.
319  
320 =item B<ClearPatterns>
321  
322 clears the loaded patterns.
323  
324 =item B<LoadArgs>
325  
326 expects an array of references to strings.
327 Loads patterns to be processed.
328 This is just a convenient version of C<LoadPatterns>.
329 Example of array to be loaded: [ "-46", "-25.1", "-i", "\bw.*?\b", "-100" ].
330  
331 =item B<Process>
332  
333 expects a reference to a string.
334 Makes substitution of color tags inside the string.
335 Returns count of found matches.
336  
337 =back
338  
339 =head1 SEE ALSO
340  
341 hl(1)
342  
343 =head1 AUTHOR
344  
345 A. Radkov, E<lt>alexey.radkov@gmail.comE<gt>
346  
347 =head1 COPYRIGHT AND LICENSE
348  
349 Copyright (C) 2008-2016 by A. Radkov.
350  
351 This library is free software; you can redistribute it and/or modify
352 it under the same terms as Perl itself, either Perl version 5.8.8 or,
353 at your option, any later version of Perl 5 you may have available.
354  
355 =cut
356  
357 1;