views:

425

answers:

5

Is there a proper plugin or a class to perform background and foreground text colorization within a common output console?

I remember, when programming Pascal we all used to play with textcolor(...) procedures to make our small educational programs look more pretty and presentational.

Is there anything similar in Ruby?

+3  A: 

This may help you: Colorized ruby output

ennuikiller
+1  A: 

You can use ANSI escape sequences to do this on the console. I know this works on Linux and OSX, I'm not sure if the Windows console (cmd) supports ANSI.

I did it in Java, but the ideas are the same.

//foreground color
public static final String BLACK_TEXT()   { return "\033[30m";}
public static final String RED_TEXT()     { return "\033[31m";}
public static final String GREEN_TEXT()   { return "\033[32m";}
public static final String BROWN_TEXT()   { return "\033[33m";}
public static final String BLUE_TEXT()    { return "\033[34m";}
public static final String MAGENTA_TEXT() { return "\033[35m";}
public static final String CYAN_TEXT()    { return "\033[36m";}
public static final String GRAY_TEXT()    { return "\033[37m";}

//background color
public static final String BLACK_BACK()   { return "\033[40m";}
public static final String RED_BACK()     { return "\033[41m";}
public static final String GREEN_BACK()   { return "\033[42m";}
public static final String BROWN_BACK()   { return "\033[43m";}
public static final String BLUE_BACK()    { return "\033[44m";}
public static final String MAGENTA_BACK() { return "\033[45m";}
public static final String CYAN_BACK()    { return "\033[46m";}
public static final String WHITE_BACK()   { return "\033[47m";}

//ANSI control chars
public static final String RESET_COLORS() { return "\033[0m";}
public static final String BOLD_ON()      { return "\033[1m";}
public static final String BLINK_ON()     { return "\033[5m";}
public static final String REVERSE_ON()   { return "\033[7m";}
public static final String BOLD_OFF()     { return "\033[22m";}
public static final String BLINK_OFF()    { return "\033[25m";}
public static final String REVERSE_OFF()  { return "\033[27m";}
Ryan Michela
+5  A: 

Colorize is my favorite gem! :-) Check it out:

http://colorize.rubyforge.org/files/README_txt.html

usage:

puts "I am now red.".red
puts "I am now blue.".green
puts "I am a super coder".yellow
pageinventor
Much easier than doing it by hand!
Ryan Michela
Thanks! That's probably the easiest way!
gmile
Well, nice solution. But it made me 5 gems to install instead of one, to run it
gmile
+2  A: 

I found a few:

http://github.com/ssoroka/ansi/tree/master

Examples:

puts ANSI.color(:red) { "hello there" }
puts ANSI.color(:green) + "Everything is green now" + ANSI.no_color

http://flori.github.com/term-ansicolor/

Examples:

print red, bold, "red bold", reset, "\n"
print red(bold("red bold")), "\n"
print red { bold { "red bold" } }, "\n"

http://github.com/sickill/rainbow

Example:

puts "this is red".foreground(:red) + " and " + "this on yellow bg".background(:yellow) + " and " + "even bright underlined!".underline.bright

If you are on Windows you may need to do a "gem install win32console" to enable support for colors.

Also the article Colorizing console Ruby-script output is useful if you need to create your own gem. It explains how to add ANSI coloring to strings. You can use this knowledge to wrap it in some class that extends string or something.

Petros
+2  A: 

I keep a list of color-related gems here. Petros covered most of 'em but there's also text-highlight, clio and termcolor