Linux Terminal output color

Source: Internet
Author: User
Tags clear screen

When you debug a program under a Linux terminal, you sometimes need to output a lot of information. If you can control the color and display of the font, you can make the output information sharp contrast, easy to observe the data during debugging.

The character color of the terminal is controlled by escape sequence (escape Sequence), which is the system display function in text mode, regardless of the specific language.

The escape sequence begins with the control character ' ESC '. The ASCII decimal representation of the character is 27, hexadecimal is 0x1b, and octal is represented as 033. Most escape sequences are more than two characters long and are usually preceded by ' ESC ' and ' open parenthesis ' ['. The starting sequence is called the Control sequence Guide (Csi,control Sequence Intro) and is usually replaced by ' \033[' or ' \e['.

When you set terminal Display properties by escape sequences, you can use the following format:

\033[Param {; Param, ...} M or \e[Param {; Param, ...} M

Where the ' \033[' or ' \e[' boot escape sequence, ' m ' represents the Set property and ends the escape sequence. Param is a property value, {...} Indicates optional (multiple parameters are separated by semicolons, regardless of order).

Note that escape sequences can be controlled by the character ' can ' (Cancel) and ' SUB ' (substitute) interrupts.

The common parameters associated with escape sequences are as follows (more parameter descriptions can be viewed through the man console_codes command):

Display: 0 (default), 1 (bold/highlighted), 22 (non-Bold), 4 (single underline), 24 (no underscore), 5 (blinking), 25 (no flicker), 7 (invert, flip foreground and background), 27 (no echo)

Colors: 0 (Black), 1 (red), 2 (green), 3 (yellow), 4 (blue), 5 (magenta), 6 (cyan), 7 (white)

The foreground color is the value of the 40+, such as 31, which indicates a red color, and a background color of the value, such as 41 for the background color red.

The color effect is as follows:

Therefore, when you set the terminal display property by an escape sequence, the common format is:

\033[display mode; background color m output string \033[0m or \e[display; foreground color m output string \033[0m

In addition, there are also ANSI control codes, such as NA (the cursor moves n rows), NB (the cursor moves down n lines), NC (the cursor moves n rows), ND (the cursor shifts n rows), 2J (Clear screen), K (clears the contents from the cursor to the end of the line), s (save cursor position), U (Restore cursor position 25l (     Hide cursor),? 25l (display cursor). Where ' \033[0m ' is used to restore the default terminal output properties, otherwise it will affect subsequent output.

Based on common parameters, you can define a single control macro for the printf series of statements:

1 #define NONE "\e[0m" 2 #define BLACK "\e[0;30m" 3 #define L_black "\e[1;30m" 4 #define RED "\e[0;31m" 5 #define l_red "\e[1;31m" 6 #define GREEN "\E[0; 32m "7 #define L_green" \e[1;32m "8 #define BROWN" \e[0;33m "9 #define YELLOW" \               e[1;33m "Ten #define BLUE" \e[0;34m "one #define L_blue" \e[1;34m "#define PURPLE               "\e[0;35m" #define L_purple "\e[1;35m" #define CYAN "\e[0;36m" #define L_cyan                 "\e[1;36m" #define GRAY "\e[0;37m" #define WHITE "\e[1;37m" #define BOLD              "\e[1m" #define underline "\e[4m" #define BLINK "\e[5m" #define REVERSE "\e[7m" #define HIDE "\e[8m" #define CLEAR "\e[2j"
#define Clrline "\r\e[k"//or "\e[1k\r"

Write test code to verify the effect of escape sequence control:

 1 int main (void) 2 {3 printf ("This is a character control test!\n"); 4 sleep (3); 5 printf ("[%2u]" "CLEAR" CL ear\n "NONE, __line__); 6 7 printf ("[%2u]" BLACK "black" L_black "l_black\n" NONE, __line__); 8 printf ("[%2u]" red "red" l_red "l_red\n" NONE, __line__); 9 printf ("[%2u]" green "green" L_green "l_green\n" NONE, __line__); printf ("[%2u]" Brown "Brown" YELLOW "Yello" W\n "None, __line__); printf (" [%2u] "blue" blue "L_blue" l_blue\n "none, __line__); printf (" [%2u] "PURPLE" PU Rple "L_purple" l_purple\n "none, __line__); printf (" [%2u] "CYAN" CYAN "L_cyan" l_cyan\n "none, __line__); Rintf ("[%2u]" gray "gray" white "white\n" NONE, __line__); printf ("[%2u]\e[1;31;40m Red \e[0m\n", __line__); 17 printf ("[%2u]" BOLD "bold\n" None, __line__), printf ("[%2u]" underline "underline\n" none, __line__); p Rintf ("[%2u]" BLINK "blink\n" none, __line__); printf ("[%2u]" REVERSE "reverse\n" None, __line__); printf ("[%2u]" HIDE "hide\n" NONE, __line__), printf ("Cursor test begins!\n"); printf ("=======!\n" ); ("[%2u]" "\e[2acursor up 2 lines\n" NONE, __line__); printf ("[%2u]"); "\e[2bcursor down 2 lines\n" none, __line__); sleep (5); printf ("[%2u]" "\e[?25lcursor hide\n" None, __line__ (5), ("[%2u]", "\e[?25hcursor display\n" NONE, __line__), Sleep (5), and printf ("Test E Nds!\n "), Notoginseng Sleep (3), (" [%2u] "," \e[2acursor up 2 lines\n "NONE, __line__), and (5); [%2u] "" \e[kclear from Cursor downward\n "NONE, __line__); return 0; 43}

The results of the implementation are as follows:

Linux 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.