Evo*_*lve 40 ruby command-line colors
如何从基于命令行的ruby程序颜色输出put命令?我很感激任何关于我如何称呼每种不同颜色的参考.
让我们说我们从这开始..
puts "The following word is blue.. Im Blue!"
puts "The following word is green.. Im Green!"
puts "The following word is red.. Im Red!"
Run Code Online (Sandbox Code Playgroud)
我得到了我想要的不同颜色的不同文字,你得到了这个想法.
我使用Ubuntu,我需要改变我的方法,以便程序在diff os中正确输出吗?
Veg*_*ger 57
我发现这篇文章描述了一种将彩色文本写入控制台的简单方法.这篇文章描述了这个似乎可以解决问题的小例子(我冒昧地稍微改进了一下):
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') + ']'
Run Code Online (Sandbox Code Playgroud)
Best似乎定义了一些颜色.当您还需要不同的背景颜色时,可以扩展示例(请参阅文章底部).
使用Window XP时,作者提到了一个名为win32console的gem的要求.
Mar*_*rey 38
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
Run Code Online (Sandbox Code Playgroud)
小智 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
Run Code Online (Sandbox Code Playgroud)
现在您可以使用另一种String方法:
"Hello World".color(Colors::DARKGREEN)
Run Code Online (Sandbox Code Playgroud)
要知道所有颜色,只需执行以下操作:
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
Run Code Online (Sandbox Code Playgroud)
我想我会添加另一个解决方案,因为它的作用有点不同,并且包含更多颜色代码:
首先一些例子...
使用方法链:
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]
Run Code Online (Sandbox Code Playgroud)
使用该style方法并应用多个属性:
puts "How are you?".style(:red)
puts 'I am good!'.style(:blue, :underline)
puts 'Good to hear'.style([:bg_magenta, :blink])
Run Code Online (Sandbox Code Playgroud)
这可以用于以某种方式存储样式属性以便稍后应用:
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
Run Code Online (Sandbox Code Playgroud)
关于我创建的要点,我给出了更多示例我创建并扩展了代码以包含改进,以便对 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29257 次 |
| 最近记录: |