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.', "\n";
17 print colored ['red on_bright_yellow'], 'Red on bright yellow.', "\n";
18 print colored ['bright_red on_black'], 'Bright red on black.', "\n";
19 print "\n";
20
21 use Term::ANSIColor qw(uncolor);
22 print uncolor ('01;31'), "\n";
23
24 use Term::ANSIColor qw(colorstrip);
25 print colorstrip '\e[1mThis is bold\e[0m', "\n";
26
27 use Term::ANSIColor qw(colorvalid);
28 my $valid = colorvalid ('blue bold', 'on_magenta');
29 print "Color string is ", $valid ? "valid\n" : "invalid\n";
30
31 use Term::ANSIColor qw(:constants);
32 print BOLD, BLUE, "This text is in bold blue.\n", RESET;
33
34 use Term::ANSIColor qw(:constants);
35 {
36 local $Term::ANSIColor::AUTORESET = 1;
37 print BOLD BLUE "This text is in bold blue.\n";
38 print "This text is normal.\n";
39 }
40
41 use Term::ANSIColor qw(:pushpop);
42 print PUSHCOLOR RED ON_GREEN "This text is red on green.\n";
43 print PUSHCOLOR BRIGHT_BLUE "This text is bright blue on green.\n";
44 print RESET BRIGHT_BLUE "This text is just bright blue.\n";
45 print POPCOLOR "Back to red on green.\n";
46 print LOCALCOLOR GREEN ON_BLUE "This text is green on blue.\n";
47 print "This text is red on green.\n";
48 {
49 local $Term::ANSIColor::AUTOLOCAL = 1;
50 print ON_BLUE "This text is red on blue.\n";
51 print "This text is red on green.\n";
52 }
53 print POPCOLOR "Back to whatever we started as.\n";
54
56 This module has two interfaces, one through color() and colored() and
57 the other through constants. It also offers the utility functions
58 uncolor(), colorstrip(), and colorvalid(), which have to be explicitly
59 imported to be used (see "SYNOPSIS").
60
61 Supported Colors
62 Terminal emulators that support color divide into two types: ones that
63 support only eight colors, and ones that support sixteen. This module
64 provides both the ANSI escape codes for the "normal" colors, supported
65 by both types, as well as the additional colors supported by sixteen-
66 color emulators. These colors are referred to as ANSI colors 0 through
67 7 (normal) and 8 through 15.
68
69 Unfortunately, interpretation of colors 0 through 7 often depends on
70 whether the emulator supports eight colors or sixteen colors.
71 Emulators that only support eight colors (such as the Linux console)
72 will display colors 0 through 7 with normal brightness and ignore
73 colors 8 through 15, treating them the same as white. Emulators that
74 support 16 colors, such as gnome-terminal, normally display colors 0
75 through 7 as dim or darker versions and colors 8 through 15 as normal
76 brightness. On such emulators, the "normal" white (color 7) usually is
77 shown as pale grey, requiring bright white (15) to be used to get a
78 real white color. Bright black usually is a dark grey color, although
79 some terminals display it as pure black. Some sixteen-color terminal
80 emulators also treat normal yellow (color 3) as orange or brown, and
81 bright yellow (color 11) as yellow.
82
83 Following the normal convention of sixteen-color emulators, this module
84 provides a pair of attributes for each color. For every normal color
85 (0 through 7), the corresponding bright color (8 through 15) is
86 obtained by prepending the string "bright_" to the normal color name.
87 For example, "red" is color 1 and "bright_red" is color 9. The same
88 applies for background colors: "on_red" is the normal color and
89 "on_bright_red" is the bright color. Capitalize these strings for the
90 constant interface.
91
92 There is unfortunately no way to know whether the current emulator
93 supports sixteen colors or not, which makes the choice of colors
94 difficult. The most conservative choice is to use only the regular
95 colors, which are at least displayed on all emulators. However, they
96 will appear dark in sixteen-color terminal emulators, including most
97 common emulators in UNIX X environments. If you know the display is
98 one of those emulators, you may wish to use the bright variants
99 instead. Even better, offer the user a way to configure the colors for
100 a given application to fit their terminal emulator.
101
102 Support for colors 8 through 15 (the "bright_" variants) was added in
103 Term::ANSIColor 3.0.
104
105 Function Interface
106 The function interface uses attribute strings to describe the colors
107 and text attributes to assign to text. The recognized non-color
108 attributes are clear, reset, bold, dark, faint, underline, underscore,
109 blink, reverse, and concealed. Clear and reset (reset to default
110 attributes), dark and faint (dim and saturated), and underline and
111 underscore are equivalent, so use whichever is the most intuitive to
112 you.
113
114 Note that not all attributes are supported by all terminal types, and
115 some terminals may not support any of these sequences. Dark and faint,
116 blink, and concealed in particular are frequently not implemented.
117
118 The recognized normal foreground color attributes (colors 0 to 7) are:
119
120 black red green yellow blue magenta cyan white
121
122 The corresponding bright foreground color attributes (colors 8 to 15)
123 are:
124
125 bright_black bright_red bright_green bright_yellow
126 bright_blue bright_magenta bright_cyan bright_white
127
128 The recognized normal background color attributes (colors 0 to 7) are:
129
130 on_black on_red on_green on yellow
131 on_blue on_magenta on_cyan on_white
132
133 The recognized bright background color attributes (colors 8 to 15) are:
134
135 on_bright_black on_bright_red on_bright_green on_bright_yellow
136 on_bright_blue on_bright_magenta on_bright_cyan on_bright_white
137
138 For any of the above listed attributes, case is not significant.
139
140 Attributes, once set, last until they are unset (by printing the
141 attribute "clear" or "reset"). Be careful to do this, or otherwise
142 your attribute will last after your script is done running, and people
143 get very annoyed at having their prompt and typing changed to weird
144 colors.
145
146 color(ATTR[, ATTR ...])
147 color() takes any number of strings as arguments and considers them
148 to be space-separated lists of attributes. It then forms and
149 returns the escape sequence to set those attributes. It doesn't
150 print it out, just returns it, so you'll have to print it yourself
151 if you want to. This is so that you can save it as a string, pass
152 it to something else, send it to a file handle, or do anything else
153 with it that you might care to. color() throws an exception if
154 given an invalid attribute.
155
156 colored(STRING, ATTRIBUTES)
157 colored(ATTR-REF, STRING[, STRING...])
158 As an aid in resetting colors, colored() takes a scalar as the
159 first argument and any number of attribute strings as the second
160 argument and returns the scalar wrapped in escape codes so that the
161 attributes will be set as requested before the string and reset to
162 normal after the string. Alternately, you can pass a reference to
163 an array as the first argument, and then the contents of that array
164 will be taken as attributes and color codes and the remainder of
165 the arguments as text to colorize.
166
167 Normally, colored() just puts attribute codes at the beginning and
168 end of the string, but if you set $Term::ANSIColor::EACHLINE to
169 some string, that string will be considered the line delimiter and
170 the attribute will be set at the beginning of each line of the
171 passed string and reset at the end of each line. This is often
172 desirable if the output contains newlines and you're using
173 background colors, since a background color that persists across a
174 newline is often interpreted by the terminal as providing the
175 default background color for the next line. Programs like pagers
176 can also be confused by attributes that span lines. Normally
177 you'll want to set $Term::ANSIColor::EACHLINE to "\n" to use this
178 feature.
179
180 uncolor(ESCAPE)
181 uncolor() performs the opposite translation as color(), turning
182 escape sequences into a list of strings corresponding to the
183 attributes being set by those sequences.
184
185 colorstrip(STRING[, STRING ...])
186 colorstrip() removes all color escape sequences from the provided
187 strings, returning the modified strings separately in array context
188 or joined together in scalar context. Its arguments are not
189 modified.
190
191 colorvalid(ATTR[, ATTR ...])
192 colorvalid() takes attribute strings the same as color() and
193 returns true if all attributes are known and false otherwise.
194
195 Constant Interface
196 Alternately, if you import ":constants", you can use the following
197 constants directly:
198
199 CLEAR RESET BOLD DARK
200 FAINT UNDERLINE UNDERSCORE BLINK
201 REVERSE CONCEALED
202
203 BLACK RED GREEN YELLOW
204 BLUE MAGENTA CYAN WHITE
205 BRIGHT_BLACK BRIGHT_RED BRIGHT_GREEN BRIGHT_YELLOW
206 BRIGHT_BLUE BRIGHT_MAGENTA BRIGHT_CYAN BRIGHT_WHITE
207
208 ON_BLACK ON_RED ON_GREEN ON_YELLOW
209 ON_BLUE ON_MAGENTA ON_CYAN ON_WHITE
210 ON_BRIGHT_BLACK ON_BRIGHT_RED ON_BRIGHT_GREEN ON_BRIGHT_YELLOW
211 ON_BRIGHT_BLUE ON_BRIGHT_MAGENTA ON_BRIGHT_CYAN ON_BRIGHT_WHITE
212
213 These are the same as color('attribute') and can be used if you prefer
214 typing:
215
216 print BOLD BLUE ON_WHITE "Text", RESET, "\n";
217
218 to
219
220 print colored ("Text", 'bold blue on_white'), "\n";
221
222 (Note that the newline is kept separate to avoid confusing the terminal
223 as described above since a background color is being used.)
224
225 When using the constants, if you don't want to have to remember to add
226 the ", RESET" at the end of each print line, you can set
227 $Term::ANSIColor::AUTORESET to a true value. Then, the display mode
228 will automatically be reset if there is no comma after the constant.
229 In other words, with that variable set:
230
231 print BOLD BLUE "Text\n";
232
233 will reset the display mode afterward, whereas:
234
235 print BOLD, BLUE, "Text\n";
236
237 will not. If you are using background colors, you will probably want
238 to print the newline with a separate print statement to avoid confusing
239 the terminal.
240
241 The subroutine interface has the advantage over the constants interface
242 in that only two subroutines are exported into your namespace, versus
243 thirty-eight in the constants interface. On the flip side, the
244 constants interface has the advantage of better compile time error
245 checking, since misspelled names of colors or attributes in calls to
246 color() and colored() won't be caught until runtime whereas misspelled
247 names of constants will be caught at compile time. So, pollute your
248 namespace with almost two dozen subroutines that you may not even use
249 that often, or risk a silly bug by mistyping an attribute. Your
250 choice, TMTOWTDI after all.
251
252 The Color Stack
253 As of Term::ANSIColor 2.0, you can import ":pushpop" and maintain a
254 stack of colors using PUSHCOLOR, POPCOLOR, and LOCALCOLOR. PUSHCOLOR
255 takes the attribute string that starts its argument and pushes it onto
256 a stack of attributes. POPCOLOR removes the top of the stack and
257 restores the previous attributes set by the argument of a prior
258 PUSHCOLOR. LOCALCOLOR surrounds its argument in a PUSHCOLOR and
259 POPCOLOR so that the color resets afterward.
260
261 When using PUSHCOLOR, POPCOLOR, and LOCALCOLOR, it's particularly
262 important to not put commas between the constants.
263
264 print PUSHCOLOR BLUE "Text\n";
265
266 will correctly push BLUE onto the top of the stack.
267
268 print PUSHCOLOR, BLUE, "Text\n"; # wrong!
269
270 will not, and a subsequent pop won't restore the correct attributes.
271 PUSHCOLOR pushes the attributes set by its argument, which is normally
272 a string of color constants. It can't ask the terminal what the
273 current attributes are.
274
276 Bad escape sequence %s
277 (F) You passed an invalid ANSI escape sequence to uncolor().
278
279 Bareword "%s" not allowed while "strict subs" in use
280 (F) You probably mistyped a constant color name such as:
281
282 $Foobar = FOOBAR . "This line should be blue\n";
283
284 or:
285
286 @Foobar = FOOBAR, "This line should be blue\n";
287
288 This will only show up under use strict (another good reason to run
289 under use strict).
290
291 Invalid attribute name %s
292 (F) You passed an invalid attribute name to either color() or
293 colored().
294
295 Name "%s" used only once: possible typo
296 (W) You probably mistyped a constant color name such as:
297
298 print FOOBAR "This text is color FOOBAR\n";
299
300 It's probably better to always use commas after constant names in
301 order to force the next error.
302
303 No comma allowed after filehandle
304 (F) You probably mistyped a constant color name such as:
305
306 print FOOBAR, "This text is color FOOBAR\n";
307
308 Generating this fatal compile error is one of the main advantages
309 of using the constants interface, since you'll immediately know if
310 you mistype a color name.
311
312 No name for escape sequence %s
313 (F) The ANSI escape sequence passed to uncolor() contains escapes
314 which aren't recognized and can't be translated to names.
315
317 ANSI_COLORS_DISABLED
318 If this environment variable is set, all of the functions defined
319 by this module (color(), colored(), and all of the constants not
320 previously used in the program) will not output any escape
321 sequences and instead will just return the empty string or pass
322 through the original text as appropriate. This is intended to
323 support easy use of scripts using this module on platforms that
324 don't support ANSI escape sequences.
325
326 For it to have its proper effect, this environment variable must be
327 set before any color constants are used in the program.
328
330 It would be nice if one could leave off the commas around the constants
331 entirely and just say:
332
333 print BOLD BLUE ON_WHITE "Text\n" RESET;
334
335 but the syntax of Perl doesn't allow this. You need a comma after the
336 string. (Of course, you may consider it a bug that commas between all
337 the constants aren't required, in which case you may feel free to
338 insert commas unless you're using $Term::ANSIColor::AUTORESET or
339 PUSHCOLOR/POPCOLOR.)
340
341 For easier debugging, you may prefer to always use the commas when not
342 setting $Term::ANSIColor::AUTORESET or PUSHCOLOR/POPCOLOR so that
343 you'll get a fatal compile error rather than a warning.
344
345 It's not possible to use this module to embed formatting and color
346 attributes using Perl formats. They replace the escape character with
347 a space (as documented in perlform(1)), resulting in garbled output
348 from the unrecognized attribute. Even if there were a way around that
349 problem, the format doesn't know that the non-printing escape sequence
350 is zero-length and would incorrectly format the output. For formatted
351 output using color or other attributes, either use sprintf() instead or
352 use formline() and then add the color or other attributes after
353 formatting and before output.
354
356 The codes generated by this module are standard terminal control codes,
357 complying with ECMA-048 and ISO 6429 (generally referred to as "ANSI
358 color" for the color codes). The non-color control codes (bold, dark,
359 italic, underline, and reverse) are part of the earlier ANSI X3.64
360 standard for control sequences for video terminals and peripherals.
361
362 Note that not all displays are ISO 6429-compliant, or even
363 X3.64-compliant (or are even attempting to be so). This module will
364 not work as expected on displays that do not honor these escape
365 sequences, such as cmd.exe, 4nt.exe, and command.com under either
366 Windows NT or Windows 2000. They may just be ignored, or they may
367 display as an ESC character followed by some apparent garbage.
368
369 Jean Delvare provided the following table of different common terminal
370 emulators and their support for the various attributes and others have
371 helped me flesh it out:
372
373 clear bold faint under blink reverse conceal
374 ------------------------------------------------------------------------
375 xterm yes yes no yes yes yes yes
376 linux yes yes yes bold yes yes no
377 rxvt yes yes no yes bold/black yes no
378 dtterm yes yes yes yes reverse yes yes
379 teraterm yes reverse no yes rev/red yes no
380 aixterm kinda normal no yes no yes yes
381 PuTTY yes color no yes no yes no
382 Windows yes no no no no yes no
383 Cygwin SSH yes yes no color color color yes
384 Mac Terminal yes yes no yes yes yes yes
385
386 Windows is Windows telnet, Cygwin SSH is the OpenSSH implementation
387 under Cygwin on Windows NT, and Mac Terminal is the Terminal
388 application in Mac OS X. Where the entry is other than yes or no, that
389 emulator displays the given attribute as something else instead. Note
390 that on an aixterm, clear doesn't reset colors; you have to explicitly
391 set the colors back to what you want. More entries in this table are
392 welcome.
393
394 Note that codes 3 (italic), 6 (rapid blink), and 9 (strike-through) are
395 specified in ANSI X3.64 and ECMA-048 but are not commonly supported by
396 most displays and emulators and therefore aren't supported by this
397 module at the present time. ECMA-048 also specifies a large number of
398 other attributes, including a sequence of attributes for font changes,
399 Fraktur characters, double-underlining, framing, circling, and
400 overlining. As none of these attributes are widely supported or
401 useful, they also aren't currently supported by this module.
402
404 ECMA-048 is available on-line (at least at the time of this writing) at
405 http://www.ecma-international.org/publications/standards/Ecma-048.htm
406 <http://www.ecma-
407 international.org/publications/standards/Ecma-048.htm>.
408
409 ISO 6429 is available from ISO for a charge; the author of this module
410 does not own a copy of it. Since the source material for ISO 6429 was
411 ECMA-048 and the latter is available for free, there seems little
412 reason to obtain the ISO standard.
413
414 The current version of this module is always available from its web
415 site at <http://www.eyrie.org/~eagle/software/ansicolor/>. It is also
416 part of the Perl core distribution as of 5.6.0.
417
419 Original idea (using constants) by Zenin, reimplemented using subs by
420 Russ Allbery <rra@stanford.edu>, and then combined with the original
421 idea by Russ with input from Zenin. Russ Allbery now maintains this
422 module.
423
425 Copyright 1996, 1997, 1998, 2000, 2001, 2002, 2005, 2006, 2008, 2009,
426 2010, 2011 Russ Allbery <rra@stanford.edu> and Zenin. This program is
427 free software; you may redistribute it and/or modify it under the same
428 terms as Perl itself.
429
430 PUSHCOLOR, POPCOLOR, and LOCALCOLOR were contributed by openmethods.com
431 voice solutions.
432
433
434
435perl v5.16.3 2013-03-04 Term::ANSIColor(3pm)