forked from hound-search/hound
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathansi.go
105 lines (86 loc) · 1.58 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package ansi
import (
"fmt"
"os"
)
var (
start = "\033["
reset = "\033[0m"
bold = "1;"
blink = "5;"
underline = "4;"
inverse = "7;"
)
type Style byte
const (
Normal Style = 0x00
Bold Style = 0x01
Blink Style = 0x02
Underline Style = 0x04
Invert Style = 0x08
Intense Style = 0x10
)
type Color int
const (
Black Color = iota
Red
Green
Yellow
Blue
Magenta
Cyan
White
Colorless
)
const (
normalFg = 30
intenseFg = 90
normalBg = 40
intenseBg = 100
)
type Colorer struct {
enabled bool
}
func NewFor(f *os.File) *Colorer {
return &Colorer{isTTY(f.Fd())}
}
func (c *Colorer) Fg(s string, color Color, style Style) string {
return c.FgBg(s, color, style, Colorless, Normal)
}
func (c *Colorer) FgBg(s string, fgColor Color, fgStyle Style, bgColor Color, bgStyle Style) string {
if !c.enabled {
return s
}
buf := make([]byte, 0, 24)
buf = append(buf, start...)
if fgStyle&Bold != 0 {
buf = append(buf, bold...)
}
if fgStyle&Blink != 0 {
buf = append(buf, blink...)
}
if fgStyle&Underline != 0 {
buf = append(buf, underline...)
}
if fgStyle&Invert != 0 {
buf = append(buf, inverse...)
}
var fgBase int
if fgStyle&Intense == 0 {
fgBase = normalFg
} else {
fgBase = intenseFg
}
buf = append(buf, fmt.Sprintf("%d;", fgBase+int(fgColor))...)
if bgColor != Colorless {
var bgBase int
if bgStyle&Intense == 0 {
bgBase = normalBg
} else {
bgBase = intenseBg
}
buf = append(buf, fmt.Sprintf("%d;", bgBase+int(bgColor))...)
}
buf = append(buf[:len(buf)-1], "m"...)
return string(buf) + s + reset
}