NAME

Term::Chrome - DSL for colors and other terminal chrome

SYNOPSIS

use Term::Chrome qw<Red Blue Bold Reset Underline Green color>;
use feature qw<say>; # Just for this example

# Base color constant and attribute
say Red, 'red text', Reset;

# Composition, using operator overloading
say Red/Blue+Bold, 'red on blue', Reset;

# 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 );

# Get an efficient chromizer sub (applies given chrome before, and
# Reset after the argument)
my $boldifier = \&{ +Bold };
# Use the chromizer
say $boldifier->("bold text");

DESCRIPTION

Term::Chrome is a domain-specific language (DSL) for terminal decoration (colors and other attributes).

In the current implementation stringification to ANSI sequences for xterm and xterm-256 is hard-coded (which means it doesn't use the terminfo(5) database), but this gives optimized (short) strings.

Colors and attributes are exposed as objects that have overloading for arithmetic operators.

EXPORTS

Functions

color(0-255)

Build a Term::Chrome object 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 as Black (but not the same object).

Colors

Each of these function return a Chrome object.

  • Black: color 0

  • Red: color 1

  • Green: color 2

  • Yellow: color 3

  • Blue: color 4

  • Magenta: color 5

  • Cyan: color 6

  • White: 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.

  • Bold

  • Underline

  • Blink

  • Reverse

Special flags

  • Reset : reset all colors and flags

METHODS

Here are the methods on Term::Chrome objects:

fg

Extract the Chrome object of just the foreground color. Maybe undef.

bg

Extract the Chrome object of the just background color. Maybe undef.

flags

Extract a Chrome object of just the decoration flags. Maybe undef.

OVERLOADED OPERATORS

/ (mnemonic: "over")

Conmbine a foreground color (on the left) with a background color.

+

Add decoration flags (on the right) to colors (on the left).

"" (stringification)

Transform the object into a sting 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.

Example:

say "normal ${ Red } red ${ Reset }";
&{} (code dereference, or "codulation")

Wrap some text with the given chrome and Reset.

Example:

say Red->("red text");
# Same result as:
say Red, "red text", Reset;

Unfortunately perl had a bug (perl RT#122607) that makes this feature not much usable in practice when applied to constants. That bug is fixed in perl 5.21.4+.

This can also be used to extract a colorizer sub that will be more efficient if you reuse it:

my $redifier = \&{ Red };
say $redifier->("red text");

BUGS

See the warning about &{} above.

SEE ALSO

Comments on each modules are opinions of the author.

TRIVIA

Did you know that chartreuse is one of the favorite color of Larry Wall?

AUTHOR

Olivier Mengué, mailto:dolmen@cpan.org

COPYRIGHT & LICENSE

Copyright © 2013-2014 Olivier Mengué.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl 5 itself.