使用Lua脚本启用bash输出颜色 [英] Enable bash output color with Lua script

查看:213
本文介绍了使用Lua脚本启用bash输出颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个Lua脚本可以在文本文件和控制台中运行经验并输出大量信息。我想在控制台输出中添加一些颜色,使其更易于阅读。



我知道可以使用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屋!

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