// Red is a convenient helper function to print with red foreground. A // newline is appended to format by default. funcRed(format string, a ...interface{}) { colorPrint(format, FgRed, a...) }
// 核心代码 funccolorPrint(format string, p Attribute, a ...interface{}) { c := getCachedColor(p)
if !strings.HasSuffix(format, "\n") { format += "\n" }
// Print formats using the default formats for its operands and writes to // standard output. Spaces are added between operands when neither is a // string. It returns the number of bytes written and any write error // encountered. This is the standard fmt.Print() method wrapped with the given // color. func(c *Color) Print(a ...interface{}) (n int, err error) { c.Set() // 带颜色字体的固定格式的前半部分 defer c.unset() // 带颜色字体的固定格式的后半部分 // 具体要打印的具体字符 return fmt.Fprint(Output, a...) }
const escape = "\x1b"
//----------------------------------- // 固定格式的前半部分 // Set sets the SGR sequence. func(c *Color) Set() *Color { if c.isNoColorSet() { return c }