blib/lib/Term/Highlight.pm untested

filter: Statementstmt 0.0% 0 / 187 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 24 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 310 CC 38 SCAR 47.2
CC
38
Cov
0%
LoadArgs
210.0
InsertTags
182.0
ByPositions
30.0
CRAP 112.0
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 GetPatterns Process );
10  
11 our $VERSION = "1.6";
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 GetPatterns
113 {
114 my $self = shift;
115 $self->{ Patterns };
116 }
117  
118  
119 sub FindPositionsOfTags
120 {
121 my ( $positions, $patterns, $string_ref ) = @_;
122 my $result = 0;
123 for ( my $i = 0; $i < @$patterns; ++$i )
124 {
125 if ( defined $$patterns[ $i ][ 0 ] )
126 {
127 while ( $$string_ref =~ /$$patterns[ $i ][ 0 ]/g )
128 {
129 my $length = $+[ 0 ] - $-[ 0 ];
130 next if $length == 0;
131 push @$positions, ( [ $-[ 0 ], $length, 1, $i, \$$patterns[ $i ] ],
132 [ $+[ 0 ], $length, 0, $i, \$$patterns[ $i ] ] );
133 ++$result;
134 }
135 }
136 else
137 {
138 my $length = length $$string_ref;
139 next if $length == 0;
140 push @$positions, ( [ 0, $length, 1, $i, \$$patterns[ $i ] ],
141 [ $length, $length, 0, $i, \$$patterns[ $i ] ] );
142 ++$result;
143 }
144 }
145 $result;
146 }
147  
148  
149 sub RearrangePositionsOfTags
150 {
151 my ( $positions, $patterns ) = @_;
152 #@counts is a stack of not-ended pattern starts
153 my @counts;
154 for my $position( sort ByPositions @$positions )
155 {
156 #PrintPosition( $position, "Pos1" );
157 if ( $$position[ 2 ] ) #current start position
158 {
159 push @counts, [ $$position[ 3 ], $$position[ 1 ] ];
160 }
161 else #current end position
162 {
163 #remove first matching pattern count number from @counts
164 my $found_count;
165 for ( my $i = 0; $i < @counts; ++$i )
166 {
167 if ( $counts[ $i ][ 0 ] == $$position[ 3 ] )
168 {
169 $found_count = $i;
170 last;
171 }
172 }
173 if ( defined $found_count )
174 {
175 splice( @counts, $found_count, 1 );
176 #end of found pattern are changed by start of a pattern
177 #which corresponds to the last element in the @counts
178 if ( @counts )
179 {
180 $$position[ 1 ] = $counts[ $#counts ][ 1 ];
181 $$position[ 2 ] = -1; #not 1 for correct work of ByPositions()
182 $$position[ 3 ] = $counts[ $#counts ][ 0 ];
183 $$position[ 4 ] = \$$patterns[ $counts[ $#counts ][ 0 ] ];
184 }
185 }
186 }
187 #PrintPosition( $position, "Pos2" );
188 }
189 }
190  
191  
192 sub InsertTags
193 {
194 my ( $positions, $string_ref, $tagtype ) = @_;
195 my $offset = 0;
196 my $colortag = "";
197 for my $position( sort ByPositions @$positions )
198 {
199 #PrintPosition( $position, "Pos3" );
200 if ( $$position[ 2 ] ) #position of the starting tag
201 {
202 SWITCH_TAGTYPE:
203 {
204 if ( $tagtype eq "term" )
205 {
206 use integer;
207 my $bold = ${ $$position[ 4 ] }->[ 2 ] || 22;
208 # BEWARE: documentation says that escape sequences 39; and 49;
209 # are not supported everywhere
210 my ( $fgcolor, $bgcolor )= ( "39", "49" );
211 #color id in [0..15]
212 if ( ${ $$position[ 4 ] }->[ 1 ] < 16 && ${ $$position[ 4 ] }->[ 3 ] < 16 )
213 {
214 $fgcolor = "3" . ${ $$position[ 4 ] }->[ 1 ] % 8 if defined
215 ${ $$position[ 4 ] }->[ 1 ];
216 $bgcolor = "4" . ${ $$position[ 4 ] }->[ 3 ] % 8 if defined
217 ${ $$position[ 4 ] }->[ 3 ];
218 $bold = ${ $$position[ 4 ] }->[ 1 ] / 8 if defined
219 ${ $$position[ 4 ] }->[ 1 ];
220 $bold ||= ${ $$position[ 4 ] }->[ 2 ] || 22;
221 }
222 #color id in [16..255]
223 else
224 {
225 $fgcolor = "38;5;" . ${ $$position[ 4 ] }->[ 1 ] if defined
226 ${ $$position[ 4 ] }->[ 1 ];
227 $bgcolor = "48;5;" . ${ $$position[ 4 ] }->[ 3 ] if defined
228 ${ $$position[ 4 ] }->[ 3 ];
229 }
230 $bold .= ";" if defined $bold;
231 $colortag = qq/\033[$bold$fgcolor;${bgcolor}m/;
232 last SWITCH_TAGTYPE;
233 }
234 if ( $tagtype eq "debug" || $tagtype eq "debug-term" )
235 {
236 $colortag = "{_$$position[ 3 ]_"; last SWITCH_TAGTYPE;
237 }
238 }
239 }
240 else #position of the closing tag
241 {
242 SWITCH_TAGTYPE:
243 {
244 if ( $tagtype eq "term" ) { $colortag = qq/\033[0m/; last SWITCH_TAGTYPE; }
245 if ( $tagtype eq "debug" || $tagtype eq "debug-term" )
246 {
247 $colortag = "_$$position[ 3 ]_}"; last SWITCH_TAGTYPE;
248 }
249 }
250 }
251 substr( $$string_ref, $$position[ 0 ] + $offset, 0 ) = $colortag;
252 $offset += length( $colortag );
253 }
254 }
255  
256  
257 sub Process
258 {
259 #string to process
260 my ( $self, $String_ref ) = @_;
261 #@Positions contains the start and end positions of all found patterns in the current string
262 #and other data relative to the patterns (length of found pattern, start/end_of_pattern flag,
263 #pattern count number and a reference to pattern data)
264 my @Positions;
265  
266 #populate @Positions
267 my $found_matches = FindPositionsOfTags( \@Positions, \@{ $self->{ Patterns } }, $String_ref )
268 or return 0;
269  
270 #do not change original string and return found positions if an array is expected
271 return @Positions if wantarray;
272  
273 #replace all but the last ending tags from @Positions by one-before-the-last starting tag
274 #(crucial for terminal color escape sequences algorithm)
275 RearrangePositionsOfTags( \@Positions, \@{ $self->{ Patterns } } );
276  
277 #insert tags into current line
278 InsertTags( \@Positions, $String_ref, $$self{ tagtype } );
279  
280 $found_matches;
281 }
282  
283  
284 =head1 NAME
285  
286 Term::Highlight - Perl module to highlight regexp patterns on terminals
287  
288 =head1 SYNOPSIS
289  
290 =over
291  
292 =item use Term::Highlight;
293  
294 =item $obj = Term::Highlight->new( tagtype => $TAGTYPE );
295  
296 =item $obj->LoadArgs( \@args );
297  
298 =item $obj->LoadPatterns( \@ptns );
299  
300 =item $obj->ClearPatterns( );
301  
302 =item $obj->GetPatterns( );
303  
304 =item $obj->Process( \$string );
305  
306 =back
307  
308 Currently C<term> and C<term-debug> tagtypes are supported.
309 If tagtype is C<term> then boundaries of found patterns will be enclosed in
310 ANSI terminal color escape sequence tags, if tagtype is C<term-debug> then they
311 will be marked by special symbolic sequences.
312  
313 =head1 DESCRIPTION
314  
315 Term::Highlight is a Perl module aimed to support highlighting of regexp
316 patterns on color terminals.
317 It supports 256 color terminals a well as older 8 color terminals.
318  
319 =head1 EXPORTS
320  
321 =over
322  
323 =item B<LoadPatterns>
324  
325 expects a reference to an array of references to arrays of type
326 [ $pattern, $fg, $bold, $bg ].
327 Loads patterns to be processed.
328  
329 =item B<ClearPatterns>
330  
331 clears the loaded patterns.
332  
333 =item B<GetPatterns>
334  
335 returns a reference to the loaded patterns.
336  
337 =item B<LoadArgs>
338  
339 expects an array of references to strings.
340 Loads patterns to be processed.
341 This is just a convenient version of C<LoadPatterns>.
342 Example of array to be loaded: [ "-46", "-25.1", "-i", "\bw.*?\b", "-100" ].
343  
344 =item B<Process>
345  
346 expects a reference to a string.
347 Makes substitution of color tags inside the string.
348 Returns count of found matches.
349  
350 =back
351  
352 =head1 SEE ALSO
353  
354 hl(1)
355  
356 =head1 AUTHOR
357  
358 A. Radkov, E<lt>alexey.radkov@gmail.comE<gt>
359  
360 =head1 COPYRIGHT AND LICENSE
361  
362 Copyright (C) 2008-2016 by A. Radkov.
363  
364 This library is free software; you can redistribute it and/or modify
365 it under the same terms as Perl itself, either Perl version 5.8.8 or,
366 at your option, any later version of Perl 5 you may have available.
367  
368 =cut
369  
370 1;