blib/lib/Term/Highlight.pm untested

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