views:

496

answers:

2

Hello,

I am writting a small Ruby script that will run in a CLI.

To improve the interface, I need to would love to add color/boldness to some elements that I output.

Is that doable? If so (and I am almost sure this is), how?

Thanks a bunch

+2  A: 

On many terminals (but not Windows), you can use an a sequence like this: "\e[#{code}m", where the codes are based on these tables. The codes must be separated by a semicolon if using more than one. The major codes are:

Intensity:

1  Bold Intensity
4  Underline
5  Slow blink
6  Fast blink
22 Normal Intensity

Color:

Foreground 3X
Background 4X

Where X is:
-----------
0 Black
1 Red
2 Green
3 Yellow
4 Blue
5 Magenta
6 Cyan
7 White

So, for example, for slowly blinking, bold green text on a blue background, you would use "\e[5;1;32;44mWOW!\e[0m". The \e[0m resets everything to the terminal default.

Pesto
+3  A: 

There is a gem called rainbow that makes it really easy to style your terminal output.

sudo gem install rainbow
http://github.com/sickill/rainbow/tree/master

After installing it you can do stuff like:

puts 'some text'.underline
heycarsten