views:

531

answers:

6

How can I make the puts commands I output from a commandline based ruby program colour? I would appreciated any references to how I call each different colour also.

Lets say we start with this..

Puts "The following word is blue.. Im Blue!"
Puts "The following word is green.. Im Green!"
Puts "The following word is red.. Im Red!"

And I get different text I want in different colours I want, You get the idea.

Im using Ubuntu, would I need to change my approach so that the program outputs correctly in diff os?

+6  A: 

I found this article describing a very easy way to write coloured texts to the console. The article describes this little example which seems to do the trick (I took the liberty to improve it slightly):

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

def red(text); colorize(text, 31); end
def green(text); colorize(text, 32); end

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

Best seems to define some of the colours. You can extent the example when you need also different background colours (see bottom of article).

When using Window XP, the author mentions the requirement of a gem called win32console.

Veger
A: 

For a quick and dirty solution you can just embed ASCII colour codes in your strings (\e[XXm sets the colour to be used from now on to XX and \e[0m reset the colour to normal):

puts "The following word is blue.. \e[34mIm Blue!\e[0m"
puts "The following word is green.. \e[32mIm Green!\e[0m"
puts "The following word is red.. \e[31mIm Red!\e[0m"

ASCII codes also support things like underlining, blinking, and highlighting of text.

There also seems to be a helper library available that deals with the actual ASCII codes for you.

Edit: regarding the different platforms: you shouldn't have any trouble using ASCII codes on unix machines, but windows, AFAIK, doesn't support them out of the box. Fortunately there's a win32console gem that seems to fix this.

You can use the following snippet (found on the page Veger linked to) to load the win32console library only on windows:

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

I've created something like this:

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

class Colors
   COLOR1 = "\e[1;36;40m"
   COLOR2 = "\e[1;35;40m"
   NOCOLOR = "\e[0m"
   RED = "\e[1;31;40m"
   GREEN = "\e[1;32;40m"
   DARKGREEN = "\e[0;32;40m"
   YELLOW = "\e[1;33;40m"
   DARKCYAN = "\e[0;36;40m"
end

class String
   def color(color)
      return color + self + Colors::NOCOLOR
   end
end

Now you can just use another method of String:

"Hello World".color(Colors::DARKGREEN)

To know all the colors just execute this:

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

[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
darek129
+5  A: 

I find the Colored gem to be the easiest and cleanest to use.

puts "this is red".red
puts "this is red with a blue background (read: ugly)".red_on_blue
puts "this is red with an underline".red.underline
puts "this is really bold and really blue".bold.blue
logger.debug "hey this is broken!".red_on_yellow 
Mark Carey
+1  A: 

Check out the cli-colorize gem: http://github.com/stjohncj/cli-colorize/blob/master/README.rdoc

Chris