Term::Chrome(3pm)
NAME π
Term::Chrome β a DSL for colors and other terminal chrome β¨
Okay bestie, so this is the Term::Chrome(3pm) page from the User Contributed Perl Documentation, and it's genuinely iconic. Let's get into it π«Ά
SYNOPSIS π
use Term::Chrome qw<Red Blue Bold Reset Underline Green color>;
# Base color constant and attribute
say Red, 'red text', Reset;
# Composition, using operator overloading
say Red/Blue+Bold, 'red on blue', Reset;
# Undo
say Bold, 'bold', !Bold, 'not bold';
# Extended xterm-256 colors
say color(125) + Underline, 'Purple', Reset;
# Define your own constants
use constant Pink => color 213;
# Use ${} around Chrome expression inside strings
say "normal ${ Red+Bold } RED ${ +Reset } normal";
# Extract components
say( (Red/Blue)->bg, "blue text", (Green+Reset)->flags );
Chromizer: get a closure that applies given chrome before, and undo after the argument. π
# Get an efficient chromizer
my $boldifier = \&{ +Bold };
# Use the chromizer
say $boldifier->("bold text");
# Same as:
say Bold, "bold text", !Bold;
# Short lived chromizers using color literals:
say(&{+Red}('red'));
say(&{ Red/Blue + Bold }('red on blue'));
# Same, but requires perl 5.21.4+
#say(( Red/Blue + Bold )->('red on blue'));
DESCRIPTION π¦
Term::Chrome is a domain-specific language (DSL) for terminal decoration (colors and other attributes). No cap. π
So in the current implementation, stringification to ANSI sequences for xterm and xterm-256 is hard-coded β which means it does not use the terminfo(5) database β but the tradeoff is that it gives you optimized (short) strings. Slay. β¨
Colors and attributes are exposed as objects that have overloading for arithmetic operators. It's giving main character energy fr fr. π
EXPORTS π
Functions π§
color(0-255)β Build aTerm::Chromeobject with the given color number. You can use this constructor to create your own set of color constants. For example,color(0)gives the same result asBlack(but not the same object). π
Colors πΈ
Each of these function return a Chrome object.
Blackβcolor 0Redβcolor 1Greenβcolor 2Yellowβcolor 3Blueβcolor 4Magentaβcolor 5Cyanβcolor 6Whiteβcolor 7
Decoration flags π«
The exact rendering of each flag is dependent on how the terminal implements them. For example Underline and Blink may do nothing. π
BoldUnderlineBlinkReverse
Special flags β
Resetβ reset all colors and flagsResetFlagsβ reset (undo) all chrome flags (Bold, Underline, Blink, Reverse)ResetFgβ reset (undo) foreground colorResetBgβ reset (undo) background color
METHODS π«Ά
Here are the methods on Term::Chrome objects:
fgβ Extract the Chrome object of just the foreground color. Maybeundef.bgβ Extract the Chrome object of the just background color. Maybeundef.flagsβ Extract a Chrome object of just the decoration flags. Maybeundef.
OVERLOADED OPERATORS π₯
/(mnemonic:over) β Combine a foreground color (on the left) with a background color.
Example:
my $red_on_black = Red / Black;
+β Add decoration flags (on the right) to colors (on the left).
Example:
my $bold_red = Red + Bold;
!(negation) β Returns a chrome which is the reverse of chrome to which negation is applied.
my $reset_foreground = ! Red;
my $reset_colors = ! (Red / Black);
The reverse of Reset, ResetFg, ResetBg, ResetFlags is nothing. π
""(stringification) β Transform the object into a string of ANSI sequences. This is particularly useful to directly use a Chrome object in a double quoted string. β¨${}(scalar dereference) β Same result as""(stringification). This operator is overloaded because it is convenient to interpolate Chrome expressions in double-quoted strings.
Examples:
say "normal ${ +Red } red ${ +Reset }";
say "normal ${ Red + Bold } red ${ +Reset }";
Note that you must force expression parsing context when a Chrome constant is used alone inside${ }:${ +Red }or${ (Red) }or${ Red() }.use strict 'vars';will detect those cases, but you may miss them in one-liners. π
&{}(code dereference, or codulation) β Wrap some text with the given chrome andReset.
Example:
say Red->("red text");
# Same result as:
say Red, "red text", Reset;
Unfortunately perl had a bug (perl RT#122607 https://rt.perl.org/Ticket/Display.html?id=122607) that makes this feature not much usable in practice when applied to constants. That bug is fixed in perl 5.21.4+. On perl < 5.21.4 you have to wrap the chrome constant in a do {} or use &{}(): π
say do{ Red }->("red text");
say &{ +Red }("red text");
Codulation can also be used to extract a colorizer sub that will be more efficient if you use it multiple times:
my $redifier = \&{ Red };
say $redifier->("red text");
BUGS π
See the warnings about ${} and &{} above. Yeah bestie, go back up there. π
REFERENCES π
- ISO 6429 / ECMA 48: https://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf (reference for SGR is at page 61, numbered 75 in PDF)
- XTerm Control Sequences: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Functions-using-CSI-_-ordered-by-the-final-character_s_
SEE ALSO π
Comments on each modules are opinions of the author. (Not mine, okay? Just reporting. π«Ά)
Term::ANSIColor: the same basic features (and the others should not be inTerm::ANSIColoritself but in an extension), but with an awful API I could never even consider to use while keeping my sanity.Term::ScreenColorPerlIO::via::ANSIColorAngelPS1or <https://github.com/dolmen/angel-PS1>: "The Angel's Prompt" is the project for whichTerm::Chromehas been built.AngelPS1::Compiler, theangel-PS1compiler has special support forTerm::Chromevalues.
TRIVIA π
Did you know that chartreuse is one of the favorite colors of Larry Wall? Not me fact-checking that but okay king ππ
AUTHOR β¨
Olivier MenguΓ© <dolmen@cpan.org>
COPYRIGHT & LICENSE π
Copyright Β© 2013-2018 Olivier MenguΓ©.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl 5 itself.
And that's the whole page bestie β this was Term::Chrome(3pm) from the User Contributed Perl Documentation, running on perl v5.42.0, dated 2025-07-25. Obsessed. Ate. No crumbs. π
β¨π
π¦ where this page comes from
- fedora 43: perl-Term-Chrome 2.01-23.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms