1Term::ANSIColor(3pm)   Perl Programmers Reference Guide   Term::ANSIColor(3pm)
2
3
4

NAME

6       Term::ANSIColor - Color screen output using ANSI escape sequences
7

SYNOPSIS

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(:constants);
23           print BOLD, BLUE, "This text is in bold blue.\n", RESET;
24
25           use Term::ANSIColor qw(:constants);
26           {
27               local $Term::ANSIColor::AUTORESET = 1;
28               print BOLD BLUE "This text is in bold blue.\n";
29               print "This text is normal.\n";
30           }
31
32           use Term::ANSIColor qw(:pushpop);
33           print PUSHCOLOR RED ON_GREEN "This text is red on green.\n";
34           print PUSHCOLOR BLUE "This text is blue on green.\n";
35           print RESET BLUE "This text is just blue.\n";
36           print POPCOLOR "Back to red on green.\n";
37           print LOCALCOLOR GREEN ON_BLUE "This text is green on blue.\n";
38           print "This text is red on green.\n";
39           {
40               local $Term::ANSIColor::AUTOLOCAL = 1;
41               print ON_BLUE "This text is red on blue.\n";
42               print "This text is red on green.\n";
43           }
44           print POPCOLOR "Back to whatever we started as.\n";
45

DESCRIPTION

47       This module has two interfaces, one through color() and colored() and
48       the other through constants.  It also offers the utility function
49       uncolor(), which has to be explicitly imported to be used (see
50       "SYNOPSIS").
51
52       color() takes any number of strings as arguments and considers them to
53       be space-separated lists of attributes.  It then forms and returns the
54       escape sequence to set those attributes.  It doesn't print it out, just
55       returns it, so you'll have to print it yourself if you want to (this is
56       so that you can save it as a string, pass it to something else, send it
57       to a file handle, or do anything else with it that you might care to).
58
59       uncolor() performs the opposite translation, turning escape sequences
60       into a list of strings.
61
62       The recognized non-color attributes are clear, reset, bold, dark,
63       faint, underline, underscore, blink, reverse, and concealed.  Clear and
64       reset (reset to default attributes), dark and faint (dim and
65       saturated), and underline and underscore are equivalent, so use
66       whichever is the most intuitive to you.  The recognized foreground
67       color attributes are black, red, green, yellow, blue, magenta, cyan,
68       and white.  The recognized background color attributes are on_black,
69       on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, and
70       on_white.  Case is not significant.
71
72       Note that not all attributes are supported by all terminal types, and
73       some terminals may not support any of these sequences.  Dark and faint,
74       blink, and concealed in particular are frequently not implemented.
75
76       Attributes, once set, last until they are unset (by sending the
77       attribute "clear" or "reset").  Be careful to do this, or otherwise
78       your attribute will last after your script is done running, and people
79       get very annoyed at having their prompt and typing changed to weird
80       colors.
81
82       As an aid to help with this, colored() takes a scalar as the first
83       argument and any number of attribute strings as the second argument and
84       returns the scalar wrapped in escape codes so that the attributes will
85       be set as requested before the string and reset to normal after the
86       string.  Alternately, you can pass a reference to an array as the first
87       argument, and then the contents of that array will be taken as
88       attributes and color codes and the remainder of the arguments as text
89       to colorize.
90
91       Normally, colored() just puts attribute codes at the beginning and end
92       of the string, but if you set $Term::ANSIColor::EACHLINE to some
93       string, that string will be considered the line delimiter and the
94       attribute will be set at the beginning of each line of the passed
95       string and reset at the end of each line.  This is often desirable if
96       the output contains newlines and you're using background colors, since
97       a background color that persists across a newline is often interpreted
98       by the terminal as providing the default background color for the next
99       line.  Programs like pagers can also be confused by attributes that
100       span lines.  Normally you'll want to set $Term::ANSIColor::EACHLINE to
101       "\n" to use this feature.
102
103       Alternately, if you import ":constants", you can use the constants
104       CLEAR, RESET, BOLD, DARK, UNDERLINE, UNDERSCORE, BLINK, REVERSE,
105       CONCEALED, BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE,
106       ON_BLACK, ON_RED, ON_GREEN, ON_YELLOW, ON_BLUE, ON_MAGENTA, ON_CYAN,
107       and ON_WHITE directly.  These are the same as color('attribute') and
108       can be used if you prefer typing:
109
110           print BOLD BLUE ON_WHITE "Text", RESET, "\n";
111
112       to
113
114           print colored ("Text", 'bold blue on_white'), "\n";
115
116       (Note that the newline is kept separate to avoid confusing the terminal
117       as described above since a background color is being used.)
118
119       When using the constants, if you don't want to have to remember to add
120       the ", RESET" at the end of each print line, you can set
121       $Term::ANSIColor::AUTORESET to a true value.  Then, the display mode
122       will automatically be reset if there is no comma after the constant.
123       In other words, with that variable set:
124
125           print BOLD BLUE "Text\n";
126
127       will reset the display mode afterward, whereas:
128
129           print BOLD, BLUE, "Text\n";
130
131       will not.  If you are using background colors, you will probably want
132       to print the newline with a separate print statement to avoid confusing
133       the terminal.
134
135       The subroutine interface has the advantage over the constants interface
136       in that only two subroutines are exported into your namespace, versus
137       twenty-two in the constants interface.  On the flip side, the constants
138       interface has the advantage of better compile time error checking,
139       since misspelled names of colors or attributes in calls to color() and
140       colored() won't be caught until runtime whereas misspelled names of
141       constants will be caught at compile time.  So, pollute your namespace
142       with almost two dozen subroutines that you may not even use that often,
143       or risk a silly bug by mistyping an attribute.  Your choice, TMTOWTDI
144       after all.
145
146       As of Term::ANSIColor 2.0, you can import ":pushpop" and maintain a
147       stack of colors using PUSHCOLOR, POPCOLOR, and LOCALCOLOR.  PUSHCOLOR
148       takes the attribute string that starts its argument and pushes it onto
149       a stack of attributes.  POPCOLOR removes the top of the stack and
150       restores the previous attributes set by the argument of a prior
151       PUSHCOLOR.  LOCALCOLOR surrounds its argument in a PUSHCOLOR and
152       POPCOLOR so that the color resets afterward.
153
154       When using PUSHCOLOR, POPCOLOR, and LOCALCOLOR, it's particularly
155       important to not put commas between the constants.
156
157           print PUSHCOLOR BLUE "Text\n";
158
159       will correctly push BLUE onto the top of the stack.
160
161           print PUSHCOLOR, BLUE, "Text\n";    # wrong!
162
163       will not, and a subsequent pop won't restore the correct attributes.
164       PUSHCOLOR pushes the attributes set by its argument, which is normally
165       a string of color constants.  It can't ask the terminal what the
166       current attributes are.
167

