Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 149 lines (116 sloc) 3.688 kB
d65db20 @tadzik Initial commit
authored
1 use v6;
2
d7375c1 @stmuk module to unit module
stmuk authored
3 unit module Term::ANSIColor;
d65db20 @tadzik Initial commit
authored
4
5 # these will be macros one day, yet macros can't be exported so far
6 sub RESET is export { "\e[0m" }
7 sub BOLD is export { "\e[1m" }
8 sub UNDERLINE is export { "\e[4m" }
9 sub INVERSE is export { "\e[7m" }
10 sub BOLD_OFF is export { "\e[22m" }
11 sub UNDERLINE_OFF is export { "\e[24m" }
12 sub INVERSE_OFF is export { "\e[27m" }
13
14 my %attrs =
ea16708 @tadzik Cleanups and fixes
authored
15 reset => "0",
16 bold => "1",
17 underline => "4",
18 inverse => "7",
19 black => "30",
20 red => "31",
21 green => "32",
22 yellow => "33",
23 blue => "34",
24 magenta => "35",
25 cyan => "36",
26 white => "37",
27 default => "39",
28 on_black => "40",
29 on_red => "41",
30 on_green => "42",
31 on_yellow => "43",
32 on_blue => "44",
33 on_magenta => "45",
34 on_cyan => "46",
35 on_white => "47",
36 on_default => "49";
d65db20 @tadzik Initial commit
authored
37
38 sub color (Str $what) is export {
828a5dc @tadzik Added uncolor, colorvalid and colorstrip note
authored
39 my @res;
d65db20 @tadzik Initial commit
authored
40 my @a = $what.split(' ');
41 for @a -> $attr {
d0af2ab @jnthn Stop using deprecated .exists method.
jnthn authored
42 if %attrs{$attr}:exists {
47afa31 @tadzik Implemented colorstrip()
authored
43 @res.push: %attrs{$attr}
ea16708 @tadzik Cleanups and fixes
authored
44 } else {
a2e5663 @tadzik Added docs, changed error messages to act as Perl5's Term::ANSIColor
authored
45 die("Invalid attribute name '$attr'")
ea16708 @tadzik Cleanups and fixes
authored
46 }
d65db20 @tadzik Initial commit
authored
47 }
ea16708 @tadzik Cleanups and fixes
authored
48 return "\e[" ~ @res.join(';') ~ "m";
d65db20 @tadzik Initial commit
authored
49 }
50
51 sub colored (Str $what, Str $how) is export {
52 color($how) ~ $what ~ color('reset');
53 }
828a5dc @tadzik Added uncolor, colorvalid and colorstrip note
authored
54
55 sub colorvalid (*@a) is export {
56 for @a -> $el {
d0af2ab @jnthn Stop using deprecated .exists method.
jnthn authored
57 return False unless %attrs{$el}:exists
828a5dc @tadzik Added uncolor, colorvalid and colorstrip note
authored
58 }
59 return True;
60 }
61
47afa31 @tadzik Implemented colorstrip()
authored
62 sub colorstrip (*@a) is export {
63 my @res;
64 for @a -> $str {
65 @res.push: $str.subst(/\e\[ <[0..9;]>+ m/, '', :g);
66 }
67 return @res.join;
68 }
828a5dc @tadzik Added uncolor, colorvalid and colorstrip note
authored
69
70 sub uncolor (Str $what) is export {
71 my @res;
69a346f @tadzik Fixes in uncolor
authored
72 my @list = $what.comb(/\d+/);
828a5dc @tadzik Added uncolor, colorvalid and colorstrip note
authored
73 for @list -> $elem {
d0af2ab @jnthn Stop using deprecated .exists method.
jnthn authored
74 if %attrs.reverse{$elem}:exists {
47afa31 @tadzik Implemented colorstrip()
authored
75 @res.push: %attrs.reverse{$elem}
ea16708 @tadzik Cleanups and fixes
authored
76 } else {
a2e5663 @tadzik Added docs, changed error messages to act as Perl5's Term::ANSIColor
authored
77 die("Bad escape sequence: {'\e[' ~ $elem ~ 'm'}")
ea16708 @tadzik Cleanups and fixes
authored
78 }
828a5dc @tadzik Added uncolor, colorvalid and colorstrip note
authored
79 }
80 return @res.join(' ');
81 }
a2e5663 @tadzik Added docs, changed error messages to act as Perl5's Term::ANSIColor
authored
82
83 =begin pod
84
85 =head1 NAME
86
87 Term::ANSIColor - Color screen output using ANSI escape sequences
88
89 =head1 SYNOPSIS
90
91 use Term::ANSIColor;
92 say color('bold'), "this is in bold", color('reset');
06768cc @timo Update documentation for colored
timo authored
93 say colored('what a lovely colours!', 'underline red on_green');
a2e5663 @tadzik Added docs, changed error messages to act as Perl5's Term::ANSIColor
authored
94 say BOLD, 'good to be fat!', BOLD_OFF;
95 say 'ok' if colorvalid('magenta', 'on_black', 'inverse');
96 say '\e[36m is ', uncolor('\e36m');
97 say colorstrip("\e[1mThis is bold\e[0m");
98
99 =head1 DESCRIPTION
100
101 Term::ANSIColor provides an interface for using colored output
102 in terminals. The following functions are available:
103
104 =head2 C<color()>
105
106 Given a string with color names, the output produced by C<color()>
107 sets the terminal output so the text printed after it will be colored
108 as specified. The following color names are recognised:
109
110 reset bold underline inverse black red green yellow blue
111 magenta cyan white default on_black on_red on_green on_yellow
112 on_blue on_magenta on_cyan on_white on_default
113
114 The on_* family of colors correspond to the background colors.
115
116 =head2 C<colored()>
117
118 C<colored()> is similar to C<color()>. It takes two Str arguments,
06768cc @timo Update documentation for colored
timo authored
119 where the first is the string to be colored, and the second is the
120 colors to be used. The C<reset> sequence is automagically placed after
a2e5663 @tadzik Added docs, changed error messages to act as Perl5's Term::ANSIColor
authored
121 the string.
122
123 =head2 C<colorvalid()>
124
125 C<colorvalid()> gets an array of color specifications (like those
126 passed to C<color()>) and returns true if all of them are valid,
127 false otherwise.
128
129 =head2 C<colorstrip()>
130
131 C<colorstrip>, given a string, removes all the escape sequences
132 in it, leaving the plain text without effects.
133
134 =head2 C<uncolor()>
135
136 Given escape sequences, C<uncolor()> returns a string with readable
137 color names. E.g. passing "\e[36;44m" will result in "cyan on_blue".
138
139 =head1 Constants
140
141 C<Term::ANSIColor> provides constants which are just strings of
142 appropriate escape sequences. The following constants are available:
143
144 RESET BOLD UNDERLINE INVERSE BOLD_OFF UNDERLINE_OFF INVERSE_OFF
145
146 =end pod
ebc20b9 @tadzik Added load test
authored
147
148 # vim: ft=perl6
Something went wrong with that request. Please try again.