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