ruby - 运行 Ruby 脚本时更改 Bash 终端输出颜色

标签 ruby bash

我想简单地更改我的终端输出颜色,运行 Ruby 脚本,以便标准输出以更改后的颜色打印,“ sleep ”一秒钟,然后将其更改回来。我知道如何设置颜色,例如提示:

PS1="\e[0;36m[\w] \e[m "

我想我需要编写一个 Bash 函数来做到这一点。那会是什么样子?

最佳答案

这是一个 Ruby 脚本,用于显示所有终端颜色。 Download it或运行下面的代码。

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

关于ruby - 运行 Ruby 脚本时更改 Bash 终端输出颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15974716/

相关文章:

ruby-on-rails - Ruby on Rails 中的 ruby​​-openai api gem : how to implement a streaming conversation?

ruby-on-rails - 即使安装了 libv8,Gem therubyracer 也不会安装

javascript - 在字符类中使用 && 运算符

bash - 是否可以列出与 ls 模式匹配的文件?

arrays - 在 bash v.3 中迭代数组

ruby - 在方法定义中将等号 ('=' ) 放在方法名称之后有什么作用?

ruby - 我怎样才能从 block 中提前返回一些东西?

linux - 如何查找目录组合

linux - Sh, awk : how to convert integers, 在 shell 中可见为 16 位低位/大端二进制整数的字符串?

bash - 如何可靠地 rm -fr *?