-
Notifications
You must be signed in to change notification settings - Fork 3
/
color.go
128 lines (111 loc) · 2.57 KB
/
color.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package readline
import (
"fmt"
"io"
)
type Coloring interface {
// Reset has to initialize receiver's fields and return default color.
Init() ColorSequence
// Next has to return color for the given rune.
Next(rune) ColorSequence
}
type ColorSequence int64
const (
colorCodeBitSize = 8
colorCodeMask = (1<<colorCodeBitSize - 1)
)
func (c ColorSequence) Add(value int) ColorSequence {
n := (c & colorCodeMask) + 1
return ((c &^ colorCodeMask) | n) | (ColorSequence(value) << (n * colorCodeBitSize))
}
func (c ColorSequence) Chain(value ColorSequence) ColorSequence {
for n := (value & colorCodeMask); n > 0; n-- {
value >>= colorCodeBitSize
c = c.Add(int(value & colorCodeMask))
}
return c
}
const (
ColorReset ColorSequence = 1
)
const (
Black ColorSequence = 3 | ((30 + iota) << colorCodeBitSize) | (49 << (colorCodeBitSize * 2)) | (1 << (colorCodeBitSize * 3))
Red
Green
Yellow
Blue
Magenta
Cyan
White
_
DefaultForeGroundColor
)
const (
DarkGray ColorSequence = 3 | ((30 + iota) << colorCodeBitSize) | (22 << (colorCodeBitSize * 2)) | (49 << (colorCodeBitSize * 3))
DarkRed
DarkGree
DarkYellow
DarkBlue
DarkMagenta
DarkCyan
DarkWhite
_
DarkDefaultForeGroundColor
)
type _MonoChrome struct{}
func (_MonoChrome) Init() ColorSequence {
return DefaultForeGroundColor
}
func (_MonoChrome) Next(rune) ColorSequence {
return DefaultForeGroundColor
}
func SGR1(n1 int) ColorSequence {
return ColorSequence(1) |
(ColorSequence(n1) << colorCodeBitSize)
}
func SGR2(n1, n2 int) ColorSequence {
return ColorSequence(2) |
(ColorSequence(n1) << colorCodeBitSize) |
(ColorSequence(n2) << (colorCodeBitSize * 2))
}
func SGR3(n1, n2, n3 int) ColorSequence {
return ColorSequence(3) |
(ColorSequence(n1) << colorCodeBitSize) |
(ColorSequence(n2) << (colorCodeBitSize * 2)) |
(ColorSequence(n3) << (colorCodeBitSize * 3))
}
func SGR4(n1, n2, n3, n4 int) ColorSequence {
return ColorSequence(4) |
(ColorSequence(n1) << colorCodeBitSize) |
(ColorSequence(n2) << (colorCodeBitSize * 2)) |
(ColorSequence(n3) << (colorCodeBitSize * 3)) |
(ColorSequence(n4) << (colorCodeBitSize * 4))
}
func (c ColorSequence) WriteTo(w io.Writer) (int64, error) {
if c <= 0 {
return 0, nil
}
n := int64(0)
io.WriteString(w, "\x1B[")
count := c & colorCodeMask
for {
c >>= colorCodeBitSize
_n, err := fmt.Fprintf(w, "%d", c&colorCodeMask)
n += int64(_n)
if err != nil {
break
}
count--
if count <= 0 {
break
}
_n, err = w.Write([]byte{';'})
n += int64(_n)
if err != nil {
break
}
}
_n, err := w.Write([]byte{'m'})
n += int64(_n)
return n, err
}