Easy ANSI code coloring for strings.


require 'colorer'
Colorer.def_custom_styles :errorize => [ :red, :bold, :underline ],
                          :mysgr => [ :red, 8 ]

"a red bold underlined text on white background".red.bold.underline.onwhite
"an error string".errorize
"my native (Select Graphic Rendition) string".mysgr

'    reversed    '.reversed.or('==== reversed ====')

plain_text = colored_string.strip_ansi


This is feedback-driven software. Just send me a line about you and/or what you think about IRT: that will be a wonderful contribution that will help me to keep improving (and documenting) this software.

My email address is ddnexus at … waiting for your. Ciao.


  • Does not pollute String of unwanted methods

  • Allows you to define basic styles with one line of code

  • Allows you to easily add your own custom styles

  • Allows extended (Select Graphic Rendition) parameters

Basic Styles

You can define the basic styles for any string by using the def_basic_styles method:

# all basic styles
"a string".green.bold.reversed.underline...

# a few basic styles
Colorer.define_styles [:bold, :reversed]
"a string".bold.reversed

# one basic style
Colorer.define_styles :red
"a string".red

Basic Styles List

  • clear

  • bold

  • underline

  • blinking

  • reversed

  • black

  • red

  • green

  • yellow

  • blue

  • magenta

  • cyan

  • white

  • onblack

  • onred

  • ongreen

  • onyellow

  • onblue

  • onmagenta

  • oncyan

  • onwhite

Custom Styles

You can define your own custom styles by aggregating any basic styles names:

Colorer.def_custom_styles :errorize => [ :red, :bold, :underline ],
                          :okize => [ :green, :bold ],
                          :crazyize => [ :magenta, :onyellow, :bold, :underline ]

# same as
# same as
# same as

SGR Styles

You can also add native SGR (Select Graphic Rendition) parameters (0..109) to any style:

Colorer.def_custom_styles :mysgr => [ :red, 8 ]

See for a complete list

Strict ANSI

Some terminals don't parse composite SGR styles correctly, and need separate SGR for each.

puts "\e[7;31;46mSTRING\e[0m"         # strict_ansi == true (may be difficult to parse)
puts "\e[7m\e[31m\e[46mSTRING\e[0m"   # strict_ansi == false

On the other way most of the terminals that parse them correctly can parse also separate SGRs, so Colorer will output non strict ansi by default. If you want to have strict ansi you can do:

Colorer.strict_ansi = true

or you can set the COLORER_ANSI_STRICT environment variable for a system wide setting.


The color is true by defealut on a non-dumb tty terminal, anyway you can force it by explicitly setting it:

Colorer.color?         #=> true/false by default depending on your terminal
Colorer.color = true   # force true
Colorer.color?         #=> true
Colorer.color = false  # force false
Colorer.color?         #=> false


Copyright © 2010-2011 Domizio Demichelis. See LICENSE for details.