DIAGNOSTICS

169       Bad escape sequence %s
170           (F) You passed an invalid ANSI escape sequence to uncolor().
171
172       Bareword "%s" not allowed while "strict subs" in use
173           (F) You probably mistyped a constant color name such as:
174
175               $Foobar = FOOBAR . "This line should be blue\n";
176
177           or:
178
179               @Foobar = FOOBAR, "This line should be blue\n";
180
181           This will only show up under use strict (another good reason to run
182           under use strict).
183
184       Invalid attribute name %s
185           (F) You passed an invalid attribute name to either color() or
186           colored().
187
188       Name "%s" used only once: possible typo
189           (W) You probably mistyped a constant color name such as:
190
191               print FOOBAR "This text is color FOOBAR\n";
192
193           It's probably better to always use commas after constant names in
194           order to force the next error.
195
196       No comma allowed after filehandle
197           (F) You probably mistyped a constant color name such as:
198
199               print FOOBAR, "This text is color FOOBAR\n";
200
201           Generating this fatal compile error is one of the main advantages
202           of using the constants interface, since you'll immediately know if
203           you mistype a color name.
204
205       No name for escape sequence %s
206           (F) The ANSI escape sequence passed to uncolor() contains escapes
207           which aren't recognized and can't be translated to names.
208

ENVIRONMENT

210       ANSI_COLORS_DISABLED
211           If this environment variable is set, all of the functions defined
212           by this module (color(), colored(), and all of the constants not
213           previously used in the program) will not output any escape
214           sequences and instead will just return the empty string or pass
215           through the original text as appropriate.  This is intended to
216           support easy use of scripts using this module on platforms that
217           don't support ANSI escape sequences.
218
219           For it to have its proper effect, this environment variable must be
220           set before any color constants are used in the program.
221

RESTRICTIONS

223       It would be nice if one could leave off the commas around the constants
224       entirely and just say:
225
226           print BOLD BLUE ON_WHITE "Text\n" RESET;
227
228       but the syntax of Perl doesn't allow this.  You need a comma after the
229       string.  (Of course, you may consider it a bug that commas between all
230       the constants aren't required, in which case you may feel free to
231       insert commas unless you're using $Term::ANSIColor::AUTORESET or
232       PUSHCOLOR/POPCOLOR.)
233
234       For easier debugging, you may prefer to always use the commas when not
235       setting $Term::ANSIColor::AUTORESET or PUSHCOLOR/POPCOLOR so that
236       you'll get a fatal compile error rather than a warning.
237

NOTES

239       The codes generated by this module are standard terminal control codes,
240       complying with ECMA-048 and ISO 6429 (generally referred to as "ANSI
241       color" for the color codes).  The non-color control codes (bold, dark,
242       italic, underline, and reverse) are part of the earlier ANSI X3.64
243       standard for control sequences for video terminals and peripherals.
244
245       Note that not all displays are ISO 6429-compliant, or even
246       X3.64-compliant (or are even attempting to be so).  This module will
247       not work as expected on displays that do not honor these escape
248       sequences, such as cmd.exe, 4nt.exe, and command.com under either
249       Windows NT or Windows 2000.  They may just be ignored, or they may
250       display as an ESC character followed by some apparent garbage.
251
252       Jean Delvare provided the following table of different common terminal
253       emulators and their support for the various attributes and others have
254       helped me flesh it out:
255
256                     clear    bold     faint   under    blink   reverse  conceal
257        ------------------------------------------------------------------------
258        xterm         yes      yes      no      yes     bold      yes      yes
259        linux         yes      yes      yes    bold      yes      yes      no
260        rxvt          yes      yes      no      yes  bold/black   yes      no
261        dtterm        yes      yes      yes     yes    reverse    yes      yes
262        teraterm      yes    reverse    no      yes    rev/red    yes      no
263        aixterm      kinda   normal     no      yes      no       yes      yes
264        PuTTY         yes     color     no      yes      no       yes      no
265        Windows       yes      no       no      no       no       yes      no
266        Cygwin SSH    yes      yes      no     color    color    color     yes
267        Mac Terminal  yes      yes      no      yes      yes      yes      yes
268
269       Windows is Windows telnet, Cygwin SSH is the OpenSSH implementation
270       under Cygwin on Windows NT, and Mac Terminal is the Terminal
271       application in Mac OS X.  Where the entry is other than yes or no, that
272       emulator displays the given attribute as something else instead.  Note
273       that on an aixterm, clear doesn't reset colors; you have to explicitly
274       set the colors back to what you want.  More entries in this table are
275       welcome.
276
277       Note that codes 3 (italic), 6 (rapid blink), and 9 (strike-through) are
278       specified in ANSI X3.64 and ECMA-048 but are not commonly supported by
279       most displays and emulators and therefore aren't supported by this
280       module at the present time.  ECMA-048 also specifies a large number of
281       other attributes, including a sequence of attributes for font changes,
282       Fraktur characters, double-underlining, framing, circling, and
283       overlining.  As none of these attributes are widely supported or
284       useful, they also aren't currently supported by this module.
285

SEE ALSO

287       ECMA-048 is available on-line (at least at the time of this writing) at
288       <http://www.ecma-international.org/publications/standards/ECMA-048.HTM>.
289
290       ISO 6429 is available from ISO for a charge; the author of this module
291       does not own a copy of it.  Since the source material for ISO 6429 was
292       ECMA-048 and the latter is available for free, there seems little
293       reason to obtain the ISO standard.
294
295       The current version of this module is always available from its web
296       site at <http://www.eyrie.org/~eagle/software/ansicolor/>.  It is also
297       part of the Perl core distribution as of 5.6.0.
298

AUTHORS

300       Original idea (using constants) by Zenin, reimplemented using subs by
301       Russ Allbery <rra@stanford.edu>, and then combined with the original
302       idea by Russ with input from Zenin.  Russ Allbery now maintains this
303       module.
304
306       Copyright 1996, 1997, 1998, 2000, 2001, 2002, 2005, 2006, 2008, 2009
307       Russ Allbery <rra@stanford.edu> and Zenin.  This program is free
308       software; you may redistribute it and/or modify it under the same terms
309       as Perl itself.
310
311       PUSHCOLOR, POPCOLOR, and LOCALCOLOR were contributed by openmethods.com
312       voice solutions.
313
314
315
316perl v5.10.1                      2009-04-15              Term::ANSIColor(3pm)
Impressum