Col – high-level console color formatting
$ [sudo] gem install col
Source-code access is via Github. See Project details.
Col offers high-level access to the ANSI codes used to create colorful output on the console. It is optimised for dealing with multiple strings at once, applying different color formatting to each string. It does not add any methods to the String class.
For simple console-coloring requirements, there is little or nothing to be gained from using Col instead of Term::ANSIColor.
If formatting a single string, you can send the method names directly, or use their abbreviation if applicable:
If formatting multiple strings, you are limited to using the fmt
method.
Abbreviations are available if the format you wish to use comprises:
See Abbreviations below for details.
Col(...)
and Col[...]
create a Col
object, whose only interesting methods are
fmt
(to apply formatting) and to_s
. Any other method will be interpreted as
a format specifier.
Col#fmt
returns a String
:
Directly-applied formatting methods return a Col
object:
Because Col#to_s
is implemented, you can use puts
directly on a Col
object:
Directly-applied abbreviated formatting methods return a String
:
Incorrect use of Col results in a Col::Error
being raised:
Here are some illustrative examples of abbreviations you can use with col
.
Code Effect(s) applied
--------------- -------------------
Col["..."].g green
Col["..."].gb green bold
Col["..."]._b bold
Col["..."].gbow green bold on_white
Col["..."].g_ow green on_white
Col["..."].__ow on_white
These examples show that the abbreviations are positional. If you only want to
specify on_white
, you must use underscores for the color and style properties.
Using these abbreviations, you can apply at most one color, at most one style, and at most one background color. These are listed in full here:
Note the following solutions to abbreviation clashes:
b
for blue, B
for blacku
for underline, U
for underscoreb
for bold; k
for blink.Col is designed to make colorising a string (or collection of strings) easy, and is optimised for the common case of applying a single color, a single style and a background color. If you need to apply more than one style to a single string, you can send them all as methods:
Or you can pass all of them, in full, to the fmt
method.
If you are using Col
to format a number of strings, fmt
is your only option.
Naturally, the need for such usage should be extremely rare!
Assuming your formatting needs are straightforward, the most convenient way to format multiple strings is with a comma-separated format specification.
For example:
An alternative is to provide a list of strings or symbols:
An alternative way to format multiple strings is to use Col.inline
.
For example:
Col.uncolored
or Col.plain
will remove any ANSI color codes from a string.
People using a native Windows build of Ruby in the Windows console should include the following code in their program:
This does not apply to Cygwin users.
Col uses Term::ANSIColor to access ANSI codes, and offers access to all of
its codes/features except reset
.
The author of this library never applies anything other than a foreground color and ‘bold’, so everything else is tested only in unit tests, not in practice. Furthermore, the author has no knowledge of terminal issues and is just happy to see a few colors appear in his Ruby 1.8 and 1.9 (Cygwin) programs – no other environment has been tested!
I’ve used Term::ANSIColor many times and never sought anything more, but while developing Whitestone, which makes much use of console color, I wanted an easier way to apply color codes to groups of strings. Additionally, being a unit testing library, I didn’t want to add methods to the String class, the way I normally do when using Term::ANSIColor.
Florian Flank for Term::ANSIColor, which I’ve used heavily over the years.
gsinclair
; mail server: gmail.com
)See History.txt for more details.
Version 1.0.0 was released with no plans for future releases. Version 1.0.1 fixed a bug (18 months later), and still there are no plans for further functionality.
One possible area of enhancement is to provide a way of disabling colored output when outputing to a pipe. (I believe Term::ANSIColor may already do that.) (Sometimes, however, colored output in a pipe is desirable, viz. git.)