Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format text for STDOUT in Ruby

I am writing 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?

like image 285
Julien Genestoux Avatar asked Nov 30 '22 20:11

Julien Genestoux


1 Answers

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.

like image 115
Pesto Avatar answered Dec 18 '22 11:12

Pesto