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