Skip to content

Instantly share code, notes, and snippets.

@tai
Created April 30, 2021 13:37
Show Gist options
  • Select an option

  • Save tai/acf4fc94bcfc3ced4526acd89d67eec9 to your computer and use it in GitHub Desktop.

Select an option

Save tai/acf4fc94bcfc3ced4526acd89d67eec9 to your computer and use it in GitHub Desktop.
#!/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