1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#!/usr/bin/perl
use v5.14;
use warnings;
use String::Tagged;
my $CSI = "\e[";
while( my $line = <STDIN> ) {
my $str = String::Tagged->new( $line );
# Every capital letter red
pos $line = 0;
while( $line =~ m/[A-Z]/g ) {
$str->apply_tag( $-[0], 1, fg => 1 );
}
# Punctuation green
pos $line = 0;
while( $line =~ m/[[:punct:]]/g ) {
$str->apply_tag( $-[0], 1, fg => 2 );
}
# Numbers blue
pos $line = 0;
while( $line =~ m/\d+/g ) {
$str->apply_tag( $-[0], $+[0]-$-[0], fg => 4 );
}
# Underline whole words
pos $line = 0;
while( $line =~ m/\S+/g ) {
$str->apply_tag( $-[0], $+[0]-$-[0], u => 1 );
}
print STDERR $str->debug_sprintf;
my %pen;
$str->iter_substr_nooverlap( sub {
my ( $substr, %tags ) = @_;
my @SGR;
if( defined( my $fg = $tags{fg} ) ) {
push @SGR, $fg+30;
$pen{fg} = $fg;
}
elsif( exists $pen{fg} ) {
push @SGR, 39;
delete $pen{fg};
}
if( $tags{u} and !$pen{u} ) {
push @SGR, 4;
$pen{u} = 1;
}
elsif( !$tags{u} and $pen{u} ) {
push @SGR, 24;
delete $pen{u};
}
print "${CSI}".join(";", @SGR)."m" if @SGR;
print $substr;
} );
print "${CSI}m\n";
}
|