-
Notifications
You must be signed in to change notification settings - Fork 0
/
vt-color.go
executable file
·109 lines (95 loc) · 2.4 KB
/
vt-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
package termios
// color_util.go converts colors to VT escape codes which can be used on Unix and newer Windows versions.
import (
"fmt"
"strconv"
"strings"
)
func (vt *vt) setStyle(s Style) error {
var escape strings.Builder
escape.WriteString("\x1b[0;")
vt.writeExtras(s.Extras, &escape)
escape.WriteString(vt.colorToEscapeCode(&s.Foreground, true))
escape.WriteString(";")
escape.WriteString(vt.colorToEscapeCode(&s.Background, false))
escape.WriteString("m")
_, err := vt.term.WriteString(escape.String())
if err != nil {
return &IOError{"writing color", err}
}
return nil
}
func (vt *vt) writeExtras(e TextAttribute, out *strings.Builder) {
if e & TextBold != 0 {
out.WriteString("1;")
}
if e & TextDim != 0 {
out.WriteString("2;")
}
if e & TextUnderlined != 0 {
out.WriteString("4;")
}
if e & TextBlink != 0 {
out.WriteString("5;")
}
if e & TextReverse != 0 {
out.WriteString("7;")
}
if e & TextHidden != 0 {
out.WriteString("8;")
}
if e & TextCursive != 0 {
out.WriteString("3;")
}
}
func (vt *vt) colorToEscapeCode(c *Color, isFg bool) string {
switch c.Spectrum() {
case SpectrumDefault:
return vt.defaultColorEscapeCode(isFg)
case Spectrum8:
return vt.color8ToEscapeCode(c.basic, isFg)
case Spectrum16:
return vt.color16ToEscapeCode(c.basic, isFg)
case Spectrum256:
return vt.color256ToEscapeCode(c.basic, isFg)
case SpectrumRGB:
return vt.colorRGBToEscapeCode(c.basic, c.green, c.blue, isFg)
default:
panic("not supported")
}
}
func (vt *vt) defaultColorEscapeCode(isFg bool) string {
if isFg {
return "39"
} else {
return "49"
}
}
func (vt *vt) color8ToEscapeCode(value uint8, isFg bool) string {
if isFg {
return "3" + strconv.FormatUint(uint64(value), 10)
} else {
return "4" + strconv.FormatUint(uint64(value), 10)
}
}
func (vt *vt) color16ToEscapeCode(value uint8, isFg bool) string {
if isFg {
return "9" + strconv.FormatUint(uint64(value-brightOffset), 10)
} else {
return "10" + strconv.FormatUint(uint64(value-brightOffset), 10)
}
}
func (vt *vt) color256ToEscapeCode(value uint8, isFg bool) string {
if isFg {
return "38;5;" + strconv.FormatUint(uint64(value), 10)
} else {
return "48;5;" + strconv.FormatUint(uint64(value), 10)
}
}
func (vt *vt) colorRGBToEscapeCode(r uint8, g uint8, b uint8, isFg bool) string {
if isFg {
return fmt.Sprintf("38;2;%d;%d;%d", r, g, b)
} else {
return fmt.Sprintf("48;2;%d;%d;%d", r, g, b)
}
}