Linux terminal and Win32 console text color output

Source: Internet
Author: User

When connecting Linux systems with terminal emulators such as Putty, SECURECRT, Xshell and so on, the outputs of LS, vim and other tools contain various colors, and the output of these colors greatly enhances the readability of the text.

Usually we can use the echo command with the-e option to output text of various colors, for example: Echo-e "\033[31mred text\033[0m", which can output a red font "redtext". Where: "\033[31m" and "\033[0m" are ANSI escape sequences (ANSI escape code/sequence), which controls the format, color, etc. of text output, and most Unix-like terminal emulators can interpret ANSI escape sequences.

1. ANSI Color sequence format

The general escape sequence format for controlling text color is as follows:

CSI N1 [; N2 [; ...]] m

Where CSI is called "Control Sequence Introducer/initiator", which is the "\033[" in the above example (where \033 is the ASCII code (octal) corresponding to the ESC key in the upper-left corner of your keyboard); N1, N2 represents the SGR parameter (some common SGR parameters are listed below) to control the text output format such as color, bold, italic, blinking, and M for the end of the escape sequence.

Note: The \033 is the ASCII code (8 binary) for the ESC key in the upper-left corner of the keyboard, the same as \033, \x1b, and \e, for example: Echo-e "\x1b[31mred text\e[0m" also outputs the red font "red Text".

The common SRG parameter list is as follows:

Coding

Description

0

Close all formats, revert to initial state

1

Bold/Highlight

2

Blur ()

3

Italic ()

4

Underline (single line)

5

Flashing (slow)

6

Flashing (FAST) ()

7

Swap background and foreground colors

8

Hide (pitch, can't see anything) ()

30-37

foreground color, that is, 30+x,x represents different colors (see "Color table" below)

40-47

The background color, or 40+x,x, represents a different color (see "Color table" below)

Note: (1) The encoding that contains (※) callout indicates that not all terminal emulators are supported, only a few emulators are supported.

(2) Multiple SGR parameters can be used together, for example: Echo-e "\x1b[31;4mred underline text\e[0m" output red underline font "Red underline Text".

(3) Please refer to "ANSI Escape code" For more information about the parameters.

Color table:

color value x

0

1

2

3

4

5

6

7

color

black

red

green

yellow

blue

Violet

cyan

white

2. Example of output color text for various languages

In addition to the shell commands to output colors, other languages can also use the above ANSI escape sequence output color (provided that you use the terminal emulator to resolve the ANSI escape sequence), the following gives examples of output color text in several major languages.

Note: The following programs are only valid for Unix-like terminals, and the Win32 console does not support ANSI escape sequences and is therefore invalid.

2.1 C Program Output Color example
1 // hello.c 2 #include <stdio.h>3int  main () {4     printf ("  \033[31;4mred underline text\033[0m\n"); 5 }

Compiling: gcc hello.c

Run:./a.out

Output:Red underline Text

Example of output color in 2.2 C + + programs
1 // Hello.cpp 2 #include <iostream>3int  main () {4     "\033[ 31;4mred underline text\033[0m" << Std::endl; 5 }

Compilation: g++ hello.cpp

Run:./a.out

Output:Red underline Text

2.3 Output Color examples in Java programs
1 // Hello.java 2 class Hello {3      Public Static void Main (string[] args) {4         System.out.println ("\033[31;4mred underline text\033[0m"); 5     }6 }

Note: \e and \0x1b are not recognized in Java and can only be used with \033.

Compilation: Javac Hello.java

Run: Java Hello

Output:Red underline Text

Example of output color in 2.4 python program
1 # hello.py 2 Print " \033[31;4mred underline text\033[0m"

Note: \e is not recognized in Python (v2.6.5) and can be used with \033 and \x1b.

Run: Python hello.py

Output:Red underline Text

3. Win32 Console Program output color

Because the Win32 console does not support ANSI escape sequences, it is relatively complex to make the Win32 console output color text.

3.1/C + + Windows Program Console output Color

Using the Setconsoletextattribute and GetStdHandle two Win32 API functions to output colors, examples are as follows:

1#include <stdio.h>2#include <windows.h>3 4 intMain () {5HANDLE HDL =GetStdHandle (std_output_handle);6Setconsoletextattribute (HDL, foreground_red |foreground_intensity);7printf"Hello");8Setconsoletextattribute (HDL, Foreground_green |foreground_intensity);9printf"world!\n");Ten  One GetChar (); A}

The two API prototypes used are as follows:

1 HANDLE GetStdHandle (DWORD nstdhandle); 2 BOOL setconsoletextattribute (HANDLE hconsoleoutput, WORD wattributes);

Where GetStdHandle is used to get the screen buffer handle (the handle to the standard output is obtained in the example), Setconsoletextattribute is used to set the console Text property (set the foreground color to red when the first call is set to green highlighting on the second call), So the output is "Hello world!".

The meaning of the GetStdHandle interface parameter Nstdhandle and the interface return value is as follows:

VALUES ( Nstdhandle )

Meaning

Std_input_handle

Returns a handle to the standard input

Std_output_handle

Returns a handle to the standard output

Std_error_handle

Returns a handle to a standard error

Note: Please refer to "GetStdHandle" for more information.

Setconsoletextattribute () The first parameter hconsoleoutput is the screen buffer handle (which can be obtained through GetStdHandle ()), and the second parameter wattributes as a color property. Common color attribute values are as follows (can be combined):

Properties (Wattributes )

Description

Foreground_blue

Foreground color (blue)

Foreground_green

Foreground color (green)

Foreground_red

Foreground color (red)

Foreground_intensity

Foreground color highlighting

Background_blue

Background color (blue)

Background_green

Background color (green)

Background_red

Background color (red)

Background_intensity

Background color highlighting

Note: Please refer to "Console screen buffers" for more information.

3.2 Command Prompt to downgrade full screen color

Command Prompt (CMD) to downgrade the full screen color output you can use the color command in the following format:

Color BF

where B and f represent the color attribute (b background color, f foreground color), and the attribute value is a hexadecimal number (0-f). The relationship between the attribute values and the color is as follows:

0

2

4

6

red

yellow

8

a

c

e

TD valign= "Top" width= "> TD valign= "Top" width= "> TD valign= "Top" width= "> TD valign= "Top" width= "> TD valign= "Top" width= "> TD valign= "Top" width= "> TD valign= "Top" width= "> TD valign= "Top" width= "> TD valign= "Top" width= "> TD valign= "Top" width= ">

property value

1

3

5

7

color

black

blue

green

Aqua

Violet

white

property value

9

b

d

f

Color

Gray

Light

Light Green

Freshwater Green

Pink

Light Purple

Yellowish

Bright White

For example, to set the command Prompt window, "Background color is green, foreground color is blue" executable "color 21".

3.3 PowerShell under output character color

Reference to: PowerShell output character color

3.4 Python binding Windows interface Output Color rendering

See: Python implementation modifies the Windows CMD command line output color (full parsing)

Linux terminal and Win32 console text color output

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.