-
Notifications
You must be signed in to change notification settings - Fork 22
/
ct_ansi.go
51 lines (45 loc) · 880 Bytes
/
ct_ansi.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// +build !windows
package ct
import (
"fmt"
"os"
"strconv"
)
func isDumbTerm() bool {
return os.Getenv("TERM") == "dumb"
}
func resetColor() {
if isDumbTerm() {
return
}
fmt.Fprint(Writer, "\x1b[0m")
}
func ansiText(fg Color, fgBright bool, bg Color, bgBright bool) string {
if fg == None && bg == None {
return ""
}
s := []byte("\x1b[0")
if fg != None {
s = strconv.AppendUint(append(s, ";"...), 30+(uint64)(fg-Black), 10)
if fgBright {
s = append(s, ";1"...)
}
}
if bg != None {
s = strconv.AppendUint(append(s, ";"...), 40+(uint64)(bg-Black), 10)
if bgBright {
s = append(s, ";1"...)
}
}
s = append(s, "m"...)
return string(s)
}
func changeColor(fg Color, fgBright bool, bg Color, bgBright bool) {
if isDumbTerm() {
return
}
if fg == None && bg == None {
return
}
fmt.Fprint(Writer, ansiText(fg, fgBright, bg, bgBright))
}