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.
是否有像'Term :: ANSIColor'这样的宝石可以使用256色终端? perl脚本256colors2.pl在我的终端中运行良好,我想在我的ruby脚本中使用其中一些颜色,而无需手动插入ANSI代码。
5 个解决方案
#1
11
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.
这是256colors2.pl脚本改编为ruby,在本指南的帮助下。它定义了一个应该可以轻松应用于项目的print_color(文本,前景,背景)方法。它以彩色打印字符串,然后将颜色重置为终端默认值。如果您愿意,应该很容易跳过重置。
def rgb(red, green, blue)
16 + (red * 36) + (green * 6) + blue
end
def gray(g)
232 + g
end
def set_color(fg, bg)
print "\x1b[38;5;#{fg}m" if fg
print "\x1b[48;5;#{bg}m" if bg
end
def reset_color
print "\x1b[0m"
end
def print_color(txt, fg, bg)
set_color(fg, bg)
print txt
reset_color
end
# convenience method
def rgb_cube
for green in 0..5 do
for red in 0..5 do
for blue in 0..5 do
yield [red, green, blue]
end
print " "
end
puts
end
end
# rgb list on black bg
rgb_cube do |red, green, blue|
print_color("%d%d%d " % [red, green, blue], rgb(red, green, blue), nil)
end
puts
# rgb list on white bg
rgb_cube do |red, green, blue|
print_color("%d%d%d " % [red, green, blue], rgb(red, green, blue), 15)
end
puts
# system palette:
print "System colors:\n";
(0..7).each do |color|
print_color(" ", nil, color)
end
puts
(8..15).each do |color|
print_color(" ", nil, color)
end
puts
puts
# color cube
print "Color cube, 6x6x6:\n"
rgb_cube do |red, green, blue|
print_color(" ", nil, rgb(red, green, blue))
end
puts
# grayscale ramp
print "Grayscale ramp:\n"
for g in (0..23) do
print_color(" ", nil, gray(g))
end
puts
puts
#3
2
Here's one that supports 256-colours -
这是一个支持256色的 -
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.
涂料
#4
1
I played around a bit with the earlier answer and got something that I found a little more fun to work with.
我在前面的答案中玩了一下,得到了一些我发现更有趣的东西。
LIB
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 }
#5
-3
There is a gem called Term::ANSIColor on Rubyforge... :)
No idea how good it is though.
在Rubyforge上有一个名为Term :: ANSIColor的宝石...... :)不知道它有多好。
http://rubyforge.org/projects/term-ansicolor/
http://rubyforge.org/projects/term-ansicolor/
#1
11
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.
这是256colors2.pl脚本改编为ruby,在本指南的帮助下。它定义了一个应该可以轻松应用于项目的print_color(文本,前景,背景)方法。它以彩色打印字符串,然后将颜色重置为终端默认值。如果您愿意,应该很容易跳过重置。
def rgb(red, green, blue)
16 + (red * 36) + (green * 6) + blue
end
def gray(g)
232 + g
end
def set_color(fg, bg)
print "\x1b[38;5;#{fg}m" if fg
print "\x1b[48;5;#{bg}m" if bg
end
def reset_color
print "\x1b[0m"
end
def print_color(txt, fg, bg)
set_color(fg, bg)
print txt
reset_color
end
# convenience method
def rgb_cube
for green in 0..5 do
for red in 0..5 do
for blue in 0..5 do
yield [red, green, blue]
end
print " "
end
puts
end
end
# rgb list on black bg
rgb_cube do |red, green, blue|
print_color("%d%d%d " % [red, green, blue], rgb(red, green, blue), nil)
end
puts
# rgb list on white bg
rgb_cube do |red, green, blue|
print_color("%d%d%d " % [red, green, blue], rgb(red, green, blue), 15)
end
puts
# system palette:
print "System colors:\n";
(0..7).each do |color|
print_color(" ", nil, color)
end
puts
(8..15).each do |color|
print_color(" ", nil, color)
end
puts
puts
# color cube
print "Color cube, 6x6x6:\n"
rgb_cube do |red, green, blue|
print_color(" ", nil, rgb(red, green, blue))
end
puts
# grayscale ramp
print "Grayscale ramp:\n"
for g in (0..23) do
print_color(" ", nil, gray(g))
end
puts
puts
#2
#3
2
Here's one that supports 256-colours -
这是一个支持256色的 -
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.
涂料
#4
1
I played around a bit with the earlier answer and got something that I found a little more fun to work with.
我在前面的答案中玩了一下,得到了一些我发现更有趣的东西。
LIB
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 }
#5
-3
There is a gem called Term::ANSIColor on Rubyforge... :)
No idea how good it is though.
在Rubyforge上有一个名为Term :: ANSIColor的宝石...... :)不知道它有多好。
http://rubyforge.org/projects/term-ansicolor/
http://rubyforge.org/projects/term-ansicolor/