bash 红宝石的终端颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1108767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:02:29  来源:igfitidea点击:

terminal color in ruby?

rubybashshellcolorsterminal

提问by gustavgans

Is there a ruby module for colorize strings in a linux terminal?

在 linux 终端中是否有用于为字符串着色的 ruby​​ 模块?

回答by Andy

I prefer the Rainbow gemsince it also supports Windows if win32console gem has been installed.

我更喜欢Rainbow gem,因为如果安装了 win32console gem,它也支持 Windows。

You can use it like this:

你可以这样使用它:

puts "some " + "red".color(:red) + " and " + "blue on yellow".color(:blue).background(:yellow)

回答by gustavgans

ehm ok google was my friend :)

嗯,好吧,谷歌是我的朋友 :)

http://term-ansicolor.rubyforge.org/

http://term-ansicolor.rubyforge.org/

回答by mvndaai

All you have to do is start with "\e[##m"and end with "\e[0m"

你所要做的就是开始"\e[##m"和结束"\e[0m"

Just replace the ## with the color number. Examples are:

只需用颜色编号替换##。例子是:

31:Red32:Green33:Yellow34:Blue35:Magenta36:Teal37:Grey

31:Red32:Green33:Yellow34:Blue35:Magenta36:Teal37:Grey

1:Bold (Can be used with any color)

1:Bold (Can be used with any color)

Here is a ruby script to show all the terminal colors. Download itor run the code below.

这是一个显示所有终端颜色的 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

回答by TantrajJa

Using String class methods like:

使用 String 类方法,例如:

class String
def black;          "3[30m#{self}3[0m" end
def red;            "3[31m#{self}3[0m" end
def green;          "3[32m#{self}3[0m" end
def brown;          "3[33m#{self}3[0m" end
def blue;           "3[34m#{self}3[0m" end
def magenta;        "3[35m#{self}3[0m" end
def cyan;           "3[36m#{self}3[0m" end
def gray;           "3[37m#{self}3[0m" end
end

and usage:

和用法:

puts "This prints green".green
puts "This prints red".red

回答by TantrajJa

I am a big fan of the ruby colorize gem, which I recently downloaded. Once you download and include it into your program, you can add

我是最近下载的 ruby​​ colorize gem 的忠实粉丝。下载并将其包含在程序中后,您可以添加

.colorize(:blue)

to the end of any string. You can use most colors, including preceding the color by light_ like so

到任何字符串的末尾。您可以使用大多数颜色,包括在颜色前加上 light_ 像这样

.colorize(:light_blue)

you can also do background colors, EG:

你也可以做背景颜色,EG:

puts "mytext".colorize(:background => :green

colorized underlines, EG:

彩色下划线,EG:

puts "mytext".on_blue.underline

or use HTML-like tags for it as well

或者也使用类似 HTML 的标签

puts <blue> "text text text" </blue>

for the colorize Github, go to The colorize Github

对于着色 Github,请转到着色 Github

you can install the colorize gem by typing

您可以通过键入来安装 colorize gem

gem install colorize

into your terminal, command prompt, whatever. then put this into your file BEFORE YOU PUT IN THE USES OF IT

进入您的终端,命令提示符,等等。然后在您使用它之前将其放入您的文件中

EG:

例如:

require 'rubygems'
require 'colorize'
puts "mytext".colorize(:red)

But NOT

但不是

puts "mytext".colorize(:red)
require 'rubygems'
require 'colorize'

The require statements MUST be in the program in lines BEFORE you use the gem

在使用 gem 之前,require 语句必须在程序中的行中