Objective
Everyone knows that when using the LS command to list file lists, different file types are displayed in different colors. So how do you implement such a colored text output? The answer is not complicated, either in the shell or the C language.
First, the implementation method under the shell
First of all, under the shell, how to achieve. This can be achieved with the echo command, see the following example:
Echo-e "33[32mhello, world!"
When you hit this command in the terminal, did you find that the system was using green to output "hello,world!", more than that, even after the command prompt has become green? Do not worry, listen to me continue to say. The echo command-e option activates the terminal's interpretation of the backslash escape character (that is). The quotation marks 33 is used to guide the unconventional character sequence, here is the role of the boot setting output properties, the back of [32m is the foreground color is set to green, the letter M is the Set property category, the number represents the property value.
Settings can be used alone, for example:
Echo-e "33[0m"
The purpose of this line of command is to restore the property to the default value, which means that the 0m setting is used to restore the default value. Now, is your terminal all right again?
With this understanding, the rest is simple. With this command, you can set many properties in addition to setting the text foreground color.
The other settings are listed below:
33[0m Close All properties 33[1m set high brightness 33[4m underline 33[5m blink 33[7m reverse 33[8m fade 33[30m to 33[37m set foreground color 33[40m to 33[47m set background color 33[na cursor up n rows 33[nb cursor down n rows 33[NC cursor right shift n line 33[nd cursor left n line 33[y;xh set cursor position 33[2j Clear screen 33[k clear content from cursor to end of line 33[s save cursor position 33[u restore cursor position 33[?25l Hide cursor 33[?25h display cursor
The colors represented by the numbers are as follows:
Word background color range: 49----
40: Black
41: Crimson
42: Green
43: Yellow
44: Blue
45: Purple
46: Dark Green
47: White
Word color: 39-----------
30: Black
31: Red
32: Green
33: Yellow
34: Blue
35: Purple
36: Dark Green
37: White
In addition, a variety of similar settings can be grouped together, separated by semicolons (;).
As follows:
Echo-e "33[20;1h33[1;4;32mhello,world33[0m"
This line of command first 33[20;1h the cursor to the 20th row 1th column of the terminal, after which 33[1;4;32m sets the Text property to be highlighted, underlined, and the color is green, and then outputs Hello,world, and finally 33[0m restores the terminal property to its default value. This will not see the command prompt after the command has been completed is changed.
Through the combination of the above commands can be achieved to the terminal output complex control.
Second, how to implement in C programming?
Understanding the implementation of the above in the shell, about how to implement in C is very simple. It can be said that only the printf function instead of the upper echo-e is OK, see the following example:
int color = 32; printf ("33[20;1h33[1;4;%dmhello, world.33[0m", color);
This example is similar to the last example in the shell above, except that the color value is specified by the variable color (or, of course, it can be specified directly).
Third, Lenovo
See here you may wonder, is it possible to use similar methods in other programming languages to control the output of the terminal? The answer is YES! In Python, for example, you can output the following:
color=32 print "33[20;1h33[1;4;%dhello, world.33[0m"%color
The effect of this example is the same as the example in C above.
Summarize
The above is the entire content of this article, I hope that the content of this article on everyone's study or work can bring some help, if there are questions you can message exchange.