Skip to content

Instantly share code, notes, and snippets.

@kzys kzys/escfilter forked from kyanny/escfilter
Created Sep 9, 2010

Embed
What would you like to do?
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long qw(HelpMessage VersionMessage);
use Term::ANSIColor qw(colored);
our $VERSION = '0.01';
$VERSION = eval $VERSION;
my %color_map = (
'k' => 'black',
'r' => 'red',
'g' => 'green',
'y' => 'yellow',
'b' => 'blue',
'p' => 'purple',
'l' => 'cyan',
'w' => 'white',
);
my %map = (
(map {
my $key = $_;
my $value = $color_map{$key};
($key => $value, uc $key => "on_$value");
} keys %color_map),
# 'return',
'strong' => 'bold',
# 'perpendicular' => 2,
'underscore' => 'underline',
'blink' => 'blink',
'reverse' => 'reverse',
'invisible' => 'concealed',
# 'bell',
);
Getopt::Long::Configure('bundling', 'no_ignore_case', 'auto_help', 'auto_version');
GetOptions(
\my %opts,
(grep {
$_ ne 'underscore';
} (keys %map)),
'underscore|u', # `u' is enabled for short hand
# 'bell',
'h' => \&HelpMessage,
'v' => \&VersionMessage,
);
my $colors = join ' ', map { $map{$_} } keys %opts;
my $word = shift || '';
while (<>) {
if ($word) {
s/($word)/colored($1, $colors)/ge;
}
print;
}
__END__
=head1 NAME
escfilter - wrap `word' by escape sequence
=head1 SYNOPSIS
$ escfilter -ru [word] < /etc/passwd
options:
short options: (lower for word, upper for background)
-k/-K black
-r/-R red
-g/-G green
-y/-Y yellow
-b/-B blue
-p/-P purple
-l/-L light blue
-w/-W white
long options:
--strong
--underscore
--blink
--reverse
--invisible
-h, -?, --help print help message
-v, --version print version string
=head1 SEE ALSO
L<http://d.hatena.ne.jp/rx7/20100827/p1>
=head1 AUTHOR
Kensuke Kaneko
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.