-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcolors_unix.go
More file actions
36 lines (27 loc) · 890 Bytes
/
colors_unix.go
File metadata and controls
36 lines (27 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// +build linux darwin
package cmd
import "fmt"
const colorStringEscape = "\033[3%dm"
// Choose an ANSI color by converting a string to an int.
//
// Color 5, magenta, reserved for controller log messages.
//
// Colors 0 and 7, black and white, are skipped because they are likely to be unreadable
// against terminal backgrounds. Instead, 9, the default text color is used, likely to be the color
// of the two that is readable against the terminal background.
func chooseColor(input string) string {
if input == "INFO" {
return fmt.Sprintf(colorStringEscape, 5)
}
var sum uint8
for _, char := range []byte(input) {
sum += uint8(char)
}
// Eight possible terminal colors, but Black and White are excluded
color := (sum % 6) + 1
// Color 5 is reserved, replace it with default text color.
if color == 5 {
color = 9
}
return fmt.Sprintf(colorStringEscape, color)
}