使用Lua脚本启用bash输出颜色 [英] Enable bash output color with Lua script
问题描述
我知道可以使用ANSI转义来对bash脚本的输出进行着色序列。例如:
$ echo -e这是red-> \e [00; 31mRED\e [00m
我尝试在Lua中执行相同操作:
$ lua -eio.write('这是red-> \\e [00; 31mRED\\\e [00m\\\
')
但它不起作用。我也尝试使用 print()
而不是 io.write()
,但结果是一样的。 p>
lua -eprint('这是red-> \27 [31mred\\\
')
注意\27。每当Lua看到一个 \
后面跟一个十进制数字时,它将这个十进制数转换为ASCII等价物。我使用\27获取bash \033 ESC字符。更多信息此处。
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.
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"
I tried to do the same in Lua :
$ lua -e "io.write('This is red->\\e[00;31mRED\\e[00m\n')"
but it does not work. I also tried with print()
instead of io.write()
, but the result is the same.
lua -e "print('This is red->\27[31mred\n')"
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屋!