Printf output color and ANSI control code (highlighted, underlined, flickering, cursor position, clear screen, etc)

Source: Internet
Author: User

Printf output color and ANSI control code (highlighted, underlined, flickering, cursor position, clear screen, etc)
Adding special effects such as colors to the printf output can make the printed information clearer and be particularly useful in debugging.

Color:

# Define none "/033 [M"

# Define red "/033 [0; 32; 31 m"

# Define light_red "/033 [1; 31 m"

# Define green "/033 [0; 32; 32 m"

# Define light_green "/033 [1; 32 m"

# Define blue "/033 [0; 32; 34 m"

# Define light_blue "/033 [1; 34 m"

# Define dary_gray "/033 [1; 30 m"

# Define cyan "/033 [0; 36 m"

# Define light_cyan "/033 [1; 36 m"

# Define purple "/033 [0; 35 m"

# Define light_purple "/033 [1; 35 m"

# Define Brown "/033 [0; 33 m"

# Define yellow "/033 [1; 33 m"

# Define light_gray "/033 [0; 37 m"

# Define white "/033 [1; 37 m"

For example:

Printf ("/033 [31 m ####----- & gt;/033 [32 m" "Hello/N" "/033 [M ")

Int main ()

{

Printf (cyan "current function is % s" green "file line is % d/N" none,

_ FUNCTION __, _ line __);

Fprintf (stderr, red "current function is % s" blue "file line is % d/N" none,

_ FUNCTION __, _ line __);

Return 0;

}

The color is divided into the background color and font color, 30 ~ 39 is used to set the font color, 40 ~ 49. Set the background:

Background Color font color

40: Black 30: Black

41: Red 31: red

42: Green 32: Green

43: yellow 33: Yellow

44: Blue 34: Blue

45: Purple 35: Purple

46: dark green 36: dark green

47: White 37: white

Remember to restore the color to none after printing, otherwise the subsequent printing will change color.

In addition, you can add some ANSI control codes. Color addition is only one of the following control codes:

/033 [0 m close all attributes

/033 [1 m set high brightness

/033 [4 m underline

/033 [5 m flashing

/033 [7 m reverse display

/033 [8 m blanking

/033 [30 m --/033 [37 m sets the foreground color

/033 [40 m --/033 [47 m set the background color

/033 [move the NA cursor up n rows

/033 [move the Nb cursor down n rows

/033 [the NC cursor shifts n rows right

/033 [Nd cursor shifts n rows left

/033 [Y; XH: Set the cursor position

/033 [2j clear screen

/033 [k clear content from the cursor to the end of the line

/033 [s Save the cursor position

/033 [U restore cursor position

/033 [? 25l hide the cursor

/033 [? 25 h show cursor

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.