Colorizing console Ruby-script output

Mar 23
2007 18:28 (Development, Ruby on Rails) · Русский (13,248 views)

Very often I have to implement console scripts (because of my laziness, for boring processes optimization). Many of them write some information to the output, show process status or display results of work. Anyway, it’s very wearisome action to read script output, and I want to highlight most important things: errors in red, successfully finished steps in green color, etc. And it is a case when ANSI escape sequences could help. They are supported by the most terminals, including VT100 (btw, Windows NT family console does not support it, but I will back to this issue later).

For the beginning, let’s examine ANSI escape sequence structure. It starts with ESC symbol (ASCII-code 27), following by left square bracket [. There are one or more numbers separated by semicolon ; with a letter at the end appear after it.

I will not describe all possible codes, anybody who wishes could find them in Wikipedia. The sequence with m letter at the end is used to change foreground and background colors. In general situation it looks like: ESC[31m, where 31 sets red color as foreground. Here the table with codes which supported by most terminals:

Code Effect
0 Turn off all attributes
1 Set bright mode
4 Set underline mode
5 Set blink mode
7 Exchange foreground and background colors
8 Hide text (foreground color would be the same as background)
30 Black text
31 Red text
32 Green text
33 Yellow text
34 Blue text
35 Magenta text
36 Cyan text
37 White text
39 Default text color
40 Black background
41 Red background
42 Green background
43 Yellow background
44 Blue background
45 Magenta background
46 Cyan background
47 White background
49 Default background color

As you could see from the table, you are able to set foreground and background colors separately, also you could combine them into one sequence (for example, ESC[1;33;44m - bright yellow text on blue background).

Attention: Don’t forget to turn off all attributes before exit, otherwise all following text would be displayed with your attributes.

That’s enough of the theory, let’s examine example:

# Actual work
puts "Importing categories [ \e[32mDONE\e[0m ]"
# Actual work
puts "Importing tags       [\e[31mFAILED\e[0m]"

As the result you will see something like following:

VT100 Example 1

All my life I used codes just like shown in the previous example, but not so long ago I found simple helpers when delving in the RSpec sources:

def colorize(text, color_code)
  "#{color_code}#{text}\e[0m"
end

def red(text); colorize(text, "\e[31m"); end
def green(text); colorize(text, "\e[32m"); end

# Actual work
puts 'Importing categories [ ' + green('DONE') + ' ]'
# Actual work
puts 'Importing tags       [' + red('FAILED') + ']'

It’s a good idea, and now I’m using it. And recommend it to you :-)

Now about sorrowful things. Windows XP (and as far as I remember, Windows 2000 too) does not support ANSI escape sequences. If you are love perversions, look at the Command Interpreter Ansi Support article. Others could stay here and look, how to solve problem using Ruby facilities.

You should install win32console first:

gem install win32console

Now add following lines at the beginning of your script (and again, I found them in RSpec):

begin
  require 'Win32/Console/ANSI' if PLATFORM =~ /win32/
rescue LoadError
  raise 'You must gem install win32console to use color on Windows'
end

Script output will be colorized both on windows and Unix systems.

And in the end I will show full table of different codes, which you could use in your scripts:

VT100 Terminal

It has been obtained using following script:

#!/usr/bin/ruby

[0, 1, 4, 5, 7].each do |attr|
  puts '----------------------------------------------------------------'
  puts "ESC[#{attr};Foreground;Background"
  30.upto(37) do |fg|
    40.upto(47) do |bg|
      print "\033[#{attr};#{fg};#{bg}m #{fg};#{bg}  "
    end
  puts "\033[0m"
  end
end

9 Responses to 'Colorizing console Ruby-script output'

Subscribe to comments with RSS or TrackBack to 'Colorizing console Ruby-script output'.

1
shaliko
said on 2007-03-24 at 3.24 pm

Большое спасибо за статью!

Непременно воспользуюсь всем выше перечисленным..

2
Shatyorkin
said on 2007-03-27 at 11.58 am

Классная штука - я это для тестов приспособил.

3
said on 2007-07-12 at 12.13 am

Thanks for the code, it helped a lot for a console app that I am working on.

4
said on 2007-07-25 at 12.17 am

This is a great article. If you continue to write interesting things like this I will have to subscribe to your feed!

5
said on 2007-10-12 at 9.57 am

спасибо за статью. я как–бы думал, што у руби может быть свой стиль колоризации, но оказывается это те же саммые анси .)

6
Artem Vasiliev
said on 2007-11-27 at 6.54 pm

Круто, спасибо!

Прикрутил это у себя и к серверной консоли, и к autotest..

Правда, не без помощи такой-то матери - простое добавление в environment.rb для autotest не достаточно, пришлось продублировать в .autotest

7
pmil
said on 2008-02-27 at 11.31 pm

I have similar problem but I wanted to see Rails log colors in win console.
A little addition.
1. create file console.rb

require 'rubygems'
require 'win32console'
include Win32::Console::ANSI
include Term::ANSIColor

puts bold
8
said on 2008-05-06 at 7.40 am

[...] Colorizing console Ruby-script output | Dmytro Shteflyuk’s Home Colorizing console Ruby-script output (tags: Ruby ansi console code) [...]

9
said on 2008-07-15 at 5.36 am

Я так делаю обычно:

class String

    def red; colorize(self, "\e[1m\e[31m"); end
    def green; colorize(self, "\e[1m\e[32m"); end
    def dark_green; colorize(self, "\e[32m"); end
    def yellow; colorize(self, "\e[1m\e[33m"); end
    def blue; colorize(self, "\e[1m\e[34m"); end
    def dark_blue; colorize(self, "\e[34m"); end
    def pur; colorize(self, "\e[1m\e[35m"); end
    def colorize(text, color_code)  "#{color_code}#{text}\e[0m" end
end

И при использовании

puts "Hello".red

Post a comment

You can use simple HTML-formatting tags (like <a>, <ul> and others). To format your code sample use <code lang="php">$a = "hello";</code> (allowed languages are ruby, php, yaml, html, csharp, javascript). Also you could use <code>$a = "hello";</code> and its syntax would not be highlighted. If you are not using <code> tag, replace < sign with &lt;.

Submit Comment

 
Copyright © 2005 - 2008, Dmytro Shteflyuk