如何以不同的颜色输出我的ruby命令行文本

我怎样才能使我从基于命令行的ruby程序颜色输出的puts命令? 我会赞赏任何提及我怎样称呼每种不同的颜色。

可以说我们从这开始..

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

我想用不同的颜色,我想要不同的文字,你明白了。

即时通讯使用Ubuntu,我需要改变我的方法,以便程序正确输出差异吗?

我发现这篇文章描述了一个非常简单的方法来写入彩色文本到控制台。 这篇文章描述了这个小例子,这个小例子好像在做诡计(我冒昧地改进了一下):

 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 example puts 'Importing categories [ ' + green('DONE') + ' ]' puts 'Importing tags [' + red('FAILED') + ']' 

最好似乎定义了一些颜色。 当您需要不同的背景颜色时,您可以扩展该示例(请参阅文章的底部)。

在使用Window XP时,作者提到了一个叫做win32console的gem的需求。

我发现彩色的gem是最简单和最干净的使用。

 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 

我创build了这样的东西:

 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 

现在你可以使用另一种string方法:

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

要知道所有的颜色只需执行此操作:

 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 

使用转义序列\033而不是\e因为它是100%posix兼容的,并且也可以在bsd-ish(例如osx)系统上工作。 后者是一个GNU扩展。

以为我会添加另一个解决scheme,因为它做的事情有点不同,包括更多的颜色代码:

先举一些例子

使用方法链接:

 String.include(AnsiTextStyles) puts "How are you?".blue.bold + " " + 'I am good!'.red.bold puts '%s %s' % ["How are you?".blue.bold, 'I am good!'.red.bold] 

使用style方法并应用多个属性:

 puts "How are you?".style(:red) puts 'I am good!'.style(:blue, :underline) puts 'Good to hear'.style([:bg_magenta, :blink]) 

这可以用来以某种方式存储样式属性以便稍后应用:

 text_styles = { red_bold: [:red, :bold], blue_underline: [:blue, :underline], pretty: [:bg_magenta, :blink], } text_styles.each do |name, style| styled_text = "Text styled multiple ways".style(style) puts "%s: %s" % [name, styled_text] end 

我已经给出了几个关于这个要点的例子, 我创build并扩展了代码以包含细化,以便修改string的范围。

这是基本的代码:

 module AnsiTextStyles TEXT_ATTRIBUTES = { # text properties none: 0, # turn off all attributes bold: 1, bright: 1, # these do the same thing really italic: 3, underline: 4, blink: 5, reverse: 7, # swap foreground and background colours hide: 8, # foreground color same as background # foreground colours black: 30, grey: 90, lt_grey: 37, :white => 97, red: 31, lt_red: 91, green: 32, lt_green: 92, dk_yellow: 33, brown: 33, yellow: 93, blue: 34, lt_blue: 94, magenta: 35, pink: 95, lt_magenta: 95, cyan: 36, lt_cyan: 96, default: 39, # background colours bg_black: 40, bg_grey: 100, bg_lt_grey: 47, bg_white: 107, bg_red: 41, bg_lt_red: 101, bg_green: 42, bg_lt_green: 102, bg_dk_yellow: 43, bg_brown: 43, bg_yellow: 103, bg_blue: 44, bg_lt_blue: 104, bg_magenta: 45, bg_pink: 105, bg_lt_magenta: 105, bg_cyan: 46, bg_lt_cyan: 106, } def self.text_attributes TEXT_ATTRIBUTES.keys end # applies the text attributes to the current string def style(*text_attributes) codes = TEXT_ATTRIBUTES.values_at(*text_attributes.flatten).compact "\e[%sm%s\e[m" % [codes.join(';'), self.to_s] end end 

对于一个快速和肮脏的解决scheme,您可以在您的string中embeddedASCII颜色代码(\ e [XXm设置从现在开始使用的颜色到XX和\ e [0m将颜色重置为正常):

 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码还支持下划线,闪烁和突出显示文本等内容。

似乎还有一个可用的帮助程序库可以处理实际的ASCII代码。

编辑 :关于不同的平台:在unix机器上使用ASCII代码不应该有任何麻烦,但窗口,AFAIK,不支持开箱即用。 幸运的是有一个win32consolegem ,似乎解决这个问题。

您可以使用下面的代码片段(在链接到Veger的页面上find)只在Windows上加载win32console库:

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

查看cli-colorize gem: http : //github.com/stjohncj/cli-colorize/blob/master/README.rdoc

我的build议: 油漆gem。 它不强制string扩展,并支持256色(对于非256色terminal采用后备模式)。

用法:

 puts Paint["I'm blue!", :blue] puts Paint["I'm dark blue if your terminal supports it!", "#000044"] 

使用着色gem! 一探究竟:

https://github.com/fazibear/colorize

安装:

 sudo gem install colorize 

用法:

 require 'colorize' puts "I am now red.".red puts "I am now blue.".green puts "I am a super coder".yellow