Set the color and display of Linux terminal output via printf

Source: Internet
Author: User
Tags clear screen echo command

Preface

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.

This article briefly introduces how to change the color and display of the terminal output through printf in C language. The code involved in this article is run in the following environment:

Body

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). For example, execute the following command in the Linux shell:

That is, set the output to a red font (31) and a white background (47). The option '-e ' is a parser that activates special characters for the echo command.

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; foreground color; background colour m output string \033[0m

Or

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

Where ' \033[0m ' is used to restore the default terminal output properties, otherwise it will affect subsequent output.

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

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

1 #defineNone "\e[0m"2 #defineBLACK "\e[0;30m"3 #defineL_black "\e[1;30m"4 #defineRED "\e[0;31m"5 #defineL_red "\e[1;31m"6 #defineGREEN "\e[0;32m"7 #defineL_green "\e[1;32m"8 #defineBROWN "\e[0;33m"9 #defineYELLOW "\e[1;33m"Ten #defineBLUE "\e[0;34m" One #defineL_blue "\e[1;34m" A #definePURPLE "\e[0;35m" - #defineL_purple "\e[1;35m" - #defineCYAN "\e[0;36m" the #defineL_cyan "\e[1;36m" - #defineGRAY "\e[0;37m" - #defineWhite "\e[1;37m" -  + #defineBOLD "\e[1m" - #defineUnderline "\e[4m" + #defineBLINK "\e[5m" A #defineREVERSE "\e[7m" at #defineHIDE "\e[8m" - #defineCLEAR "\E[2J"

Write The test code validates the effect of escape sequence control:

1 intMainvoid)2 {3printf"This is a character control test!\n" );4Sleep3);5printf"[%2u]"CLEAR"clear\n"NONE, __line__);6 7printf"[%2u]"BLACK"BLACK"L_black"l_black\n"NONE, __line__);8printf"[%2u]"RED"RED"L_red"l_red\n"NONE, __line__);9printf"[%2u]"GREEN"GREEN"L_green"l_green\n"NONE, __line__);Tenprintf"[%2u]"BROWN"BROWN"YELLOW"yellow\n"NONE, __line__); Oneprintf"[%2u]"BLUE"BLUE"L_blue"l_blue\n"NONE, __line__); Aprintf"[%2u]"PURPLE"PURPLE"L_purple"l_purple\n"NONE, __line__); -printf"[%2u]"CYAN"CYAN"L_cyan"l_cyan\n"NONE, __line__); -printf"[%2u]"GRAY"GRAY"White"white\n"NONE, __line__); the  -printf"[%2u]\e[1;31;40m Red \e[0m\n", __line__); -  -printf"[%2u]"BOLD"bold\n"NONE, __line__); +printf"[%2u]"Underline"underline\n"NONE, __line__); -printf"[%2u]"BLINK"blink\n"NONE, __line__); +printf"[%2u]"REVERSE"reverse\n"NONE, __line__); Aprintf"[%2u]"HIDE"hide\n"NONE, __line__); at  -printf"Cursor Test begins!\n" ); -printf"=======!\n" ); -SleepTen); -printf"[%2u]" "\e[2acursor up 2 lines\n"NONE, __line__); -SleepTen); inprintf"[%2u]" "\e[2bcursor down 2 lines\n"NONE, __line__); -Sleep5); toprintf"[%2u]" "\e[?25lcursor hide\n"NONE, __line__); +Sleep5); -printf"[%2u]" "\e[?25hcursor display\n"NONE, __line__); theSleep5); *  $printf"Test ends!\n" );Panax NotoginsengSleep3); -printf"[%2u]" "\e[2acursor up 2 lines\n"NONE, __line__); theSleep5); +printf"[%2u]" "\e[kclear from cursor downward\n"NONE, __line__); A  the     return 0 ; +}

The results of the implementation are as follows:

Due to limitations, the effects such as flicker and cursor movement are not displayed.

Note that the color characters are displayed correctly under the Linux terminal. However, if you ssh into the Linux host via Windows tools such as SECURECRT, you will need to set up the tools.

Take SecureCRT 6.6 As an example, select ANSI or Linux or xterm in the Terminal type of Options->session options->terminal->emulation page, and check ANSI Color.

Then, in the Appearance page, select the color scheme for windows or traditional. To highlight the output color in the test code, select the Windows scenario here (you need to tick the use color scheme on the emulation page):

Set the color and display of Linux terminal output via printf

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.