Colored texts while waiting for the shell

As a name, is there a way to use colored texts in a wait shell? Like the command script echo the commands below.

echo -e "\033[32m Hello World" 
+4
source share
1 answer

I endured the answer to this question for quite some time, and now, it seems, I finally got the answer.

How it works is to use 033 to send <ESC> and then [ to send ANSI escape codes separated by semicolons, however, since [ is a special character, it must also be escaped with a backslash. You can then send ANSI sequences and delimit with m .

ANSI Interrupt Sequence Example

  • 0 Reset / Cancel all attributes

  • 1 Bold or increased intensity

  • 4 Underline: Single

  • 30 Text Color Black

  • 31 Text color Red

  • 32 Set the text color to Green.

A complete list can be found here: http://en.wikipedia.org/wiki/ANSI_escape_code

Example:

 puts "\033\[01;31m" # This will turn text red puts "~~~This text is red and bold\n" puts "\033\[0;32m" # This will turn text green puts "This text is green and bold switched off\n" 

However, it does not work with the -nonewline option, which is a bit annoying. However, the send_user command seems to be much better at handling tasks and under control:

 send_user "\033\[01;31mRed bold \033\[0;32mGreen again" 

You can even combine this with variables to make the output more readable:

 set green "\033\[0;32;40m" set red "\033\[1;31m" send_user "${red}Red bold ${green}Green again" 
+6
source

Source: https://habr.com/ru/post/1439608/


All Articles