更好的ruby终端着色库
有很多着色库:彩色,term-ansicolor。 但有没有可以做到这一点:
puts "#{'hello'.red} world!".bold
和world!
应该是大胆的。
为了说清楚,我想得到这个:
"\e[1m\e[31mhello\e[0m\e[1m world!\e[0m"
甚至更好(更短):
"\e[1;31mhello\e[0;1m world!\e[0m"
而不是这个:
"\e[1m\e[31mhello\e[0m world!\e[0m"
由于没有,我写了我自己的, 二十一点和妓女聪明一个 – smart_colored
gem install smart_colored
并运行
require 'smart_colored/extend' # without extend you'll need to use 'string'.colored.red puts "#{'hello'.red} world!".bold
lib被称为Highline它有一个color
方法: say("This should be <%= color('bold', BOLD) %>!")
这可以很容易地用来实现String#bold
。
如果您使用的是高线,我制作了一个可以扩展颜色的gem并提供帮助,例如:
say_bold 'this is bold text'