blib/lib/Term/Highlight.pm untested

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