Is there a gem like 'Term::ANSIColor' which works with 256 color terminals? The perl script 256colors2.pl works great in my terminal, and I'd like to use some of these colors in my ruby scripts without manually inserting the ANSI codes.
256 color terminal library for Ruby?
4.3k Views Asked by brianegge At
5
There are 5 best solutions below
0

Here's one that supports 256-colours -
Paint manages terminal colors and effects for you. It combines the strengths of gems like term-ansicolor or rainbow into a simple to use and flexible colorizer.
0

I played around a bit with the earlier answer and got something that I found a little more fun to work with.
LIB
def gray(g); 232 + g; end
def rgb(red, green, blue); 16 + (red * 36) + (green * 6) + blue; end
def green; rgb(0,5,0); end
def red; rgb(5,0,0); end
def c( fg, bg = nil ); "#{fg ? "\x1b[38;5;#{fg}m" : ''}#{bg ? "\x1b[48;5;#{bg}m" : ''}" end
def ec; "\x1b[0m"; end
EXAMPLE USAGE
BASE_DIR = File.expand_path( File.join( File.dirname(__FILE__), '..' ) )
def status( sDaemon )
b = File.exist?( File.join( BASE_DIR, 'pids', "#{sDaemon}.pid" ) )
puts c( b ? green : red ) + sDaemon + ( b ? ' RUNNING' : ' STOPPED' ) + ec
end
%w{ backuper emailSpamChecker indexer log2email orderManager sitemapProducer }.each { |s| status s }
Here's an adaptation of the 256colors2.pl script to ruby, with some help from this guide. It defines a
print_color(text, foreground, background)
method that should be easily applicable to your projects. It prints the string in colour, and then resets the colour to the terminal default. Should be easy enough to skip the reset if you'd prefer that.