Ruby 中 STDOUT 的文本格式

发布于 2024-07-21 08:12:33 字数 117 浏览 7 评论 0原文

我正在编写一个将在 CLI 中运行的小型 Rub​​y 脚本。

为了改进界面,我需要为我输出的某些元素添加颜色/粗体。

这可行吗? 如果是这样,我几乎可以肯定这是这样,怎么办?

I am writing a small Ruby script that will run in a CLI.

To improve the interface, I need to would love to add color/boldness to some elements that I output.

Is that doable? If so, and I am almost sure this is, how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

腹黑女流氓 2024-07-28 08:12:33

在许多终端(但不是 Windows)上,您可以使用如下所示的 a 序列:"\e[#{code}m",其中代码基于 这些表。 如果使用多个代码,则必须用分号分隔。 主要代码是:

强度:

1  Bold Intensity
4  Underline
5  Slow blink
6  Fast blink
22 Normal Intensity

颜色:

Foreground 3X
Background 4X

Where X is:
-----------
0 Black
1 Red
2 Green
3 Yellow
4 Blue
5 Magenta
6 Cyan
7 White

例如,对于蓝色背景上缓慢闪烁的粗体绿色文本,您可以使用 "\e[5;1;32;44mWOW!\e[0m"< /代码>。 \e[0m 将所有内容重置为终端默认值。

On many terminals (but not Windows), you can use an a sequence like this: "\e[#{code}m", where the codes are based on these tables. The codes must be separated by a semicolon if using more than one. The major codes are:

Intensity:

1  Bold Intensity
4  Underline
5  Slow blink
6  Fast blink
22 Normal Intensity

Color:

Foreground 3X
Background 4X

Where X is:
-----------
0 Black
1 Red
2 Green
3 Yellow
4 Blue
5 Magenta
6 Cyan
7 White

So, for example, for slowly blinking, bold green text on a blue background, you would use "\e[5;1;32;44mWOW!\e[0m". The \e[0m resets everything to the terminal default.

海拔太高太耀眼 2024-07-28 08:12:33

有一个名为 rainbow 的 gem,可以非常轻松地设置终端输出的样式。

sudo gem install rainbow

安装后,您可以执行以下操作:

puts 'some text'.underline

There is a gem called rainbow that makes it really easy to style your terminal output.

sudo gem install rainbow

After installing it you can do stuff like:

puts 'some text'.underline
丘比特射中我 2024-07-28 08:12:33

亲爱的鲁比朋友们!
我更喜欢在 Ruby 中找到默认的集成支持。 我在这里找到了一些,无需安装任何 gem 即可工作:

def red(mytext); "\e[31m#{mytext}\e[0m"; end
def light_red(mytext); "\e[1;31m#{mytext}\e[0m"; end
def green(mytext); "\e[32m#{mytext}\e[0m"; end
def light_green(mytext); "\e[1;32m#{mytext}\e[0m"; end
def yellow(mytext); "\e[1;33m#{mytext}\e[0m"; end
def blue(mytext); "\e[34m#{mytext}\e[0m"; end
def light_blue(mytext); "\e[1;34m#{mytext}\e[0m"; end

puts red("hello world. I don't need no color.")
puts light_red("hello world. I don't need no color.")
puts green("hello world. I don't need no color.")
puts light_green("hello world. I don't need no color.")
puts blue("hello world. I don't need no color.")
puts light_blue("hello world. I don't need no color.")
puts yellow("hello world. I don't need no color.")

它适用于 putsprint

Dear Ruby folks!
I prefer to find the default integrated support available within Ruby. Here I've found a few, which may work without installing any gem:

def red(mytext); "\e[31m#{mytext}\e[0m"; end
def light_red(mytext); "\e[1;31m#{mytext}\e[0m"; end
def green(mytext); "\e[32m#{mytext}\e[0m"; end
def light_green(mytext); "\e[1;32m#{mytext}\e[0m"; end
def yellow(mytext); "\e[1;33m#{mytext}\e[0m"; end
def blue(mytext); "\e[34m#{mytext}\e[0m"; end
def light_blue(mytext); "\e[1;34m#{mytext}\e[0m"; end

puts red("hello world. I don't need no color.")
puts light_red("hello world. I don't need no color.")
puts green("hello world. I don't need no color.")
puts light_green("hello world. I don't need no color.")
puts blue("hello world. I don't need no color.")
puts light_blue("hello world. I don't need no color.")
puts yellow("hello world. I don't need no color.")

It works with both puts and print.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文