-
-
Save tai/acf4fc94bcfc3ced4526acd89d67eec9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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/perl | |
| sub usage { | |
| my $p = ($0 =~ m|[^/]+$|, $&); | |
| print STDERR <<EOF; | |
| $p - Add linecolor to input | |
| Usage: $p <perlexpr> [color] [<perlexpr> <color> ...] | |
| Example: | |
| \$ lsusb -tv | $p '!/hub/io' | |
| \$ lsusb -tv | $p '!/hub/io' green | |
| \$ lsusb -tv | $p '!/hub/io' 'bold ansi2' | |
| \$ lsusb -tv | $p '!/hub/io' 'red on_rgb150' | |
| \$ lsusb -tv | $p '!/hub/io' green '/root/io' red | |
| NOTE: | |
| - Any Term::ANSIColor supported color or color syntax can be used. | |
| EOF | |
| exit 1; | |
| } | |
| use strict; | |
| use warnings; | |
| use Term::ANSIColor; | |
| use Tie::Hash::Indexed; | |
| usage unless @ARGV; | |
| my $rule = Tie::Hash::Indexed->new; | |
| while (@ARGV) { | |
| my $exp = shift; | |
| my $col = shift || 'green'; | |
| $rule->push($exp, $col); | |
| } | |
| while (<>) { | |
| my $line = $_; | |
| my $it = $rule->iterator; | |
| while (my($exp, $col) = $it->next) { | |
| next unless eval($exp); | |
| $_ = colored($line, $col); | |
| } | |
| print; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment