问题描述
如何让我从基于命令行的ruby程序颜色输出的puts命令?
我们以这个开头。
puts以下字是蓝色... Im Blue!
puts以下字是绿色的... Im Green!
puts下面的字是红色的... Im Red!
我得到不同的文字,我想要不同的颜色,我想,你得到的想法。 >
我使用Ubuntu,我需要改变我的方法,以便程序在diff os正确输出吗?
我发现描述了一种非常简单的方法来将彩色文本写入控制台。该文章描述了这个小例子,似乎做的伎俩(我采取了自由改善它稍微):
def colorize text,color_code)
\e [#{color_code} m#{text} \e [0m
end
def red colorize(text,31); end
def green(text); colorize(text,32); end
#实际示例
puts'导入类别['+ green('DONE')+']'
puts'导入标签['+ red('FAILED') +']'
Best似乎定义了一些颜色。
当使用Window XP时,作者提到了一个gem的需求,这个gem称为。
How can I make the puts commands I output from a commandline based ruby program colour?I would appreciated any references to how I call each different colour also.
Lets say we start with this..
puts "The following word is blue.. Im Blue!"
puts "The following word is green.. Im Green!"
puts "The following word is red.. Im Red!"
And I get different text I want in different colours I want, You get the idea.
Im using Ubuntu, would I need to change my approach so that the program outputs correctly in diff os?
I found this article describing a very easy way to write coloured texts to the console. The article describes this little example which seems to do the trick (I took the liberty to improve it slightly):
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') + ']'
Best seems to define some of the colours. You can extent the example when you need also different background colours (see bottom of article).
When using Window XP, the author mentions the requirement of a gem called win32console.
这篇关于如何输出我的ruby命令行文本不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!