Console terminal output color

Source: Internet
Author: User
Tags echo command

When Terminal simulators such as putty, secureCRT, and XShell are used to connect to the linux system, the outputs of ls, vim, and other tools contain various colors, which greatly enhance the readability of the text.

I. General example of terminal text color output

In bash, we can usually use the echo command to add the-e option to output texts of various colors, for example:

Echo-e "\ 033 [31 mRed Text \ 033 [0 m"
Echo-e "\ 033 [32 mGreen Text \ 033 [0 m"
Echo-e "\ 033 [33 mYellow Text \ 033 [0 m"
Echo-e "\ 033 [34 mBlue Text \ 033 [0 m"
Echo-e "\ 033 [35 mMagenta Text \ 033 [0 m"
Echo-e "\ 033 [36 mCyan Text \ 033 [0 m"

Output:

Red Text
Green Text
Yellow Text
Blue Text
Magenta Text
Cyan Text

Where: "\ 033 [31 m", "\ 033 [31 m", "\ 033 [0 m", etc. are ANSI escape sequences (ANSI escape code/sequence ), it controls the format and color of text output.
[Note]: \ 033 is the ASCII code corresponding to the Esc key in the upper left corner of the keyboard (in octal format). \ 033, \ x1b, and \ e have the same effect. For example: echo-e "\ x1b [31 mRed Text \ e [0 m" also outputs the Red font "Red Text".

 

It can be said that no matter what language, as long as your terminal can interpret the ANSI escape sequence (most unix-like terminal simulators can interpret the ANSI escape sequence, but not the win32 console ), you can use the ANSI escape sequence to output colors. The following are examples.

Example 1: C program output color example
1 // hello. c2 # include <stdio. h> 3 int main () {4 printf ("\ 033 [31; 4 mRed Underline Text \ 033 [0m \ n"); 5}

Compilation: gcc hello. c
Run:./a. out
Output: Red Underline Text

Example 2: example of the output color in the C ++ program
1 // hello. cpp2 # include <iostream> 3 int main () {4 std: cout <"\ 033 [31; 4 mRed Underline Text \ 033 [0 m" <std :: endl; 5}

Compile: g ++ hello. cpp
Run:./a. out
Output: Red Underline Text

Example 3: Sample color output in a Java program
1 // hello. java2 class hello {3 public static void main (String [] args) {4 System. out. println ("\ 033 [31; 4 mRed Underline Text \ 033 [0 m"); 5} 6}

[Note]: \ e and \ 0x1b cannot be identified in Java. Only \ 033 can be used.
Compile: javac hello. java
Run: java hello
Output: Red Underline Text

Example 4: example of the output color in the Python program
1 # hello. py2 print "\ 033 [31; 4 mRed Underline Text \ 033 [0 m"

[Note]: \ e cannot be identified in python (v2.6.5). \ 033 and \ x1b can be used.
Run: python hello. py
Output: Red Underline Text

 

II. More detailed description of ANSI escape sequences

The following describes the ANSI escape sequence for controlling text colors in detail.

The escape sequence format for controlling text colors is as follows:

CSI n1 [; n2 [;…] M

Among them, CSI is called Control Sequence Introducer/Initiator, that is, "\ 033 [" in the above example ["; n1 and n2 indicate SGR parameters (some common SGR parameters are listed below), which are used to control text output formats such as color, bold, italic, and blinking. m indicates that the escape sequence ends.

The following lists common SRG parameters:

Encoding Description
0 Disable all formats and restore them to the initial state.
1 Bold/highlighted
2 Fuzzy ()
3 Italic ()
4 Underline (single line)
5 Blinking (slow)
6 Flashing (fast )()
7 Switch background color and foreground color
8 Hide )()
30-37 Foreground color, that is, 30 + x, x represents different colors (see the "color table" below ")
40-47 Background color, that is, 40 + x, x represents different colors (see the "color table" below ")

[Note]: (1) the codes marked with (※) indicate that not all terminal simulators are supported, and only a few of them are supported.

(2) multiple SGR parameters can be used in combination, for example, echo-e "\ x1b [31; 4 mRed Underline Text \ e [0 m ": The Red Underline font" Red Underline Text "is output ".

(3) for more parameter information, see "ANSI escape code ".

Color table:

Color valueX 0 1 2 3 4 5 6 7
Color Black Red Green Yellow Blue Purple Qing White

 

3. win32 console program output color

The win32 console does not support ANSI escape sequences, so it is more complicated to output color text on the win32 console.

The following uses the SetConsoleTextAttribute and GetStdHandle win32 API functions to output colors, for example:

1 # include <stdio. h> 2 # include <windows. h> 3 4 int main () {5 HANDLE hdl = GetStdHandle (STD_OUTPUT_HANDLE); 6 SetConsoleTextAttribute (hdl, FOREGROUND_RED | FOREGROUND_INTENSITY); 7 printf ("Hello "); 8 SetConsoleTextAttribute (hdl, FOREGROUND_GREEN | FOREGROUND_INTENSITY); 9 printf ("world! \ N "); 10 11 getchar (); 12}

The output is "Hello world !". The two API prototypes used are as follows:

1 HANDLE GetStdHandle (DWORD nStdHandle); 2 BOOL SetConsoleTextAttribute (HANDLE hConsoleOutput, WORD wAttributes );

GetStdHandle is used to obtain the screen buffer handle (the standard output handle is obtained in the example), and SetConsoleTextAttribute is used to set the console text attribute (the foreground color is highlighted in red when the first call is made, for the second call, set it to green ).

(1) the GetStdHandle interface parameter nStdHandle and the return value are as follows:

Value (NStdHandle) Description
STD_INPUT_HANDLE Returns the standard input handle.
STD_OUTPUT_HANDLE Return the handle of the standard output.
STD_ERROR_HANDLE Returns a standard error handle.

[Note]: For more information, see "GetStdHandle ".

(2) SetConsoleTextAttribute () The first parameter hConsoleOutput is the screen buffer handle (which can be obtained through GetStdHandle (), and the second parameter wAttributes is the color attribute. Common color property values are as follows (can be used in combination ):

Attribute (wAttributes) Description
FOREGROUND_BLUE Foreground (blue)
FOREGROUND_GREEN Foreground (green)
FOREGROUND_RED Foreground color (red)
FOREGROUND_INTENSITY Foreground highlight
BACKGROUND_BLUE Background color (blue)
BACKGROUND_GREEN Background color (green)
BACKGROUND_RED Background color (red)
BACKGROUND_INTENSITY Highlighted background color

[Note ]:

(1) For more information, see "Console Screen Buffers ".

(2) the above three colors (red, green, and blue) can be mixed into other colors by "coloring", as shown in:

     

Console terminal output color

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.