-
Notifications
You must be signed in to change notification settings - Fork 3
/
ctc_string.go
123 lines (111 loc) · 2.63 KB
/
ctc_string.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
package ctc
import (
"bytes"
"strconv"
"strings"
"unsafe"
_ "github.com/wzshiming/winseq" // Use Unix like Sequences in Windows
)
var cc = []string{
"Black",
"Red",
"Green",
"Yellow",
"Blue",
"Magenta",
"Cyan",
"White",
}
var pre = []byte("\x1b[0")
// String returns UnixLike markup
func (c Color) String() string {
b := c.Bytes()
return *(*string)(unsafe.Pointer(&b))
}
// Bytes returns UnixLike markup
func (c Color) Bytes() []byte {
s := make([]byte, 0, 16)
s = append(s, pre...)
if c&applyForeground == applyForeground {
if c&ForegroundBright == ForegroundBright {
s = appendColor(s, uint8(c&foregroundMask), 90)
} else {
s = appendColor(s, uint8(c&foregroundMask), 30)
}
}
if c&applyBackground == applyBackground {
if c&BackgroundBright == BackgroundBright {
s = appendColor(s, uint8(c&backgroundMask>>4), 100)
} else {
s = appendColor(s, uint8(c&backgroundMask>>4), 40)
}
}
if c&Underline == Underline {
s = append(s, ';', '4')
}
if c&Negative == Negative {
s = append(s, ';', '7')
}
s = append(s, 'm')
return s
}
// Name returns color name
func (c Color) Name() string {
if c&(applyForeground|applyBackground) == 0 {
return "Reset"
}
buf := bytes.NewBuffer(nil)
if c&applyForeground == applyForeground {
buf.WriteString("Foreground")
if c&ForegroundBright == ForegroundBright {
buf.WriteString("Bright")
}
buf.WriteString(cc[int(c&foregroundMask&white)])
}
if c&applyBackground == applyBackground {
buf.WriteString("Background")
if c&BackgroundBright == BackgroundBright {
buf.WriteString("Bright")
}
buf.WriteString(cc[int(((c&backgroundMask)>>4)&white)])
}
if c&Underline == Underline {
buf.WriteString("Underline")
}
if c&Negative == Negative {
buf.WriteString("Negative")
}
return buf.String()
}
// Info returns color details info
func (c Color) Info() string {
if c&(applyForeground|applyBackground) == 0 {
return "Reset"
}
ss := []string{}
if c&applyForeground == applyForeground {
if c&ForegroundBright == ForegroundBright {
ss = append(ss, "ForegroundBright")
}
ss = append(ss, "Foreground"+cc[int(c&foregroundMask&white)])
}
if c&applyBackground == applyBackground {
if c&BackgroundBright == BackgroundBright {
ss = append(ss, "BackgroundBright")
}
ss = append(ss, "Background"+cc[int(((c&backgroundMask)>>4)&white)])
}
if c&Underline == Underline {
ss = append(ss, "Underline")
}
if c&Negative == Negative {
ss = append(ss, "Negative")
}
return strings.Join(ss, " | ")
}
func appendColor(s []byte, c uint8, off uint8) []byte {
s = append(s, ';')
n := uint64(off + c&uint8(white))
s = strconv.AppendUint(s, n, 10)
return s
}