使用Lua脚本启用bash输出颜色 [英] Enable bash output color with Lua script
问题描述
我有几个Lua脚本运行经验和输出大量的信息,在文本文件和控制台。我想在控制台输出中添加一些颜色,以使其更易于阅读。
I have several Lua scripts that run experiences and output a lot of information, in text files and in the console. I'd like to add some colors in the console output, to make it more readable.
我知道可以使用ANSI escape来对bash脚本的输出进行着色序列。例如:
I know that it's possible to color the output of bash scripts using the ANSI escape sequences. For example :
$ echo -e "This is red->\e[00;31mRED\e[00m"
我试图在Lua中这样做:
I tried to do the same in Lua :
$ lua -e "io.write('This is red->\\e[00;31mRED\\e[00m\n')"
但它不工作。我也尝试用 print()
而不是 io.write()
,但结果是一样的。 p>
but it does not work. I also tried with print()
instead of io.write()
, but the result is the same.
推荐答案
lua -eprint('This is red-> \27 [31mred\\\
')
注意\27。每当Lua看到一个 \
后跟一个十进制数,它会将这个十进制数转换成它的ASCII等效值。我使用\27获得了bash \033 ESC字符。更多信息此处。
Note the \27. Whenever Lua sees a \
followed by a decimal number, it converts this decimal number into its ASCII equivalent. I used \27 to obtain the bash \033 ESC character. More info here.
这篇关于使用Lua脚本启用bash输出颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!