Printf output font color

Source: Internet
Author: User

Printf output font color favorites
Sometimes, in order to enable the font color and generate a simple flickering function when the terminal outputs, close up the following code
Implements the hello World color output, and generates a flickering function every second.
(Note: You must determine the terminal type)

Code:

# Include <stdio. h>
# Include <unistd. h>
# Include <signal. h>
# Include <stdlib. h>

Int flag = 0;

Void sig_int (int signum)
{
If (flag = 1)
Fprintf (stderr, "/033 [47; 31 mhello world/033 [5 m ");
Fprintf (stderr, "/033 [;/033 [0 m ");
Fprintf (stderr, "/033 [;/033 [0 m ");
Printf ("/N ");
Exit (0 );
}

Int main ()
{
Int I = 0;
Struct sigaction sig_act;
Sig_act.sa_handler = sig_int;
Sigemptyset (& sig_act.sa_mask );
Sig_act.sa_flags = 0;
If (sigaction (SIGINT, & sig_act, null) <0)
{
Fprintf (stderr, "signal error/N ");
Exit (1 );
}
While (I ++ <10)
{
Fprintf (stderr, "/033 [;/033 [s ");
Fprintf (stderr, "/033 [47; 31 mhello world/033 [5 m ");
Flag = 0;
Sleep (1 );
Fprintf (stderr, "/033 [;/033 [U ");
Fprintf (stderr, "/033 [;/033 [K ");
Flag = 1;
Sleep (1 );
}

Fprintf (stderr, "/033 [47; 31 mhello world/033 [5 m ");
Fprintf (stderr, "/033 [;/033 [0 m ");
Printf ("/N ");
Return 0;
}

This program outputs Hello world 10 times and then exits. A signal is used to restore the color of the default terminal output.
In addition, let's talk about how to output the color and color code.

Format:

 

Code:

Printf ("/033 [text background color; font color M string/033 [0 m ");

Printf ("/033 [47; 31 mhello world/033 [5 m ");
47 is the color of the background, 31 is the color of the font, and hello World is the string./033 [5 m is the control code.

Color Code:

 

Quote:

Color Range: 40--49 color: 30--39
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

ANSI control code:

 

Quote:

/033 [0 m close all attributes
/033 [1 m set high brightness
/03 [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
/03 [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
/33 [? 25 h show cursor

In this way, dynamic output can be achieved in some cases.

Site: http://blog.sina.com.cn/s/blog_4c12aff701008284.html

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/andydung/archive/2010/04/08/5464110.aspx

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.