Created
September 29, 2016 20:24
-
-
Save nkh/311e7241dc1c7c4f3343944c172c8aff to your computer and use it in GitHub Desktop.
put colors in pipes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict ; | |
use warnings ; | |
use Term::ANSIColor ; | |
if (grep {/^--help/} @ARGV) | |
{ | |
print <<'EOH' ; | |
Usage: $> command | color_pipe regex color [regex color ...] | command ... | |
color_pipe colors stdio using perl regex and ANSI colors | |
Example: | |
color_pipe NAME red DESCRIPTION bright_blue 'MAX(IMUM)*' green | |
EOH | |
exit 0 ; | |
} | |
do { print <> and exit(0) } unless @ARGV ; | |
my ($i, $letter, $regex, $code, @colors) = (0, 'A', '', '$colors[') ; | |
my $reset = color('reset') ; | |
while (@ARGV) | |
{ | |
$regex .= '|' if $i ; | |
$regex .= "(?'$letter'" . shift(@ARGV) . ')' ; | |
push @colors, color(shift @ARGV // 'reset') ; | |
$code .= "defined \$+{$letter} ? $i : " ; | |
$letter++ ; | |
$i++ ; | |
} | |
$code .= "'' ] " ; | |
my $compiler_regex = qr/$regex/ ; | |
while(<>) { s/($compiler_regex)/$code . '. $1 . $reset'/gee ; print ; } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment