Debian Codegraph @

upstream/opensuse-tumbleweed/man3/Term::ANSIColor.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "Term::ANSIColor 3pm"
61 .TH Term::ANSIColor 3pm 2026-03-09 "perl v5.42.1" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 Term::ANSIColor \- Color screen output using ANSI escape sequences
68 .SH SYNOPSIS
69 .IX Header "SYNOPSIS"
70 .Vb 11
71 \& use Term::ANSIColor;
72 \& print color(\*(Aqbold blue\*(Aq);
73 \& print "This text is bold blue.\en";
74 \& print color(\*(Aqreset\*(Aq);
75 \& print "This text is normal.\en";
76 \& print colored("Yellow on magenta.", \*(Aqyellow on_magenta\*(Aq), "\en";
77 \& print "This text is normal.\en";
78 \& print colored([\*(Aqyellow on_magenta\*(Aq], \*(AqYellow on magenta.\*(Aq, "\en");
79 \& print colored([\*(Aqred on_bright_yellow\*(Aq], \*(AqRed on bright yellow.\*(Aq, "\en");
80 \& print colored([\*(Aqbright_red on_black\*(Aq], \*(AqBright red on black.\*(Aq, "\en");
81 \& print "\en";
82 \&
83 \& # Map escape sequences back to color names.
84 \& use Term::ANSIColor 1.04 qw(uncolor);
85 \& my @names = uncolor(\*(Aq01;31\*(Aq);
86 \& print join(q{ }, @names), "\en";
87 \&
88 \& # Strip all color escape sequences.
89 \& use Term::ANSIColor 2.01 qw(colorstrip);
90 \& print colorstrip("\ee[1mThis is bold\ee[0m"), "\en";
91 \&
92 \& # Determine whether a color is valid.
93 \& use Term::ANSIColor 2.02 qw(colorvalid);
94 \& my $valid = colorvalid(\*(Aqblue bold\*(Aq, \*(Aqon_magenta\*(Aq);
95 \& print "Color string is ", $valid ? "valid\en" : "invalid\en";
96 \&
97 \& # Create new aliases for colors.
98 \& use Term::ANSIColor 4.00 qw(coloralias);
99 \& coloralias(\*(Aqalert\*(Aq, \*(Aqred\*(Aq);
100 \& print "Alert is ", coloralias(\*(Aqalert\*(Aq), "\en";
101 \& print colored("This is in red.", \*(Aqalert\*(Aq), "\en";
102 \&
103 \& use Term::ANSIColor qw(:constants);
104 \& print BOLD, BLUE, "This text is in bold blue.\en", RESET;
105 \&
106 \& use Term::ANSIColor qw(:constants);
107 \& {
108 \& local $Term::ANSIColor::AUTORESET = 1;
109 \& print BOLD BLUE "This text is in bold blue.\en";
110 \& print "This text is normal.\en";
111 \& }
112 \&
113 \& use Term::ANSIColor 2.00 qw(:pushpop);
114 \& print PUSHCOLOR RED ON_GREEN "This text is red on green.\en";
115 \& print PUSHCOLOR BRIGHT_BLUE "This text is bright blue on green.\en";
116 \& print RESET BRIGHT_BLUE "This text is just bright blue.\en";
117 \& print POPCOLOR "Back to red on green.\en";
118 \& print LOCALCOLOR GREEN ON_BLUE "This text is green on blue.\en";
119 \& print "This text is red on green.\en";
120 \& {
121 \& local $Term::ANSIColor::AUTOLOCAL = 1;
122 \& print ON_BLUE "This text is red on blue.\en";
123 \& print "This text is red on green.\en";
124 \& }
125 \& print POPCOLOR "Back to whatever we started as.\en";
126 .Ve
127 .SH DESCRIPTION
128 .IX Header "DESCRIPTION"
129 This module has two interfaces, one through \fBcolor()\fR and \fBcolored()\fR and the
130 other through constants. It also offers the utility functions \fBuncolor()\fR,
131 \&\fBcolorstrip()\fR, \fBcolorvalid()\fR, and \fBcoloralias()\fR, which have to be explicitly
132 imported to be used (see "SYNOPSIS").
133 .PP
134 If you are using Term::ANSIColor in a console command, consider supporting the
135 CLICOLOR standard. See "Supporting CLICOLOR" for more information.
136 .PP
137 See "COMPATIBILITY" for the versions of Term::ANSIColor that introduced
138 particular features and the versions of Perl that included them.
139 .SS "Supported Colors"
140 .IX Subsection "Supported Colors"
141 Terminal emulators that support color divide into four types: ones that
142 support only eight colors, ones that support sixteen, ones that support 256,
143 and ones that support 24\-bit color. This module provides the ANSI escape
144 codes for all of them. These colors are referred to as ANSI colors 0 through
145 7 (normal), 8 through 15 (16\-color), 16 through 255 (256\-color), and true
146 color (called direct\-color by \fBxterm\fR).
147 .PP
148 Unfortunately, interpretation of colors 0 through 7 often depends on
149 whether the emulator supports eight colors or sixteen colors. Emulators
150 that only support eight colors (such as the Linux console) will display
151 colors 0 through 7 with normal brightness and ignore colors 8 through 15,
152 treating them the same as white. Emulators that support 16 colors, such
153 as gnome\-terminal, normally display colors 0 through 7 as dim or darker
154 versions and colors 8 through 15 as normal brightness. On such emulators,
155 the "normal" white (color 7) usually is shown as pale grey, requiring
156 bright white (15) to be used to get a real white color. Bright black
157 usually is a dark grey color, although some terminals display it as pure
158 black. Some sixteen\-color terminal emulators also treat normal yellow
159 (color 3) as orange or brown, and bright yellow (color 11) as yellow.
160 .PP
161 Following the normal convention of sixteen\-color emulators, this module
162 provides a pair of attributes for each color. For every normal color (0
163 through 7), the corresponding bright color (8 through 15) is obtained by
164 prepending the string \f(CW\*(C`bright_\*(C'\fR to the normal color name. For example,
165 \&\f(CW\*(C`red\*(C'\fR is color 1 and \f(CW\*(C`bright_red\*(C'\fR is color 9. The same applies for
166 background colors: \f(CW\*(C`on_red\*(C'\fR is the normal color and \f(CW\*(C`on_bright_red\*(C'\fR is
167 the bright color. Capitalize these strings for the constant interface.
168 .PP
169 There is unfortunately no way to know whether the current emulator
170 supports more than eight colors, which makes the choice of colors
171 difficult. The most conservative choice is to use only the regular
172 colors, which are at least displayed on all emulators. However, they will
173 appear dark in sixteen\-color terminal emulators, including most common
174 emulators in UNIX X environments. If you know the display is one of those
175 emulators, you may wish to use the bright variants instead. Even better,
176 offer the user a way to configure the colors for a given application to
177 fit their terminal emulator.
178 .PP
179 For 256\-color emulators, this module additionally provides \f(CW\*(C`ansi0\*(C'\fR
180 through \f(CW\*(C`ansi15\*(C'\fR, which are the same as colors 0 through 15 in
181 sixteen\-color emulators but use the 256\-color escape syntax, \f(CW\*(C`grey0\*(C'\fR
182 through \f(CW\*(C`grey23\*(C'\fR ranging from nearly black to nearly white, and a set of
183 RGB colors. The RGB colors are of the form \f(CW\*(C`rgb\fR\f(CIRGB\fR\f(CW\*(C'\fR where \fIR\fR, \fIG\fR,
184 and \fIB\fR are numbers from 0 to 5 giving the intensity of red, green, and
185 blue. The grey and RGB colors are also available as \f(CW\*(C`ansi16\*(C'\fR through
186 \&\f(CW\*(C`ansi255\*(C'\fR if you want simple names for all 256 colors. \f(CW\*(C`on_\*(C'\fR variants
187 of all of these colors are also provided. These colors may be ignored
188 completely on non\-256\-color terminals or may be misinterpreted and produce
189 random behavior. Additional attributes such as blink, italic, or bold may
190 not work with the 256\-color palette.
191 .PP
192 For true color emulators, this module supports attributes of the form \f(CW\*(C`r\fR\f(CINNN\fR\f(CWg\fR\f(CINNN\fR\f(CWb\fR\f(CINNN\fR\f(CW\*(C'\fR and \f(CW\*(C`on_r\fR\f(CINNN\fR\f(CWg\fR\f(CINNN\fR\f(CWb\fR\f(CINNN\fR\f(CW\*(C'\fR for all values of
193 \&\fINNN\fR between 0 and 255. These represent foreground and background colors,
194 respectively, with the RGB values given by the \fINNN\fR numbers. These colors
195 may be ignored completely on non\-true\-color terminals or may be misinterpreted
196 and produce random behavior.
197 .SS "Function Interface"
198 .IX Subsection "Function Interface"
199 The function interface uses attribute strings to describe the colors and
200 text attributes to assign to text. The recognized non\-color attributes
201 are clear, reset, bold, dark, faint, italic, underline, underscore, blink,
202 reverse, and concealed. Clear and reset (reset to default attributes),
203 dark and faint (dim and saturated), and underline and underscore are
204 equivalent, so use whichever is the most intuitive to you.
205 .PP
206 Note that not all attributes are supported by all terminal types, and some
207 terminals may not support any of these sequences. Dark and faint, italic,
208 blink, and concealed in particular are frequently not implemented.
209 .PP
210 The recognized normal foreground color attributes (colors 0 to 7) are:
211 .PP
212 .Vb 1
213 \& black red green yellow blue magenta cyan white
214 .Ve
215 .PP
216 The corresponding bright foreground color attributes (colors 8 to 15) are:
217 .PP
218 .Vb 2
219 \& bright_black bright_red bright_green bright_yellow
220 \& bright_blue bright_magenta bright_cyan bright_white
221 .Ve
222 .PP
223 The recognized normal background color attributes (colors 0 to 7) are:
224 .PP
225 .Vb 2
226 \& on_black on_red on_green on yellow
227 \& on_blue on_magenta on_cyan on_white
228 .Ve
229 .PP
230 The recognized bright background color attributes (colors 8 to 15) are:
231 .PP
232 .Vb 2
233 \& on_bright_black on_bright_red on_bright_green on_bright_yellow
234 \& on_bright_blue on_bright_magenta on_bright_cyan on_bright_white
235 .Ve
236 .PP
237 For 256\-color terminals, the recognized foreground colors are:
238 .PP
239 .Vb 2
240 \& ansi0 .. ansi255
241 \& grey0 .. grey23
242 .Ve
243 .PP
244 plus \f(CW\*(C`rgb\fR\f(CIRGB\fR\f(CW\*(C'\fR for \fIR\fR, \fIG\fR, and \fIB\fR values from 0 to 5, such as
245 \&\f(CW\*(C`rgb000\*(C'\fR or \f(CW\*(C`rgb515\*(C'\fR. Similarly, the recognized background colors are:
246 .PP
247 .Vb 2
248 \& on_ansi0 .. on_ansi255
249 \& on_grey0 .. on_grey23
250 .Ve
251 .PP
252 plus \f(CW\*(C`on_rgb\fR\f(CIRGB\fR\f(CW\*(C'\fR for \fIR\fR, \fIG\fR, and \fIB\fR values from 0 to 5.
253 .PP
254 For true color terminals, the recognized foreground colors are \f(CW\*(C`r\fR\f(CIRRR\fR\f(CWg\fR\f(CIGGG\fR\f(CWb\fR\f(CIBBB\fR\f(CW\*(C'\fR for \fIRRR\fR, \fIGGG\fR, and \fIBBB\fR values between 0 and
255 255. Similarly, the recognized background colors are \f(CW\*(C`on_r\fR\f(CIRRR\fR\f(CWg\fR\f(CIGGG\fR\f(CWb\fR\f(CIBBB\fR\f(CW\*(C'\fR for \fIRRR\fR, \fIGGG\fR, and \fIBBB\fR values between 0
256 and 255.
257 .PP
258 For any of the above listed attributes, case is not significant.
259 .PP
260 Attributes, once set, last until they are unset (by printing the attribute
261 \&\f(CW\*(C`clear\*(C'\fR or \f(CW\*(C`reset\*(C'\fR). Be careful to do this, or otherwise your attribute
262 will last after your script is done running, and people get very annoyed
263 at having their prompt and typing changed to weird colors.
264 .IP "color(ATTR[, ATTR ...])" 4
265 .IX Item "color(ATTR[, ATTR ...])"
266 \&\fBcolor()\fR takes any number of strings as arguments and considers them to be
267 space\-separated lists of attributes. It then forms and returns the escape
268 sequence to set those attributes. It doesn\*(Aqt print it out, just returns
269 it, so you\*(Aqll have to print it yourself if you want to. This is so that
270 you can save it as a string, pass it to something else, send it to a file
271 handle, or do anything else with it that you might care to. \fBcolor()\fR
272 throws an exception if given an invalid attribute.
273 .IP "colored(STRING, ATTR[, ATTR ...])" 4
274 .IX Item "colored(STRING, ATTR[, ATTR ...])"
275 .PD 0
276 .IP "colored(ATTR\-REF, STRING[, STRING...])" 4
277 .IX Item "colored(ATTR-REF, STRING[, STRING...])"
278 .PD
279 As an aid in resetting colors, \fBcolored()\fR takes a scalar as the first
280 argument and any number of attribute strings as the second argument and
281 returns the scalar wrapped in escape codes so that the attributes will be
282 set as requested before the string and reset to normal after the string.
283 Alternately, you can pass a reference to an array as the first argument,
284 and then the contents of that array will be taken as attributes and color
285 codes and the remainder of the arguments as text to colorize.
286 .Sp
287 Normally, \fBcolored()\fR just puts attribute codes at the beginning and end of
288 the string, but if you set \f(CW$Term::ANSIColor::EACHLINE\fR to some string, that
289 string will be considered the line delimiter and the attribute will be set
290 at the beginning of each line of the passed string and reset at the end of
291 each line. This is often desirable if the output contains newlines and
292 you\*(Aqre using background colors, since a background color that persists
293 across a newline is often interpreted by the terminal as providing the
294 default background color for the next line. Programs like pagers can also
295 be confused by attributes that span lines. Normally you\*(Aqll want to set
296 \&\f(CW$Term::ANSIColor::EACHLINE\fR to \f(CW"\en"\fR to use this feature.
297 .Sp
298 Particularly consider setting \f(CW$Term::ANSIColor::EACHLINE\fR if you are
299 interleaving output to standard output and standard error and you aren\*(Aqt
300 flushing standard output (via \fBautoflush()\fR or setting \f(CW$|\fR). If you don\*(Aqt,
301 the code to reset the color may unexpectedly sit in the standard output buffer
302 rather than going to the display, causing standard error output to appear in
303 the wrong color.
304 .IP uncolor(ESCAPE) 4
305 .IX Item "uncolor(ESCAPE)"
306 \&\fBuncolor()\fR performs the opposite translation as \fBcolor()\fR, turning escape
307 sequences into a list of strings corresponding to the attributes being set
308 by those sequences. \fBuncolor()\fR will never return \f(CW\*(C`ansi16\*(C'\fR through
309 \&\f(CW\*(C`ansi255\*(C'\fR, instead preferring the \f(CW\*(C`grey\*(C'\fR and \f(CW\*(C`rgb\*(C'\fR names (and likewise
310 for \f(CW\*(C`on_ansi16\*(C'\fR through \f(CW\*(C`on_ansi255\*(C'\fR).
311 .IP "colorstrip(STRING[, STRING ...])" 4
312 .IX Item "colorstrip(STRING[, STRING ...])"
313 \&\fBcolorstrip()\fR removes all color escape sequences from the provided strings,
314 returning the modified strings separately in array context or joined
315 together in scalar context. Its arguments are not modified.
316 .IP "colorvalid(ATTR[, ATTR ...])" 4
317 .IX Item "colorvalid(ATTR[, ATTR ...])"
318 \&\fBcolorvalid()\fR takes attribute strings the same as \fBcolor()\fR and returns true
319 if all attributes are known and false otherwise.
320 .IP "coloralias(ALIAS[, ATTR ...])" 4
321 .IX Item "coloralias(ALIAS[, ATTR ...])"
322 If ATTR is specified, it is interpreted as a list of space\-separated strings
323 naming attributes or existing aliases. In this case, \fBcoloralias()\fR sets up an
324 alias of ALIAS for the set of attributes given by ATTR. From that point
325 forward, ALIAS can be passed into \fBcolor()\fR, \fBcolored()\fR, and \fBcolorvalid()\fR and
326 will have the same meaning as the sequence of attributes given in ATTR. One
327 possible use of this facility is to give more meaningful names to the
328 256\-color RGB colors. Only ASCII alphanumerics, \f(CW\*(C`.\*(C'\fR, \f(CW\*(C`_\*(C'\fR, and \f(CW\*(C`\-\*(C'\fR are
329 allowed in alias names.
330 .Sp
331 If ATTR includes aliases, those aliases will be expanded at definition time
332 and their values will be used to define the new alias. This means that if you
333 define an alias A in terms of another alias B, and then later redefine alias
334 B, the value of alias A will not change.
335 .Sp
336 If ATTR is not specified, \fBcoloralias()\fR returns the standard attribute or
337 attributes to which ALIAS is aliased, if any, or undef if ALIAS does not
338 exist. If it is aliased to multiple attributes, the return value will be a
339 single string and the attributes will be separated by spaces.
340 .Sp
341 This is the same facility used by the ANSI_COLORS_ALIASES environment
342 variable (see "ENVIRONMENT" below) but can be used at runtime, not just
343 when the module is loaded.
344 .Sp
345 Later invocations of \fBcoloralias()\fR with the same ALIAS will override
346 earlier aliases. There is no way to remove an alias.
347 .Sp
348 Aliases have no effect on the return value of \fBuncolor()\fR.
349 .Sp
350 \&\fBWARNING\fR: Aliases are global and affect all callers in the same process.
351 There is no way to set an alias limited to a particular block of code or a
352 particular object.
353 .SS "Constant Interface"
354 .IX Subsection "Constant Interface"
355 Alternately, if you import \f(CW\*(C`:constants\*(C'\fR, you can use the following
356 constants directly:
357 .PP
358 .Vb 3
359 \& CLEAR RESET BOLD DARK
360 \& FAINT ITALIC UNDERLINE UNDERSCORE
361 \& BLINK REVERSE CONCEALED
362 \&
363 \& BLACK RED GREEN YELLOW
364 \& BLUE MAGENTA CYAN WHITE
365 \& BRIGHT_BLACK BRIGHT_RED BRIGHT_GREEN BRIGHT_YELLOW
366 \& BRIGHT_BLUE BRIGHT_MAGENTA BRIGHT_CYAN BRIGHT_WHITE
367 \&
368 \& ON_BLACK ON_RED ON_GREEN ON_YELLOW
369 \& ON_BLUE ON_MAGENTA ON_CYAN ON_WHITE
370 \& ON_BRIGHT_BLACK ON_BRIGHT_RED ON_BRIGHT_GREEN ON_BRIGHT_YELLOW
371 \& ON_BRIGHT_BLUE ON_BRIGHT_MAGENTA ON_BRIGHT_CYAN ON_BRIGHT_WHITE
372 .Ve
373 .PP
374 These are the same as color(\*(Aqattribute\*(Aq) and can be used if you prefer
375 typing:
376 .PP
377 .Vb 1
378 \& print BOLD BLUE ON_WHITE "Text", RESET, "\en";
379 .Ve
380 .PP
381 to
382 .PP
383 .Vb 1
384 \& print colored ("Text", \*(Aqbold blue on_white\*(Aq), "\en";
385 .Ve
386 .PP
387 (Note that the newline is kept separate to avoid confusing the terminal as
388 described above since a background color is being used.)
389 .PP
390 If you import \f(CW\*(C`:constants256\*(C'\fR, you can use the following constants
391 directly:
392 .PP
393 .Vb 2
394 \& ANSI0 .. ANSI255
395 \& GREY0 .. GREY23
396 \&
397 \& RGBXYZ (for X, Y, and Z values from 0 to 5, like RGB000 or RGB515)
398 \&
399 \& ON_ANSI0 .. ON_ANSI255
400 \& ON_GREY0 .. ON_GREY23
401 \&
402 \& ON_RGBXYZ (for X, Y, and Z values from 0 to 5)
403 .Ve
404 .PP
405 Note that \f(CW\*(C`:constants256\*(C'\fR does not include the other constants, so if you
406 want to mix both, you need to include \f(CW\*(C`:constants\*(C'\fR as well. You may want
407 to explicitly import at least \f(CW\*(C`RESET\*(C'\fR, as in:
408 .PP
409 .Vb 1
410 \& use Term::ANSIColor 4.00 qw(RESET :constants256);
411 .Ve
412 .PP
413 True color and aliases are not supported by the constant interface.
414 .PP
415 When using the constants, if you don\*(Aqt want to have to remember to add the
416 \&\f(CW\*(C`, RESET\*(C'\fR at the end of each print line, you can set
417 \&\f(CW$Term::ANSIColor::AUTORESET\fR to a true value. Then, the display mode will
418 automatically be reset if there is no comma after the constant. In other
419 words, with that variable set:
420 .PP
421 .Vb 1
422 \& print BOLD BLUE "Text\en";
423 .Ve
424 .PP
425 will reset the display mode afterward, whereas:
426 .PP
427 .Vb 1
428 \& print BOLD, BLUE, "Text\en";
429 .Ve
430 .PP
431 will not. If you are using background colors, you will probably want to
432 either use \fBsay()\fR (in newer versions of Perl) or print the newline with a
433 separate print statement to avoid confusing the terminal.
434 .PP
435 If \f(CW$Term::ANSIColor::AUTOLOCAL\fR is set (see below), it takes precedence
436 over \f(CW$Term::ANSIColor::AUTORESET\fR, and the latter is ignored.
437 .PP
438 The subroutine interface has the advantage over the constants interface in
439 that only two subroutines are exported into your namespace, versus
440 thirty\-eight in the constants interface, and aliases and true color attributes
441 are supported. On the flip side, the constants interface has the advantage of
442 better compile time error checking, since misspelled names of colors or
443 attributes in calls to \fBcolor()\fR and \fBcolored()\fR won\*(Aqt be caught until runtime
444 whereas misspelled names of constants will be caught at compile time. So,
445 pollute your namespace with almost two dozen subroutines that you may not even
446 use that often, or risk a silly bug by mistyping an attribute. Your choice,
447 TMTOWTDI after all.
448 .SS "The Color Stack"
449 .IX Subsection "The Color Stack"
450 You can import \f(CW\*(C`:pushpop\*(C'\fR and maintain a stack of colors using PUSHCOLOR,
451 POPCOLOR, and LOCALCOLOR. PUSHCOLOR takes the attribute string that
452 starts its argument and pushes it onto a stack of attributes. POPCOLOR
453 removes the top of the stack and restores the previous attributes set by
454 the argument of a prior PUSHCOLOR. LOCALCOLOR surrounds its argument in a
455 PUSHCOLOR and POPCOLOR so that the color resets afterward.
456 .PP
457 If \f(CW$Term::ANSIColor::AUTOLOCAL\fR is set, each sequence of color constants
458 will be implicitly preceded by LOCALCOLOR. In other words, the following:
459 .PP
460 .Vb 4
461 \& {
462 \& local $Term::ANSIColor::AUTOLOCAL = 1;
463 \& print BLUE "Text\en";
464 \& }
465 .Ve
466 .PP
467 is equivalent to:
468 .PP
469 .Vb 1
470 \& print LOCALCOLOR BLUE "Text\en";
471 .Ve
472 .PP
473 If \f(CW$Term::ANSIColor::AUTOLOCAL\fR is set, it takes precedence over
474 \&\f(CW$Term::ANSIColor::AUTORESET\fR, and the latter is ignored.
475 .PP
476 When using PUSHCOLOR, POPCOLOR, and LOCALCOLOR, it\*(Aqs particularly
477 important to not put commas between the constants.
478 .PP
479 .Vb 1
480 \& print PUSHCOLOR BLUE "Text\en";
481 .Ve
482 .PP
483 will correctly push BLUE onto the top of the stack.
484 .PP
485 .Vb 1
486 \& print PUSHCOLOR, BLUE, "Text\en"; # wrong!
487 .Ve
488 .PP
489 will not, and a subsequent pop won\*(Aqt restore the correct attributes.
490 PUSHCOLOR pushes the attributes set by its argument, which is normally a
491 string of color constants. It can\*(Aqt ask the terminal what the current
492 attributes are.
493 .SS "Supporting CLICOLOR"
494 .IX Subsection "Supporting CLICOLOR"
495 <https://bixense.com/clicolors/> proposes a standard for enabling and
496 disabling color output from console commands using two environment variables,
497 CLICOLOR and CLICOLOR_FORCE. Term::ANSIColor cannot automatically support
498 this standard, since the correct action depends on where the output is going
499 and Term::ANSIColor may be used in a context where colors should always be
500 generated even if CLICOLOR is set in the environment. But you can use the
501 supported environment variable ANSI_COLORS_DISABLED to implement CLICOLOR in
502 your own programs with code like this:
503 .PP
504 .Vb 5
505 \& if (exists($ENV{CLICOLOR}) && $ENV{CLICOLOR} == 0) {
506 \& if (!$ENV{CLICOLOR_FORCE}) {
507 \& $ENV{ANSI_COLORS_DISABLED} = 1;
508 \& }
509 \& }
510 .Ve
511 .PP
512 If you are using the constant interface, be sure to include this code before
513 you use any color constants (such as at the very top of your script), since
514 this environment variable is only honored the first time a color constant is
515 seen.
516 .PP
517 Be aware that this will export ANSI_COLORS_DISABLED to any child processes of
518 your program as well.
519 .SH DIAGNOSTICS
520 .IX Header "DIAGNOSTICS"
521 .ie n .IP "Bad color mapping %s" 4
522 .el .IP "Bad color mapping \f(CW%s\fR" 4
523 .IX Item "Bad color mapping %s"
524 (W) The specified color mapping from ANSI_COLORS_ALIASES is not valid and
525 could not be parsed. It was ignored.
526 .ie n .IP "Bad escape sequence %s" 4
527 .el .IP "Bad escape sequence \f(CW%s\fR" 4
528 .IX Item "Bad escape sequence %s"
529 (F) You passed an invalid ANSI escape sequence to \fBuncolor()\fR.
530 .IP "Bareword ""%s"" not allowed while ""strict subs"" in use" 4
531 .IX Item "Bareword ""%s"" not allowed while ""strict subs"" in use"
532 (F) You probably mistyped a constant color name such as:
533 .Sp
534 .Vb 1
535 \& $Foobar = FOOBAR . "This line should be blue\en";
536 .Ve
537 .Sp
538 or:
539 .Sp
540 .Vb 1
541 \& @Foobar = FOOBAR, "This line should be blue\en";
542 .Ve
543 .Sp
544 This will only show up under use strict (another good reason to run under
545 use strict).
546 .ie n .IP "Cannot alias standard color %s" 4
547 .el .IP "Cannot alias standard color \f(CW%s\fR" 4
548 .IX Item "Cannot alias standard color %s"
549 (F) The alias name passed to \fBcoloralias()\fR matches a standard color name.
550 Standard color names cannot be aliased.
551 .ie n .IP "Cannot alias standard color %s in %s" 4
552 .el .IP "Cannot alias standard color \f(CW%s\fR in \f(CW%s\fR" 4
553 .IX Item "Cannot alias standard color %s in %s"
554 (W) The same, but in ANSI_COLORS_ALIASES. The color mapping was ignored.
555 .ie n .IP "Invalid alias name %s" 4
556 .el .IP "Invalid alias name \f(CW%s\fR" 4
557 .IX Item "Invalid alias name %s"
558 (F) You passed an invalid alias name to \fBcoloralias()\fR. Alias names must
559 consist only of alphanumerics, \f(CW\*(C`.\*(C'\fR, \f(CW\*(C`\-\*(C'\fR, and \f(CW\*(C`_\*(C'\fR.
560 .ie n .IP "Invalid alias name %s in %s" 4
561 .el .IP "Invalid alias name \f(CW%s\fR in \f(CW%s\fR" 4
562 .IX Item "Invalid alias name %s in %s"
563 (W) You specified an invalid alias name on the left hand of the equal sign
564 in a color mapping in ANSI_COLORS_ALIASES. The color mapping was ignored.
565 .ie n .IP "Invalid attribute name %s" 4
566 .el .IP "Invalid attribute name \f(CW%s\fR" 4
567 .IX Item "Invalid attribute name %s"
568 (F) You passed an invalid attribute name to \fBcolor()\fR, \fBcolored()\fR, or
569 \&\fBcoloralias()\fR.
570 .ie n .IP "Invalid attribute name %s in %s" 4
571 .el .IP "Invalid attribute name \f(CW%s\fR in \f(CW%s\fR" 4
572 .IX Item "Invalid attribute name %s in %s"
573 (W) You specified an invalid attribute name on the right hand of the equal
574 sign in a color mapping in ANSI_COLORS_ALIASES. The color mapping was
575 ignored.
576 .IP "Name ""%s"" used only once: possible typo" 4
577 .IX Item "Name ""%s"" used only once: possible typo"
578 (W) You probably mistyped a constant color name such as:
579 .Sp
580 .Vb 1
581 \& print FOOBAR "This text is color FOOBAR\en";
582 .Ve
583 .Sp
584 It\*(Aqs probably better to always use commas after constant names in order to
585 force the next error.
586 .IP "No comma allowed after filehandle" 4
587 .IX Item "No comma allowed after filehandle"
588 (F) You probably mistyped a constant color name such as:
589 .Sp
590 .Vb 1
591 \& print FOOBAR, "This text is color FOOBAR\en";
592 .Ve
593 .Sp
594 Generating this fatal compile error is one of the main advantages of using
595 the constants interface, since you\*(Aqll immediately know if you mistype a
596 color name.
597 .ie n .IP "No name for escape sequence %s" 4
598 .el .IP "No name for escape sequence \f(CW%s\fR" 4
599 .IX Item "No name for escape sequence %s"
600 (F) The ANSI escape sequence passed to \fBuncolor()\fR contains escapes which
601 aren\*(Aqt recognized and can\*(Aqt be translated to names.
602 .SH ENVIRONMENT
603 .IX Header "ENVIRONMENT"
604 .IP ANSI_COLORS_ALIASES 4
605 .IX Item "ANSI_COLORS_ALIASES"
606 This environment variable allows the user to specify custom color aliases
607 that will be understood by \fBcolor()\fR, \fBcolored()\fR, and \fBcolorvalid()\fR. None of
608 the other functions will be affected, and no new color constants will be
609 created. The custom colors are aliases for existing color names; no new
610 escape sequences can be introduced. Only alphanumerics, \f(CW\*(C`.\*(C'\fR, \f(CW\*(C`_\*(C'\fR, and
611 \&\f(CW\*(C`\-\*(C'\fR are allowed in alias names.
612 .Sp
613 The format is:
614 .Sp
615 .Vb 1
616 \& ANSI_COLORS_ALIASES=\*(Aqnewcolor1=oldcolor1,newcolor2=oldcolor2\*(Aq
617 .Ve
618 .Sp
619 Whitespace is ignored. The alias value can be a single attribute or a
620 space\-separated list of attributes.
621 .Sp
622 For example the Solarized <https://ethanschoonover.com/solarized> colors
623 can be mapped with:
624 .Sp
625 .Vb 11
626 \& ANSI_COLORS_ALIASES=\*(Aq\e
627 \& base00=bright_yellow, on_base00=on_bright_yellow,\e
628 \& base01=bright_green, on_base01=on_bright_green, \e
629 \& base02=black, on_base02=on_black, \e
630 \& base03=bright_black, on_base03=on_bright_black, \e
631 \& base0=bright_blue, on_base0=on_bright_blue, \e
632 \& base1=bright_cyan, on_base1=on_bright_cyan, \e
633 \& base2=white, on_base2=on_white, \e
634 \& base3=bright_white, on_base3=on_bright_white, \e
635 \& orange=bright_red, on_orange=on_bright_red, \e
636 \& violet=bright_magenta,on_violet=on_bright_magenta\*(Aq
637 .Ve
638 .Sp
639 This environment variable is read and applied when the Term::ANSIColor
640 module is loaded and is then subsequently ignored. Changes to
641 ANSI_COLORS_ALIASES after the module is loaded will have no effect. See
642 \&\fBcoloralias()\fR for an equivalent facility that can be used at runtime.
643 .IP ANSI_COLORS_DISABLED 4
644 .IX Item "ANSI_COLORS_DISABLED"
645 If this environment variable is set to a true value, all of the functions
646 defined by this module (\fBcolor()\fR, \fBcolored()\fR, and all of the constants) will not
647 output any escape sequences and instead will just return the empty string or
648 pass through the original text as appropriate. This is intended to support
649 easy use of scripts using this module on platforms that don\*(Aqt support ANSI
650 escape sequences.
651 .IP NO_COLOR 4
652 .IX Item "NO_COLOR"
653 If this environment variable is set to any value, it suppresses generation of
654 escape sequences the same as if ANSI_COLORS_DISABLED is set to a true value.
655 This implements the <https://no\-color.org/> informal standard. Programs that
656 want to enable color despite NO_COLOR being set will need to unset that
657 environment variable before any constant or function provided by this module
658 is used.
659 .SH COMPATIBILITY
660 .IX Header "COMPATIBILITY"
661 Term::ANSIColor was first included with Perl in Perl 5.6.0.
662 .PP
663 The \fBuncolor()\fR function and support for ANSI_COLORS_DISABLED were added in
664 Term::ANSIColor 1.04, included in Perl 5.8.0.
665 .PP
666 Support for dark was added in Term::ANSIColor 1.08, included in Perl
667 5.8.4.
668 .PP
669 The color stack, including the \f(CW\*(C`:pushpop\*(C'\fR import tag, PUSHCOLOR,
670 POPCOLOR, LOCALCOLOR, and the \f(CW$Term::ANSIColor::AUTOLOCAL\fR variable, was
671 added in Term::ANSIColor 2.00, included in Perl 5.10.1.
672 .PP
673 \&\fBcolorstrip()\fR was added in Term::ANSIColor 2.01 and \fBcolorvalid()\fR was added
674 in Term::ANSIColor 2.02, both included in Perl 5.11.0.
675 .PP
676 Support for colors 8 through 15 (the \f(CW\*(C`bright_\*(C'\fR variants) was added in
677 Term::ANSIColor 3.00, included in Perl 5.13.3.
678 .PP
679 Support for italic was added in Term::ANSIColor 3.02, included in Perl
680 5.17.1.
681 .PP
682 Support for colors 16 through 256 (the \f(CW\*(C`ansi\*(C'\fR, \f(CW\*(C`rgb\*(C'\fR, and \f(CW\*(C`grey\*(C'\fR
683 colors), the \f(CW\*(C`:constants256\*(C'\fR import tag, the \fBcoloralias()\fR function, and
684 support for the ANSI_COLORS_ALIASES environment variable were added in
685 Term::ANSIColor 4.00, included in Perl 5.17.8.
686 .PP
687 \&\f(CW$Term::ANSIColor::AUTOLOCAL\fR was changed to take precedence over
688 \&\f(CW$Term::ANSIColor::AUTORESET\fR, rather than the other way around, in
689 Term::ANSIColor 4.00, included in Perl 5.17.8.
690 .PP
691 \&\f(CW\*(C`ansi16\*(C'\fR through \f(CW\*(C`ansi255\*(C'\fR, as aliases for the \f(CW\*(C`rgb\*(C'\fR and \f(CW\*(C`grey\*(C'\fR colors,
692 and the corresponding \f(CW\*(C`on_ansi\*(C'\fR names and \f(CW\*(C`ANSI\*(C'\fR and \f(CW\*(C`ON_ANSI\*(C'\fR constants
693 were added in Term::ANSIColor 4.06, included in Perl 5.25.7.
694 .PP
695 Support for true color (the \f(CW\*(C`rNNNgNNNbNNN\*(C'\fR and \f(CW\*(C`on_rNNNgNNNbNNN\*(C'\fR
696 attributes), defining aliases in terms of other aliases, and aliases mapping
697 to multiple attributes instead of only a single attribute was added in
698 Term::ANSIColor 5.00.
699 .PP
700 Support for NO_COLOR was added in Term::ANSIColor 5.01.
701 .SH RESTRICTIONS
702 .IX Header "RESTRICTIONS"
703 Both \fBcolored()\fR and many uses of the color constants will add the reset escape
704 sequence after a newline. If a program mixes colored output to standard
705 output with output to standard error, this can result in the standard error
706 text having the wrong color because the reset escape sequence hasn\*(Aqt yet been
707 flushed to the display (since standard output to a terminal is line\-buffered
708 by default). To avoid this, either set \fBautoflush()\fR on STDOUT or set
709 \&\f(CW$Term::ANSIColor::EACHLINE\fR to \f(CW"\en"\fR.
710 .PP
711 It would be nice if one could leave off the commas around the constants
712 entirely and just say:
713 .PP
714 .Vb 1
715 \& print BOLD BLUE ON_WHITE "Text\en" RESET;
716 .Ve
717 .PP
718 but the syntax of Perl doesn\*(Aqt allow this. You need a comma after the
719 string. (Of course, you may consider it a bug that commas between all the
720 constants aren\*(Aqt required, in which case you may feel free to insert
721 commas unless you\*(Aqre using \f(CW$Term::ANSIColor::AUTORESET\fR or
722 PUSHCOLOR/POPCOLOR.)
723 .PP
724 For easier debugging, you may prefer to always use the commas when not
725 setting \f(CW$Term::ANSIColor::AUTORESET\fR or PUSHCOLOR/POPCOLOR so that you\*(Aqll
726 get a fatal compile error rather than a warning.
727 .PP
728 It\*(Aqs not possible to use this module to embed formatting and color
729 attributes using Perl formats. They replace the escape character with a
730 space (as documented in \fBperlform\fR\|(1)), resulting in garbled output from
731 the unrecognized attribute. Even if there were a way around that problem,
732 the format doesn\*(Aqt know that the non\-printing escape sequence is
733 zero\-length and would incorrectly format the output. For formatted output
734 using color or other attributes, either use \fBsprintf()\fR instead or use
735 \&\fBformline()\fR and then add the color or other attributes after formatting and
736 before output.
737 .SH NOTES
738 .IX Header "NOTES"
739 The codes generated by this module are standard terminal control codes,
740 complying with ECMA\-048 and ISO 6429 (generally referred to as "ANSI
741 color" for the color codes). The non\-color control codes (bold, dark,
742 italic, underline, and reverse) are part of the earlier ANSI X3.64
743 standard for control sequences for video terminals and peripherals.
744 .PP
745 Note that not all displays are ISO 6429\-compliant, or even X3.64\-compliant
746 (or are even attempting to be so). This module will not work as expected
747 on displays that do not honor these escape sequences, such as cmd.exe,
748 4nt.exe, and command.com under either Windows NT or Windows 2000. They
749 may just be ignored, or they may display as an ESC character followed by
750 some apparent garbage.
751 .PP
752 Jean Delvare provided the following table of different common terminal
753 emulators and their support for the various attributes and others have
754 helped me flesh it out:
755 .PP
756 .Vb 12
757 \& clear bold faint under blink reverse conceal
758 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
759 \& xterm yes yes no yes yes yes yes
760 \& linux yes yes yes bold yes yes no
761 \& rxvt yes yes no yes bold/black yes no
762 \& dtterm yes yes yes yes reverse yes yes
763 \& teraterm yes reverse no yes rev/red yes no
764 \& aixterm kinda normal no yes no yes yes
765 \& PuTTY yes color no yes no yes no
766 \& Windows yes no no no no yes no
767 \& Cygwin SSH yes yes no color color color yes
768 \& Terminal.app yes yes no yes yes yes yes
769 .Ve
770 .PP
771 Windows is Windows telnet, Cygwin SSH is the OpenSSH implementation under
772 Cygwin on Windows NT, and Mac Terminal is the Terminal application in Mac
773 OS X. Where the entry is other than yes or no, that emulator displays the
774 given attribute as something else instead. Note that on an aixterm, clear
775 doesn\*(Aqt reset colors; you have to explicitly set the colors back to what
776 you want. More entries in this table are welcome.
777 .PP
778 Support for code 3 (italic) is rare and therefore not mentioned in that
779 table. It is not believed to be fully supported by any of the terminals
780 listed, although it\*(Aqs displayed as green in the Linux console, but it is
781 reportedly supported by urxvt.
782 .PP
783 Note that codes 6 (rapid blink) and 9 (strike\-through) are specified in ANSI
784 X3.64 and ECMA\-048 but are not commonly supported by most displays and
785 emulators and therefore aren\*(Aqt supported by this module. ECMA\-048 also
786 specifies a large number of other attributes, including a sequence of
787 attributes for font changes, Fraktur characters, double\-underlining, framing,
788 circling, and overlining. As none of these attributes are widely supported or
789 useful, they also aren\*(Aqt currently supported by this module.
790 .PP
791 Most modern X terminal emulators support 256 colors. Known to not support
792 those colors are aterm, rxvt, Terminal.app, and TTY/VC.
793 .PP
794 For information on true color support in various terminal emulators, see
795 True Colour support <https://gist.github.com/XVilka/8346728>.
796 .SH AUTHORS
797 .IX Header "AUTHORS"
798 Original idea (using constants) by Zenin, reimplemented using subs by Russ
799 Allbery <[email protected]>, and then combined with the original idea by
800 Russ with input from Zenin. 256\-color support is based on work by Kurt
801 Starsinic. Russ Allbery now maintains this module.
802 .PP
803 PUSHCOLOR, POPCOLOR, and LOCALCOLOR were contributed by openmethods.com
804 voice solutions.
805 .SH "COPYRIGHT AND LICENSE"
806 .IX Header "COPYRIGHT AND LICENSE"
807 Copyright 1996\-1998, 2000\-2002, 2005\-2006, 2008\-2018, 2020 Russ Allbery
808 <[email protected]>
809 .PP
810 Copyright 1996 Zenin
811 .PP
812 Copyright 2012 Kurt Starsinic <[email protected]>
813 .PP
814 This program is free software; you may redistribute it and/or modify it
815 under the same terms as Perl itself.
816 .SH "SEE ALSO"
817 .IX Header "SEE ALSO"
818 The CPAN module Term::ExtendedColor provides a different and more
819 comprehensive interface for 256\-color emulators that may be more
820 convenient. The CPAN module Win32::Console::ANSI provides ANSI color
821 (and other escape sequence) support in the Win32 Console environment.
822 The CPAN module Term::Chrome provides a different interface using
823 objects and operator overloading.
824 .PP
825 ECMA\-048 is available on\-line (at least at the time of this writing) at
826 <https://www.ecma\-international.org/publications/standards/Ecma\-048.htm>.
827 .PP
828 ISO 6429 is available from ISO for a charge; the author of this module
829 does not own a copy of it. Since the source material for ISO 6429 was
830 ECMA\-048 and the latter is available for free, there seems little reason
831 to obtain the ISO standard.
832 .PP
833 The 256\-color control sequences are documented at
834 <https://invisible\-island.net/xterm/ctlseqs/ctlseqs.html> (search for
835 256\-color).
836 .PP
837 Information about true color support in various terminal emulators and test
838 programs you can run to check the true color support in your terminal emulator
839 are available at <https://gist.github.com/XVilka/8346728>.
840 .PP
841 CLICOLORS <https://bixense.com/clicolors/> and
842 NO_COLOR <https://no-color.org/> are useful standards to be aware of, and
843 ideally follow, for any application using color. Term::ANSIColor complies
844 with the latter.
845 .PP
846 The current version of this module is always available from its web site
847 at <https://www.eyrie.org/~eagle/software/ansicolor/>. It is also part
848 of the Perl core distribution as of 5.6.0.