When using Ruby to output text to the console, Good coloring is necessary. The following describes how to output the colored text to the OS X terminal. In fact, it should also apply in Linux.
Originally, I only needed to post the link to the blog post. Unfortunately, the blog post is on Blogspot and is being blocked for a Great Wall. For convenience, I can only post the full text. Link to the original article:
Http://craiccomputing.blogspot.com/2010/08/printing-colored-text-in-unix-terminals.html
Printing colored text in UNIX terminals from Ruby outputting text in color in a Unix console/xterminal is kind of old school-some might say obsolete-but once in a while it is very useful. one example is the red/green coloring of test results. you don't get a lot of options but enough to highlight relevant text.
The cryptic escape codes are explained in this Wiki page.
This ruby script outputs fore and background colors and a couple of the other useful modifiers.
#! /Usr/bin/ruby
puts "\ 033 [1 mforeground colors... \ 033 [0m \ n "
puts" \ 033 [30 mblack (30) \ 033 [0m \ n "
puts" \ 033 [31 mred (31) \ 033 [0m \ n "
puts" \ 033 [32 mgreen (32) \ 033 [0m \ n "
puts" \ 033 [33 myellow (33) \ 033 [0m \ n "
puts" \ 033 [34 mblue (34) \ 033 [0m \ n "
puts" \ 033 [35 mmagenta (35) \ 033 [0m \ n "
puts" \ 033 [36 mcyan (36) \ 033 [0m \ n "
puts" \ 033 [37 mwhite (37) \ 033 [0m \ n "
puts''
puts "\ 033 [1 mbackground colors... \ 033 [0m \ n "
puts" \ 033 [40m \ 033 [37 mblack (40 ), white text \ 033 [0m \ n "
puts" \ 033 [41 mred (41) \ 033 [0m \ n "
puts" \ 033 [42 mgreen (42) \ 033 [0m \ n "
puts" \ 033 [43 myellow (43) \ 033 [0m \ n "
puts" \ 033 [44 mblue (44) \ 033 [0m \ n "
puts" \ 033 [45 mmagenta (45) \ 033 [0m \ n "
puts" \ 033 [46 mcyan (46) \ 033 [0m \ n "
puts" \ 033 [47 mwhite (47) \ 033 [0m \ n "
puts''
puts "\ 033 [1mmodifiers... \ 033 [0m \ n "
puts" RESET (0) "
puts" \ 033 [1 mbold (1) \ 033 [0m \ n "
puts" \ 033 [4 munderlined (4) \ 033 [0m \ n "
Note that the exact colors will vary between different terminals. So try it and see.
The script will show you enough to create your own colored terminal output. But what if you sometimes want your output to go to a terminal (TTY) and other times to a file? In the latter, all the escape codes will give you garbage characters when you view the file. the trick here is to test whether our not your script is writing to a tty with 'stdout. tty? '. If so then add the escape codes, if not then leave them out.