github.com/tencent/goom@v1.0.1/internal/logger/color.go (about) 1 package logger 2 3 import ( 4 "bufio" 5 "fmt" 6 "strings" 7 ) 8 9 // Foreground colors. 10 const ( 11 None Color = iota + 29 12 Black 13 Red 14 Green 15 Yellow 16 Blue 17 Magenta 18 Cyan 19 White 20 ) 21 22 // Color represents a text color. 23 type Color uint8 24 25 // Add adds the coloring to the given string. 26 func (c Color) Add(s string) string { 27 if c == None { 28 return s 29 } 30 return fmt.Sprintf("\x1b[%dm%s\x1b[0m", uint8(c), s) 31 } 32 33 // AddAll adds the coloring to the given string of all line. 34 func (c Color) AddAll(s string) string { 35 if c == None { 36 return s 37 } 38 lines := make([]string, 0, 10) 39 scanner := bufio.NewScanner(strings.NewReader(s)) 40 for scanner.Scan() { 41 lines = append(lines, c.Add(scanner.Text())) 42 } 43 return strings.Join(lines, "\n") 44 }