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