2010-01-15 244 views
38

如何使我从基于命令行的ruby程序颜色输出的puts命令成为可能? 我会赞赏任何参考,我怎么称呼每种不同的颜色也。如何输出不同颜色的ruby命令行文本

比方说,我们先从这个..

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,我需要改变我的方法,以便程序在diff中正确输出吗?

+1

我在想红宝石命令行的文本,当我问我的问题早在2010年的问题有超过11000的意见和外部链接的堆栈。当我形成我的问题时,我不会想到要寻找“彩色的ruby输出”,事实上在搜索时找不到答案。暗示的答案可能是相似的,但问题提出的方式完全不同。我看到两个问题都是不同的,但很高兴能让他们联系起来。 – Evolve 2014-03-03 05:42:52

回答

56

我发现this article描述一个非常简单的方法来写有色的文本控制台。本文介绍这似乎这样的伎俩这个小例子(我冒昧地改善它略):

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的宝石的需求。

+1

为了使它更容易使用,你可以扩展String类(“Hello”.red):'class String; def red; colorize(self,“\ 033 [31m”);结束; end'。检查此线程以及:[Colorized Ruby输出](http://stackoverflow.com/questions/1489183/colorized-ruby-output) – 2012-07-18 16:25:25

+0

谢谢你的答案。它解决了我的问题。 – suresh 2016-07-25 12:12:02

1

对于一个快速和肮脏的解决方案,您可以直接嵌入在你的字符串ASCII颜色代码(\ E [XXM设置从现在开始使用到XX和\ E中的颜色[0米将彩色复位为正常):

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

似乎还有一个可用的helper library 可用于处理实际的ASCII码。

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

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

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

我已经创建了这样的事情:

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 

现在你可以使用另一种字符串的方法:

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

我发现Colored 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 
+3

呵呵,一个很好的扩展方法String:/ – tillda 2011-12-04 00:48:08

5

使用转义序列\033,而不是作为\e它是100%POSIX兼容和BSD-ISH将工作(例如OSX)系统为好。后者是一个GNU扩展。

1

我的建议:paint宝石。它不强制字符串扩展并支持256色(对于非256色终端,采用后备模式)。

用法:

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

使用彩色化的宝石!检查出来:

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 
3

想我会添加其他的解决方案,因为它做的事情有点不同,包括多个颜色代码:

首先一些例子...

使用方法链接:

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 

我在this gist I created和ex上给了几个例子将代码放大以包含优化,以便修改字符串的范围。

这是基本的代码:

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

不错!感谢您的补充,我敢肯定,几个窥视会发现这个有用的,爱巧妙的使用方法链接:) – Evolve 2016-08-04 08:16:32

相关问题