preface

In computers, ANSI escape codes are a way to use in-band signals to control formatting, color, or other output options in a video stream or text terminal. To encode this formatting information is to embed the ANSI code described above into this text in a defined sequence of bytes. Instead of thinking of it as a simple character code (ASCII), the terminal will look for commands to interpret these characters.

ANSI code was introduced in 1970, but was not ubiquitous in minicomputers and mainframe rooms until the early 1980s. It was used in the early bulletin boards, and it improved the display compared to the earlier systems that lacked cursor movement, which led to its widespread use.

Despite hardware text terminals in the 21st century have become increasingly rare. But do not shake the influence of ANSI standards. Because most of the text simulator interpretation work, at least a significant portion of the text still has ANSI escape sequences. The only exception is Microsoft’s Win32 Console. These have been fixed since Microsoft upgraded to Windows 10.

So this ancient protocol still has an important influence on our current text output.

A little command

Echo -e "\033[1; 32mi ♡ You \e[0m"Copy the code

output:

I don’t want to document anything, do I? This command can be used as a simple confession. It’s for everyone. From here we have introduced ANSI’s influence on the output color of text, see, the color of the characters in the diagram has changed. And with that property we’re going to share today how to write a color log of days.

CSI

The escape sequence starts with the ESC control character, and for 2-character sequences, the second character is ASCII 64 through 95. (@ to _, as well as all uppercase letters and []\^), however, most sequences are more than 2 characters long and start with an ESC control character and an open middle bracket. A sequence is called CSI, short for Control Sequence guide or Control sequence initiator. The last character in this sequence is in the ASCII range 64 to 126. There is also a single-character CSI (155/0x9B/0233) ESC[, which is used more than the single-character form, see C0 and C1 control codes for details. On utF-8 encoded terminals, both forms use 2 bytes (CSI in UTF-8 is 0xC2, 0x9B), but the ESC[sequence is more straightforward.

Text color

Text colors (and SGR (Select Graphic Rendition) parameters) are processed using CSI n1 [;n2 [;…]] m sequences, as shown above, for each n1, n2,… Is an SGR parameter. So, for example, you use 30 to 37 for the foreground color and 40 to 47 for the background color. Below is a color transition diagram.

Start coding

package util import ( "fmt" "time" ) const ( color_red = uint8(iota + 91) color_green color_yellow color_blue Color_magenta // magenta info = "[info]" trac = "[trac]" erro = "[erro]" warn = "[warn]" succ = "[succ]") // see complete color rules in document in https://en.wikipedia.org/wiki/ANSI_escape_code#cite_note-ecma48-13 func Trace(format string, a ... interface{}) { prefix := yellow(trac) fmt.Println(formatLog(prefix), fmt.Sprintf(format, a...) ) } func Info(format string, a ... interface{}) { prefix := blue(info) fmt.Println(formatLog(prefix), fmt.Sprintf(format, a...) ) } func Success(format string, a ... interface{}) { prefix := green(succ) fmt.Println(formatLog(prefix), fmt.Sprintf(format, a...) ) } func Warning(format string, a ... interface{}) { prefix := magenta(warn) fmt.Println(formatLog(prefix), fmt.Sprintf(format, a...) ) } func Error(format string, a ... interface{}) { prefix := red(erro) fmt.Println(formatLog(prefix), fmt.Sprintf(format, a...) ) } func red(s string) string { return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_red, s) } func green(s string) string { return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_green, s) } func yellow(s string) string { return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_yellow, s) } func blue(s string) string { return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_blue, s) } func magenta(s string) string { return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_magenta, s) } func formatLog(prefix string) string { return time.Now().Format("2006/01/02 15:04:05") + " " + prefix + " " }Copy the code

Above is a complete color log code

Output effect:

The complete code

If you need to test locally, make sure you set up the right GO development environment and down github.com/liyu4/chill the project. Find the util unit

Yourpath refers to your project path. cd yourpath/chill/util go test -vCopy the code

Afterword.

In which \x1b[implement CSI: If the color is black, use \x1b[30m to convert to red, use \x1b[31m if the bold parameter is used, write \x1b[30;1m to get red bold, use \x1b[31;1m to reset the color to the default, use \x1b[39; 49M (or reset all attributes with \x1b[0m) \033[0m reset to normal \033[1m set high brightness or bold \033[4m underline \033[5m flicker \033[7m invert \033[8m blanking \033[30m — /33[37m set foreground color \033[40m — /33[47m set background color control ESC common representations \e, \x1b(\ x1b), \033 can all refer to \e for Escape, corresponding to octal \033, corresponding to hexadecimal \x1b

Reference documentation

ANSI Escape sequence Chinese documentation