blib/lib/Term/Highlight.pm untested

filter: Statementstmt 0.0% 0 / 205 Branchbran 0.0% 0 / 98 Conditioncond 0.0% 0 / 22 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 349 CC 50 SCAR 49.2
CC
50
Cov
0%
LoadArgs
210.0
FindPositionsOfTags
210.0
InsertTags
182.0
CRAP 137.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.1.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 ( defined ( 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, $skip_nl_check, $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 unless ( $skip_nl_check )
143 {
144 #trailing newlines cause problems: put color tags before
145 #them
146 #BEWARE: in older Mac OS "\r" was used as the line
147 #terminator, which is not supported here
148 if ( substr( $$string_ref, $+[ 0 ] - 1, 1 ) eq "\n" )
149 {
150 --$length;
151 ++$offset;
152 next if $length == 0;
153 if ( substr( $$string_ref, $+[ 0 ] - 2, 1 ) eq "\r" )
154 {
155 --$length;
156 ++$offset;
157 }
158 }
159 next if $length == 0;
160 }
161 push @$positions, ( [ $-[ 0 ], $length, 1, $i,
162 \$$patterns[ $i ] ],
163 [ $+[ 0 ] - $offset, $length, 0, $i,
164 \$$patterns[ $i ] ] );
165 ++$result;
166 }
167 }
168 else
169 {
170 #always mark the line matched in this case, perhaps with no tags
171 ++$result;
172 my $length = length $$string_ref;
173 next if $length == 0;
174 unless ( $skip_nl_check )
175 {
176 if ( substr( $$string_ref, -1 ) eq "\n" )
177 {
178 --$length;
179 next if $length == 0;
180 --$length if substr( $$string_ref, -2, 1 ) eq "\r";
181 }
182 }
183 next if $length == 0;
184 push @$positions, ( [ 0, $length, 1, $i,
185 \$$patterns[ $i ] ],
186 [ $length, $length, 0, $i,
187 \$$patterns[ $i ] ] );
188 }
189 }
190 $result;
191 }
192  
193  
194 sub RearrangePositionsOfTags
195 {
196 my ( $positions, $patterns ) = @_;
197 #@counts is a stack of not-ended pattern starts
198 my @counts;
199 for my $position( sort ByPositions @$positions )
200 {
201 #PrintPosition( $position, "Pos1" );
202 if ( $$position[ 2 ] ) #current start position
203 {
204 push @counts, [ $$position[ 3 ], $$position[ 1 ] ];
205 }
206 else #current end position
207 {
208 #remove first matching pattern count number from @counts
209 my $found_count;
210 for ( my $i = 0; $i < @counts; ++$i )
211 {
212 if ( $counts[ $i ][ 0 ] == $$position[ 3 ] )
213 {
214 $found_count = $i;
215 last;
216 }
217 }
218 if ( defined $found_count )
219 {
220 splice( @counts, $found_count, 1 );
221 #end of found pattern is changed by start of a pattern
222 #which corresponds to the last element in the @counts
223 if ( @counts )
224 {
225 $$position[ 1 ] = $counts[ $#counts ][ 1 ];
226 $$position[ 2 ] = -1; #not 1 for correct work of
227 #ByPositions()
228 $$position[ 3 ] = $counts[ $#counts ][ 0 ];
229 $$position[ 4 ] = \$$patterns[ $counts[ $#counts ][ 0 ] ];
230 }
231 }
232 }
233 #PrintPosition( $position, "Pos2" );
234 }
235 }
236  
237  
238 sub InsertTags
239 {
240 my ( $positions, $string_ref, $tagtype ) = @_;
241 my $offset = 0;
242 my $colortag = "";
243 for my $position( sort ByPositions @$positions )
244 {
245 #PrintPosition( $position, "Pos3" );
246 if ( $$position[ 2 ] ) #position of the starting tag
247 {
248 SWITCH_TAGTYPE:
249 {
250 if ( $tagtype eq "term" )
251 {
252 use integer;
253 my $bold = ${ $$position[ 4 ] }->[ 2 ] || 22;
254 #BEWARE: documentation says that escape sequences 39;
255 #and 49; are not supported everywhere
256 my ( $fgcolor, $bgcolor )= ( "39", "49" );
257 #color id in [0..15]
258 if ( ${ $$position[ 4 ] }->[ 1 ] < 16 &&
259 ${ $$position[ 4 ] }->[ 3 ] < 16 )
260 {
261 $fgcolor = "3" . ${ $$position[ 4 ] }->[ 1 ] % 8
262 if defined ${ $$position[ 4 ] }->[ 1 ];
263 $bgcolor = "4" . ${ $$position[ 4 ] }->[ 3 ] % 8
264 if defined ${ $$position[ 4 ] }->[ 3 ];
265 $bold = ${ $$position[ 4 ] }->[ 1 ] / 8
266 if defined ${ $$position[ 4 ] }->[ 1 ];
267 $bold ||= ${ $$position[ 4 ] }->[ 2 ] || 22;
268 }
269 #color id in [16..255]
270 else
271 {
272 $fgcolor = "38;5;" . ${ $$position[ 4 ] }->[ 1 ]
273 if defined ${ $$position[ 4 ] }->[ 1 ];
274 $bgcolor = "48;5;" . ${ $$position[ 4 ] }->[ 3 ]
275 if defined ${ $$position[ 4 ] }->[ 3 ];
276 }
277 $bold .= ";" if defined $bold;
278 $colortag = qq/\033[$bold$fgcolor;${bgcolor}m/;
279 last SWITCH_TAGTYPE;
280 }
281 if ( $tagtype eq "debug" || $tagtype eq "debug-term" )
282 {
283 $colortag = "{_$$position[ 3 ]_"; last SWITCH_TAGTYPE;
284 }
285 }
286 }
287 else #position of the closing tag
288 {
289 SWITCH_TAGTYPE:
290 {
291 if ( $tagtype eq "term" )
292 {
293 $colortag = qq/\033[0m/; last SWITCH_TAGTYPE;
294 }
295 if ( $tagtype eq "debug" || $tagtype eq "debug-term" )
296 {
297 $colortag = "_$$position[ 3 ]_}"; last SWITCH_TAGTYPE;
298 }
299 }
300 }
301 substr( $$string_ref, $$position[ 0 ] + $offset, 0 ) = $colortag;
302 $offset += length( $colortag );
303 }
304 }
305  
306  
307 sub Process
308 {
309 #string to process
310 my ( $self, $String_ref ) = @_;
311 #@Positions contains the start and the end positions of all found patterns
312 #in the current string and other data related to the patterns (length of
313 #found pattern, start/end_of_pattern flag, pattern count number and a
314 #reference to pattern data)
315 my @Positions;
316  
317 #populate @Positions
318 my $found_matches = FindPositionsOfTags( \@Positions,
319 \@{ $self->{ Patterns } }, $$self{ skip_nl_check }, $String_ref );
320  
321 #do not change original string and return found positions if an array is
322 #expected
323 return @Positions if wantarray;
324  
325 return 0 unless $found_matches;
326  
327 #replace all but the last ending tags from @Positions by one-before-the-last
328 #starting tag (crucial for terminal color escape sequences algorithm)
329 RearrangePositionsOfTags( \@Positions, \@{ $self->{ Patterns } } )
330 if $found_matches > 1;
331  
332 #insert tags into the current line
333 InsertTags( \@Positions, $String_ref, $$self{ tagtype } );
334  
335 $found_matches;
336 }
337  
338  
339 =head1 NAME
340  
341 Term::Highlight - Perl module to highlight regexp patterns on terminals
342  
343 =head1 SYNOPSIS
344  
345 =over
346  
347 =item use Term::Highlight;
348  
349 =item $obj = Term::Highlight->new( tagtype => $TAGTYPE, skip_nl_check => 0 );
350  
351 =item $obj->LoadPatterns( \@ptns );
352  
353 =item $obj->LoadArgs( \@args );
354  
355 =item $obj->GetPatterns( );
356  
357 =item $obj->ClearPatterns( );
358  
359 =item $obj->Process( \$string );
360  
361 =back
362  
363 =head1 DESCRIPTION
364  
365 Term::Highlight is a Perl module aimed to support highlighting of regexp
366 patterns in color terminals.
367 It supports 256 color terminals a well as older 8 color terminals.
368  
369 =head1 OPTIONS
370  
371 =over
372  
373 =item B<tagtype>
374  
375 sets type of color tags.
376 Only C<term> and C<term-debug> tag types are currently supported.
377 If tagtype is C<term> then boundaries of found patterns will be enclosed in
378 ANSI terminal color escape sequence tags.
379 If tagtype is C<term-debug> then they will be marked by special symbolic
380 sequences: this is supposed to use for debug purposes.
381  
382 =item B<skip_nl_check>
383  
384 defines how to treat newlines in input lines.
385 Set this to non-zero value if input lines do not contain newlines: this will
386 gain a bit of speed.
387  
388 =back
389  
390 =head1 EXPORTS
391  
392 =over
393  
394 =item B<LoadPatterns>
395  
396 expects a reference to an array of references to arrays of type
397 [ $pattern, $fg, $bold, $bg ].
398 Loads patterns to be processed.
399  
400 =item B<LoadArgs>
401  
402 expects an array of references to strings.
403 Loads patterns to be processed.
404 This is just a convenient version of C<LoadPatterns>.
405 Example of array to be loaded: [ "-46", "-25.1", "-i", "\bw.*?\b", "-100" ].
406  
407 =item B<GetPatterns>
408  
409 returns a reference to the loaded patterns.
410  
411 =item B<ClearPatterns>
412  
413 clears the loaded patterns.
414  
415 =item B<Process>
416  
417 expects a reference to a string.
418 Makes substitution of color tags inside the string.
419 Returns count of found matches.
420  
421 =back
422  
423 =head1 SEE ALSO
424  
425 hl(1)
426  
427 =head1 AUTHOR
428  
429 A. Radkov, E<lt>alexey.radkov@gmail.comE<gt>
430  
431 =head1 COPYRIGHT AND LICENSE
432  
433 Copyright (C) 2008-2020 by A. Radkov.
434  
435 This is free software; you can redistribute it and/or modify it
436 under the same terms as the Perl 5 programming language system itself.
437  
438 =cut
439  
440 1;