1Term::ANSIColor(3pm) Perl Programmers Reference Guide Term::ANSIColor(3pm)
2
3
4
6 Term::ANSIColor - Color screen output using ANSI escape sequences
7
9 use Term::ANSIColor;
10 print color 'bold blue';
11 print "This text is bold blue.\n";
12 print color 'reset';
13 print "This text is normal.\n";
14 print colored ("Yellow on magenta.", 'yellow on_magenta'), "\n";
15 print "This text is normal.\n";
16 print colored ['yellow on_magenta'], 'Yellow on magenta.';
17 print "\n";
18
19 use Term::ANSIColor qw(uncolor);
20 print uncolor ('01;31'), "\n";
21
22 use Term::ANSIColor qw(colorstrip);
23 print colorstrip '\e[1mThis is bold\e[0m', "\n";
24
25 use Term::ANSIColor qw(colorvalid);
26 my $valid = colorvalid ('blue bold', 'on_magenta');
27 print "Color string is ", $valid ? "valid\n" : "invalid\n";
28
29 use Term::ANSIColor qw(:constants);
30 print BOLD, BLUE, "This text is in bold blue.\n", RESET;
31
32 use Term::ANSIColor qw(:constants);
33 {
34 local $Term::ANSIColor::AUTORESET = 1;
35 print BOLD BLUE "This text is in bold blue.\n";
36 print "This text is normal.\n";
37 }
38
39 use Term::ANSIColor qw(:pushpop);
40 print PUSHCOLOR RED ON_GREEN "This text is red on green.\n";
41 print PUSHCOLOR BLUE "This text is blue on green.\n";
42 print RESET BLUE "This text is just blue.\n";
43 print POPCOLOR "Back to red on green.\n";
44 print LOCALCOLOR GREEN ON_BLUE "This text is green on blue.\n";
45 print "This text is red on green.\n";
46 {
47 local $Term::ANSIColor::AUTOLOCAL = 1;
48 print ON_BLUE "This text is red on blue.\n";
49 print "This text is red on green.\n";
50 }
51 print POPCOLOR "Back to whatever we started as.\n";
52
54 This module has two interfaces, one through color() and colored() and
55 the other through constants. It also offers the utility functions
56 uncolor(), colorstrip(), and colorvalid(), which have to be explicitly
57 imported to be used (see "SYNOPSIS").
58
59 Function Interface
60 color() takes any number of strings as arguments and considers them to
61 be space-separated lists of attributes. It then forms and returns the
62 escape sequence to set those attributes. It doesn't print it out, just
63 returns it, so you'll have to print it yourself if you want to (this is
64 so that you can save it as a string, pass it to something else, send it
65 to a file handle, or do anything else with it that you might care to).
66 color() throws an exception if given an invalid attribute, so you can
67 also use it to check attribute names for validity (see "EXAMPLES").
68
69 uncolor() performs the opposite translation, turning escape sequences
70 into a list of strings.
71
72 colorstrip() removes all color escape sequences from the provided
73 strings, returning the modified strings separately in array context or
74 joined together in scalar context. Its arguments are not modified.
75
76 colorvalid() takes attribute strings the same as color() and returns
77 true if all attributes are known and false otherwise.
78
79 The recognized non-color attributes are clear, reset, bold, dark,
80 faint, underline, underscore, blink, reverse, and concealed. Clear and
81 reset (reset to default attributes), dark and faint (dim and
82 saturated), and underline and underscore are equivalent, so use
83 whichever is the most intuitive to you. The recognized foreground
84 color attributes are black, red, green, yellow, blue, magenta, cyan,
85 and white. The recognized background color attributes are on_black,
86 on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, and
87 on_white. Case is not significant.
88
89 Note that not all attributes are supported by all terminal types, and
90 some terminals may not support any of these sequences. Dark and faint,
91 blink, and concealed in particular are frequently not implemented.
92
93 Attributes, once set, last until they are unset (by sending the
94 attribute "clear" or "reset"). Be careful to do this, or otherwise
95 your attribute will last after your script is done running, and people
96 get very annoyed at having their prompt and typing changed to weird
97 colors.
98
99 As an aid to help with this, colored() takes a scalar as the first
100 argument and any number of attribute strings as the second argument and
101 returns the scalar wrapped in escape codes so that the attributes will
102 be set as requested before the string and reset to normal after the
103 string. Alternately, you can pass a reference to an array as the first
104 argument, and then the contents of that array will be taken as
105 attributes and color codes and the remainder of the arguments as text
106 to colorize.
107
108 Normally, colored() just puts attribute codes at the beginning and end
109 of the string, but if you set $Term::ANSIColor::EACHLINE to some
110 string, that string will be considered the line delimiter and the
111 attribute will be set at the beginning of each line of the passed
112 string and reset at the end of each line. This is often desirable if
113 the output contains newlines and you're using background colors, since
114 a background color that persists across a newline is often interpreted
115 by the terminal as providing the default background color for the next
116 line. Programs like pagers can also be confused by attributes that
117 span lines. Normally you'll want to set $Term::ANSIColor::EACHLINE to
118 "\n" to use this feature.
119
120 Constant Interface
121 Alternately, if you import ":constants", you can use the constants
122 CLEAR, RESET, BOLD, DARK, FAINT, UNDERLINE, UNDERSCORE, BLINK, REVERSE,
123 CONCEALED, BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE,
124 ON_BLACK, ON_RED, ON_GREEN, ON_YELLOW, ON_BLUE, ON_MAGENTA, ON_CYAN,
125 and ON_WHITE directly. These are the same as color('attribute') and
126 can be used if you prefer typing:
127
128 print BOLD BLUE ON_WHITE "Text", RESET, "\n";
129
130 to
131
132 print colored ("Text", 'bold blue on_white'), "\n";
133
134 (Note that the newline is kept separate to avoid confusing the terminal
135 as described above since a background color is being used.)
136
137 When using the constants, if you don't want to have to remember to add
138 the ", RESET" at the end of each print line, you can set
139 $Term::ANSIColor::AUTORESET to a true value. Then, the display mode
140 will automatically be reset if there is no comma after the constant.
141 In other words, with that variable set:
142
143 print BOLD BLUE "Text\n";
144
145 will reset the display mode afterward, whereas:
146
147 print BOLD, BLUE, "Text\n";
148
149 will not. If you are using background colors, you will probably want
150 to print the newline with a separate print statement to avoid confusing
151 the terminal.
152
153 The subroutine interface has the advantage over the constants interface
154 in that only two subroutines are exported into your namespace, versus
155 twenty-two in the constants interface. On the flip side, the constants
156 interface has the advantage of better compile time error checking,
157 since misspelled names of colors or attributes in calls to color() and
158 colored() won't be caught until runtime whereas misspelled names of
159 constants will be caught at compile time. So, pollute your namespace
160 with almost two dozen subroutines that you may not even use that often,
161 or risk a silly bug by mistyping an attribute. Your choice, TMTOWTDI
162 after all.
163
164 The Color Stack
165 As of Term::ANSIColor 2.0, you can import ":pushpop" and maintain a
166 stack of colors using PUSHCOLOR, POPCOLOR, and LOCALCOLOR. PUSHCOLOR
167 takes the attribute string that starts its argument and pushes it onto
168 a stack of attributes. POPCOLOR removes the top of the stack and
169 restores the previous attributes set by the argument of a prior
170 PUSHCOLOR. LOCALCOLOR surrounds its argument in a PUSHCOLOR and
171 POPCOLOR so that the color resets afterward.
172
173 When using PUSHCOLOR, POPCOLOR, and LOCALCOLOR, it's particularly
174 important to not put commas between the constants.
175
176 print PUSHCOLOR BLUE "Text\n";
177
178 will correctly push BLUE onto the top of the stack.
179
180 print PUSHCOLOR, BLUE, "Text\n"; # wrong!
181
182 will not, and a subsequent pop won't restore the correct attributes.
183 PUSHCOLOR pushes the attributes set by its argument, which is normally
184 a string of color constants. It can't ask the terminal what the
185 current attributes are.
186
188 Bad escape sequence %s
189 (F) You passed an invalid ANSI escape sequence to uncolor().
190
191 Bareword "%s" not allowed while "strict subs" in use
192 (F) You probably mistyped a constant color name such as:
193
194 $Foobar = FOOBAR . "This line should be blue\n";
195
196 or:
197
198 @Foobar = FOOBAR, "This line should be blue\n";
199
200 This will only show up under use strict (another good reason to run
201 under use strict).
202
203 Invalid attribute name %s
204 (F) You passed an invalid attribute name to either color() or
205 colored().
206
207 Name "%s" used only once: possible typo
208 (W) You probably mistyped a constant color name such as:
209
210 print FOOBAR "This text is color FOOBAR\n";
211
212 It's probably better to always use commas after constant names in
213 order to force the next error.
214
215 No comma allowed after filehandle
216 (F) You probably mistyped a constant color name such as:
217
218 print FOOBAR, "This text is color FOOBAR\n";
219
220 Generating this fatal compile error is one of the main advantages
221 of using the constants interface, since you'll immediately know if
222 you mistype a color name.
223
224 No name for escape sequence %s
225 (F) The ANSI escape sequence passed to uncolor() contains escapes
226 which aren't recognized and can't be translated to names.
227
229 ANSI_COLORS_DISABLED
230 If this environment variable is set, all of the functions defined
231 by this module (color(), colored(), and all of the constants not
232 previously used in the program) will not output any escape
233 sequences and instead will just return the empty string or pass
234 through the original text as appropriate. This is intended to
235 support easy use of scripts using this module on platforms that
236 don't support ANSI escape sequences.
237
238 For it to have its proper effect, this environment variable must be
239 set before any color constants are used in the program.
240
242 It would be nice if one could leave off the commas around the constants
243 entirely and just say:
244
245 print BOLD BLUE ON_WHITE "Text\n" RESET;
246
247 but the syntax of Perl doesn't allow this. You need a comma after the
248 string. (Of course, you may consider it a bug that commas between all
249 the constants aren't required, in which case you may feel free to
250 insert commas unless you're using $Term::ANSIColor::AUTORESET or
251 PUSHCOLOR/POPCOLOR.)
252
253 For easier debugging, you may prefer to always use the commas when not
254 setting $Term::ANSIColor::AUTORESET or PUSHCOLOR/POPCOLOR so that
255 you'll get a fatal compile error rather than a warning.
256
258 The codes generated by this module are standard terminal control codes,
259 complying with ECMA-048 and ISO 6429 (generally referred to as "ANSI
260 color" for the color codes). The non-color control codes (bold, dark,
261 italic, underline, and reverse) are part of the earlier ANSI X3.64
262 standard for control sequences for video terminals and peripherals.
263
264 Note that not all displays are ISO 6429-compliant, or even
265 X3.64-compliant (or are even attempting to be so). This module will
266 not work as expected on displays that do not honor these escape
267 sequences, such as cmd.exe, 4nt.exe, and command.com under either
268 Windows NT or Windows 2000. They may just be ignored, or they may
269 display as an ESC character followed by some apparent garbage.
270
271 Jean Delvare provided the following table of different common terminal
272 emulators and their support for the various attributes and others have
273 helped me flesh it out:
274
275 clear bold faint under blink reverse conceal
276 ------------------------------------------------------------------------
277 xterm yes yes no yes yes yes yes
278 linux yes yes yes bold yes yes no
279 rxvt yes yes no yes bold/black yes no
280 dtterm yes yes yes yes reverse yes yes
281 teraterm yes reverse no yes rev/red yes no
282 aixterm kinda normal no yes no yes yes
283 PuTTY yes color no yes no yes no
284 Windows yes no no no no yes no
285 Cygwin SSH yes yes no color color color yes
286 Mac Terminal yes yes no yes yes yes yes
287
288 Windows is Windows telnet, Cygwin SSH is the OpenSSH implementation
289 under Cygwin on Windows NT, and Mac Terminal is the Terminal
290 application in Mac OS X. Where the entry is other than yes or no, that
291 emulator displays the given attribute as something else instead. Note
292 that on an aixterm, clear doesn't reset colors; you have to explicitly
293 set the colors back to what you want. More entries in this table are
294 welcome.
295
296 Note that codes 3 (italic), 6 (rapid blink), and 9 (strike-through) are
297 specified in ANSI X3.64 and ECMA-048 but are not commonly supported by
298 most displays and emulators and therefore aren't supported by this
299 module at the present time. ECMA-048 also specifies a large number of
300 other attributes, including a sequence of attributes for font changes,
301 Fraktur characters, double-underlining, framing, circling, and
302 overlining. As none of these attributes are widely supported or
303 useful, they also aren't currently supported by this module.
304
306 ECMA-048 is available on-line (at least at the time of this writing) at
307 http://www.ecma-international.org/publications/standards/ECMA-048.HTM
308 <http://www.ecma-
309 international.org/publications/standards/ECMA-048.HTM>.
310
311 ISO 6429 is available from ISO for a charge; the author of this module
312 does not own a copy of it. Since the source material for ISO 6429 was
313 ECMA-048 and the latter is available for free, there seems little
314 reason to obtain the ISO standard.
315
316 The current version of this module is always available from its web
317 site at <http://www.eyrie.org/~eagle/software/ansicolor/>. It is also
318 part of the Perl core distribution as of 5.6.0.
319
321 Original idea (using constants) by Zenin, reimplemented using subs by
322 Russ Allbery <rra@stanford.edu>, and then combined with the original
323 idea by Russ with input from Zenin. Russ Allbery now maintains this
324 module.
325
327 Copyright 1996, 1997, 1998, 2000, 2001, 2002, 2005, 2006, 2008, 2009
328 Russ Allbery <rra@stanford.edu> and Zenin. This program is free
329 software; you may redistribute it and/or modify it under the same terms
330 as Perl itself.
331
332 PUSHCOLOR, POPCOLOR, and LOCALCOLOR were contributed by openmethods.com
333 voice solutions.
334
335
336
337perl v5.12.4 2011-06-07 Term::ANSIColor(3pm)