colorer

Easy ANSI code coloring for strings.

Synopsis

require 'colorer'
Colorer.define_styles :basic => true,
                      :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

Features

  • 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 reserved name :basic:

# all basic styles
Colorer.define_styles :basic => true
"a string".green.bold.reversed.underline...

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

# one basic style
Colorer.define_styles :basic => :red
"a string".red
  • 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:

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

error_string.errorize
# same as
error_string.red.bold.underline
ok_string.okeyze
# same as
ok_string.green.bold
crazy_string.crazyize
# same as
crazy_string.magenta.onyellow.bold.underline

Notice: you cannot use :basic as a custom name, since it is the reserved name used to define the basic styles.

SGR Styles

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

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

See en.wikipedia.org/wiki/ANSI_colors for a complete list

Copyright © 2010 Domizio Demichelis. See LICENSE for details.