Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable bash output color with Lua script

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.

like image 761
Wookai Avatar asked Nov 11 '09 22:11

Wookai


1 Answers

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.

like image 86
jkndrkn Avatar answered Oct 04 '22 22:10

jkndrkn