如何使用 Ruby 为终端的文本输出着色? [英] How can I use Ruby to colorize the text output to a terminal?

查看:30
本文介绍了如何使用 Ruby 为终端的文本输出着色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Ruby,如何为终端中的输出执行背景和前景文本着色?

Using Ruby, how can I perform background and foreground text colorization for output in the terminal?

我记得,在为 Pascal 编程时,我们都习惯于编写自己的 textcolor(...) 程序,以使我们的小型教育程序看起来更漂亮和更具表现力.

I remember, when programming Pascal we all used to write our own textcolor(…) procedures to make our small educational programs look more pretty and presentational.

我将如何在 Ruby 中编写等效的代码?核心库中是否有任何内置支持适用于此?如果没有,添加它的惯用方法是什么?

How would I go about coding an equivalent of that in Ruby? Is there any built-in support in the core library that lends itself to this? If not, what would be an idiomatic way to add it?

推荐答案

Colorize 是我最喜欢的宝石!:-)

Colorize is my favorite gem! :-)

检查一下:

https://github.com/fazibear/colorize

安装:

gem install colorize

用法:

require 'colorize'

puts "I am now red".red
puts "I am now blue".blue
puts "Testing".yellow

这篇关于如何使用 Ruby 为终端的文本输出着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