We all know that when using LS command to list files, different file types will be displayed in different colors. So how to achieve such a color text output? The answer is not complicated, whether in shell or C.
1、 The implementation method under shell
Let’s talk about how to implement it in shell. It can be realized by using the echo command. See the following example:
echo -e "33[32mHello, world!"
When you type this command in the terminal, do you find that the system outputs “Hello, world!” in green? Not only that, but also the following command prompt turns green? Don’t worry. Listen to me.
-eThe function of the option is to activate the terminal’s interpretation of the backslash escape character. 33 in quotation marks is used to guide the unconventional character sequence. Here, it is used to guide the setting of output attributes. The [32m] in the back is to set the foreground color to green, the letter M represents the set attribute category, and the number represents the attribute value.
Settings can be used alone, for example:
echo -e "33[0m"
The function of this command is to restore the property to the default value, that is to say, the setting item is used to restore the default value. Now your terminal is all right again?
After understanding this, the rest is simple. With this command, in addition to setting the text foreground, you can also set many properties.
Other settings are listed below
33 [0m] close all attributes 33 [1m] set high brightness 33 [4m] 33 [5m] 33 [7m] 33 [8m] blanking 33 [30m to 33 [37m set foreground color Set the background color from 33 [40m to 33 [47m 33 [Na] move the cursor up n lines 33 [NB] move the cursor down n lines 33 [NC] the cursor moves n lines to the right 33 [nd] move the cursor n lines to the left 33 [y; XH] set cursor position 33 [2J] clear screen 33 [k] clear the contents from the cursor to the end of the 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:
Background color range: 40-49
46: dark green
Word color: 30-39
36: dark green
In addition, multiple settings of the same kind can be combined and separated by semicolons (;).
echo -e "33[20;1H33[1;4;32mHello,world33[0m"
This command first 33 [20; 1H moves the cursor to the first column of the 20th line of the terminal, then 33 [1; 4; 32m sets the text attribute to highlight, underline and green, and then outputs Hello, world; finally 33 [0m restores the terminal attribute to the default value, so that the command prompt will not change even after the command is completed.
Through the combination of the above commands, the complex control of the terminal output can be realized.
2、 How to realize it in C programming?
After understanding the above implementation method in shell, how to implement it in C is very simple. It can be said that it only needs to be used
printfFunction instead of
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 one in the shell above, except that the color value is specified by the variable color (or directly).
When you see this, you may wonder if you can use a similar method to control the terminal output in other programming languages? The answer is yes! For example, in Python, you can output as follows:
color=32 print “33[20;1H33[1;4;%dHello, world.33[0m"%color
The effect of this example is the same as that of C above.
The above is the whole content of this article, I hope the content of this article can bring some help to your study or work, if you have any questions, you can leave a message to exchange.