Point color to the terminal (C and Golang versions)

Source: Internet
Author: User

If you have used the Nmon tool under Aix or Linux, you should notice that it can display text in a variety of colors and skillfully combine colors and characters to make a variety of shapes.

To display the operation of the system. Take a look at this picture attached to it:

If you look at its source code, you will find that it uses the ncurses library to achieve color display. Ncurses is powerful and can run on any UNIX system that follows the Ansi/posix standard,

Contains a range of powerful features. But here, it's not the object I'm going to study, it's too heavyweight.

What I want to illustrate here is a simple way for the terminal to display color text. Using the 16-binary color control is enough to print out a variety of colors at the terminal. This method does not have to call

API, the use of the way is also very simple, I use C language to demonstrate under:

#include <stdio.h>int main (void) {printf ("\x1b[0;%dmhello World 30: Black \n\x1b[0m", 30);  printf ("\x1b[0;%dmhello World 31: Red \n\x1b[0m", 31);  printf ("\x1b[0;%dmhello World 32: Green \n\x1b[0m", 32);  printf ("\x1b[0;%dmhello World 33: Yellow \n\x1b[0m", 33);  printf ("\x1b[0;%dmhello World 34: Blue \n\x1b[0m", 34);  printf ("\x1b[0;%dmhello World 35: Violet \n\x1b[0m", 35);  printf ("\x1b[0;%dmhello World 36: Dark green \n\x1b[0m", 36);  printf ("\x1b[0;%dmhello World 37: White \n\x1b[0m", 37);  printf ("\x1b[%d;%dmhello World \x1b[0m 47: White 30: Black \ n", 47,30);  printf ("\x1b[%d;%dmhello World \x1b[0m 46: Dark green 31: Red \ n", 46,31);  printf ("\x1b[%d;%dmhello World \x1b[0m 45: Violet 32: Green \ n", 45,32);  printf ("\x1b[%d;%dmhello World \x1b[0m 44: Blue 33: Yellow \ n", 44,33);  printf ("\x1b[%d;%dmhello World \x1b[0m 43: Yellow 34: Blue \ n", 43,34);  printf ("\x1b[%d;%dmhello World \x1b[0m 42: Green 35: Violet \ n", 42,35);  printf ("\x1b[%d;%dmhello World \x1b[0m 41: Red 36: Dark green \ \", 41,36); printf ("\x1b[%d;%dmhello World \x1b[0m 40: Black 37: white \ n", 40,37);} 
You can see that there is only one printf function plus some strange characters, but look at the effect:

You can see that by passing in the appropriate numbers, you can control the color of the text and the background color, but there is a limit to show only the colors listed above. Where the text color is encoded

From 30 to 37, the background color is encoded from 40 to 47.

Knowing this technique, it's easy to wrap it in Golang, because it's essentially a print. I encapsulated one on GitHub.

URL is: Https://github.com/xcltapestry/xclpkg/tree/master/clcolor

Example of Golang invocation:

Package Mainimport ("FMT" "Github.com/xclpkg/clcolor") func main () {FMT. Println (Clcolor. Black ("Black ()")) Fmt. Println (Clcolor. Red ("Red ()")) Fmt. Println (Clcolor. ("Green ()")) Fmt. Println (Clcolor. Yellow ("Yellow ()")) Fmt. Println (Clcolor. Blue ("Blue ()")) Fmt. Println (Clcolor. Magenta ("Magenta ()")) Fmt. Println (Clcolor. Cyan ("Cyan ()")) Fmt. Println (Clcolor. White ("White ()"))}
Operating effect:

You can use this package to easily set its text display color, can be used in such cases as a log or some sentence highlighting.

Another thing to note, use this method to set the color display, if you are using the CRT or SSH and other terminals to log in to view, remember "Enable ANSI color", otherwise it will not be effective.

Now that Unix/linux has this way of displaying colors in the terminal, can windows do the same in this way?

In fact, there are:

The corresponding API functions are also available:

HANDLE GetStdHandle (DWORD nstdhandle);
Parameters are: Std_input_handle,std_output_handle and Std_error_handle.

The handle to the console output device can be obtained by passing in the Std_output_handle.

Setting properties for Console devices
BOOL Setconsoletextattribute (HANDLE hconsoleoutput,word wattributes);
The first parameter is console standard input, the standard output, or the device handle of the error
The second parameter is used for the properties of the device device, which describes in detail the parameters that represent the color.

As you can see, Windows uses the above two functions. But I'm less likely to look at the logs in the Windows console, which is not a description or encapsulation.

MAIL: [Email protected]

blog:http://blog.csdn.net/xcl168


Point color to the terminal (C and Golang versions)

Related Article

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.