Skip to content

ddnexus/colorer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

colorer

Easy ANSI code coloring for strings in applications.

Synopsis

require 'colorer'
Colorer.def_basic_styles
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 ====')

Colorer.def_strip_ansi
plain_text = colored_string.strip_ansi

Feedback!!!

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

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

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

+ Difference with the Dye gem

The Colorer gem is meant for using in your own application, it’s a cool way to style string, but it is not the perfect fit for libraries. Indeed it defines an instance method for each style, and that might clash if another library defines the same style. That’s not a problem for applications.

The Dye gem instead does not have the same problem, although its syntax is not so cool as the Colorer’s one.

Basic Styles

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

# all basic styles
Colorer.def_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 ]

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

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 en.wikipedia.org/wiki/ANSI_colors 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_STRICT_ANSI environment variable for a system wide setting.

Color

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-2012 Domizio Demichelis. See LICENSE for details.

About

Easy ANSI code coloring for strings in applications

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages