And*_*ndy 16
我更喜欢Rainbow gem,因为如果安装了win32console gem,它也支持Windows.
你可以像这样使用它:
puts "some " + "red".color(:red) + " and " + "blue on yellow".color(:blue).background(:yellow)
Run Code Online (Sandbox Code Playgroud)
你所要做的就是从一开始就"\e[##m"结束"\e[0m"
只需将##替换为颜色编号即可.例如:
31:Red
32:Green
33:Yellow
34:Blue
35:Magenta
36:Teal
37:Grey
1:Bold (Can be used with any color)
这是一个显示所有终端颜色的ruby脚本.下载或运行下面的代码.
def color(index)
normal = "\e[#{index}m#{index}\e[0m"
bold = "\e[#{index}m\e[1m#{index}\e[0m"
"#{normal} #{bold} "
end
8.times do|index|
line = color(index + 1)
line += color(index + 30)
line += color(index + 90)
line += color(index + 40)
line += color(index + 100)
puts line
end
Run Code Online (Sandbox Code Playgroud)
小智 5
使用String类方法,如:
class String
def black; "\033[30m#{self}\033[0m" end
def red; "\033[31m#{self}\033[0m" end
def green; "\033[32m#{self}\033[0m" end
def brown; "\033[33m#{self}\033[0m" end
def blue; "\033[34m#{self}\033[0m" end
def magenta; "\033[35m#{self}\033[0m" end
def cyan; "\033[36m#{self}\033[0m" end
def gray; "\033[37m#{self}\033[0m" end
end
Run Code Online (Sandbox Code Playgroud)
和用法:
puts "This prints green".green
puts "This prints red".red
Run Code Online (Sandbox Code Playgroud)