Last active
December 15, 2015 04:39
-
-
Save skaji/5203081 to your computer and use it in GitHub Desktop.
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 Pod::Usage; | |
=head1 SEE ALSO | |
http://en.wikipedia.org/wiki/ANSI_escape_code | |
=head1 NOTE | |
CSI = Control Sequence Introducer | |
SGR = Select Graphic Rendition | |
=head1 SYNOPSIS | |
# NOTE: \033 = \e = \x1b = ESC | |
# NOTE: \033[m means all attributes off | |
# render No.132 color ( \033[38;5;132m ) | |
echo -e "\033[38;5;132m" HELLO "\033[m" | |
# render No.132 color ( \033[38;5;132m ) | |
# with bold ( \033[1m ) | |
echo -e "\033[38;5;132m" "\033[1m" HELLO "\033[m" | |
# render No.132 color ( \033[38;5;132m ) | |
# with background No.111 color ( \033[48;5;111m ) | |
echo -e "\033[38;5;132m" "\033[48;5;111m" HELLO "\033[m" | |
# render No.132 color ( \033[38;5;132m ) | |
# with background No.111 color ( \033[48;5;111m ) | |
# with bold ( \033[1m ) | |
# with underline ( \033[4m ) | |
echo -e "\033[38;5;132m" "\033[48;5;111m" "\033[1m" "\033[4m" HELLO "\033[m" | |
=cut | |
my $CSI = "\x1b\x5b"; # \x1b = \e = \033 = ESC, \x5b = [ | |
my $SGR = "m"; | |
my $SGR_256COLOR = sub { my $num = shift; $CSI . "38;5;$num" . $SGR }; | |
my $SGR_256COLOR_BG = sub { my $num = shift; $CSI . "48;5;$num" . $SGR }; | |
my $SGR_BOLD = $CSI . 1 . $SGR; # Bold or increased intensity | |
my $SGR_UNDER = $CSI . 4 . $SGR; | |
my $SGR_RESET = $CSI . $SGR; | |
# normal color | |
render( 1, 8, 8); | |
# increased intensity color | |
render( 9, 16, 8); | |
# blue | |
render( 17, 21, 5); | |
# red | |
render( 52, 57, 6); | |
render( 88, 93, 6); | |
render(124, 129, 6); | |
render(160, 165, 6); | |
render(196, 201, 6); | |
# green | |
render( 22, 51, 6); | |
render( 58, 87, 6); | |
render( 94, 123, 6); | |
render(130, 159, 6); | |
render(166, 195, 6); | |
render(202, 231, 6); | |
# white black | |
render(232, 255, 6); | |
print "\n"; | |
pod2usage({-output => \*STDOUT, -exitval => 0}); | |
sub render { | |
my ($s, $e, $step) = @_; | |
my $i; | |
for ($s..$e) { | |
print $SGR_256COLOR_BG->($_), sprintf(' %3d', $_), $SGR_RESET; | |
print "\n" if ++$i % $step == 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment