Skip to content
Newer
Older
100644 82 lines (73 sloc) 1.67 KB
d65db20 @tadzik Initial commit
authored Jul 21, 2010
1 use v6;
2
3 module Term::ANSIColor;
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 Jul 23, 2010
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 Jul 21, 2010
37
38 sub color (Str $what) is export {
828a5dc @tadzik Added uncolor, colorvalid and colorstrip note
authored Jul 23, 2010
39 my @res;
d65db20 @tadzik Initial commit
authored Jul 21, 2010
40 my @a = $what.split(' ');
41 for @a -> $attr {
ea16708 @tadzik Cleanups and fixes
authored Jul 23, 2010
42 if %attrs.exists($attr) {
47afa31 @tadzik Implemented colorstrip()
authored Jul 26, 2010
43 @res.push: %attrs{$attr}
ea16708 @tadzik Cleanups and fixes
authored Jul 23, 2010
44 } else {
45 die("No such attribute: '$attr'")
46 }
d65db20 @tadzik Initial commit
authored Jul 21, 2010
47 }
ea16708 @tadzik Cleanups and fixes
authored Jul 23, 2010
48 return "\e[" ~ @res.join(';') ~ "m";
d65db20 @tadzik Initial commit
authored Jul 21, 2010
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 Jul 23, 2010
54
55 sub colorvalid (*@a) is export {
56 for @a -> $el {
ea16708 @tadzik Cleanups and fixes
authored Jul 23, 2010
57 return False unless %attrs.exists($el)
828a5dc @tadzik Added uncolor, colorvalid and colorstrip note
authored Jul 23, 2010
58 }
59 return True;
60 }
61
47afa31 @tadzik Implemented colorstrip()
authored Jul 26, 2010
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 Jul 23, 2010
69
70 sub uncolor (Str $what) is export {
71 my @res;
69a346f @tadzik Fixes in uncolor
authored Jul 26, 2010
72 my @list = $what.comb(/\d+/);
828a5dc @tadzik Added uncolor, colorvalid and colorstrip note
authored Jul 23, 2010
73 for @list -> $elem {
ea16708 @tadzik Cleanups and fixes
authored Jul 23, 2010
74 if %attrs.reverse.exists($elem) {
47afa31 @tadzik Implemented colorstrip()
authored Jul 26, 2010
75 @res.push: %attrs.reverse{$elem}
ea16708 @tadzik Cleanups and fixes
authored Jul 23, 2010
76 } else {
69a346f @tadzik Fixes in uncolor
authored Jul 26, 2010
77 die("No such sequence: {'\e[' ~ $elem ~ 'm'}")
ea16708 @tadzik Cleanups and fixes
authored Jul 23, 2010
78 }
828a5dc @tadzik Added uncolor, colorvalid and colorstrip note
authored Jul 23, 2010
79 }
80 return @res.join(' ');
81 }
Something went wrong with that request. Please try again.