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
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