-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_term.go
129 lines (106 loc) · 2.54 KB
/
color_term.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
129
// Copyright 2021 Nelson Germán Ghezzi. All rights reserved.
// Use of this source code is governed by a MIT license that
// can be found in the LICENSE file.
package coloring
import (
"fmt"
"strings"
)
const (
escapeSeq = "\033"
beginSeq = "["
endSeq = "m"
attrDelimiter = ";"
reset = "0"
bold = "1"
faint = "2"
italic = "3"
underline = "4"
blink = "5"
invert = "7"
conceal = "8"
strikethrough = "9"
notBoldNorFaint = "22"
notItalic = "23"
notUnderline = "24"
notBlink = "25"
notInvert = "27"
notConceal = "28"
notStrikethrough = "29"
fg4bColorBlack = "30"
fg4bColorRed = "31"
fg4bColorGreen = "32"
fg4bColorYellow = "33"
fg4bColorBlue = "34"
fg4bColorMagenta = "35"
fg4bColorCyan = "36"
fg4bColorWhite = "37"
fgColorDefault = "39"
bg4bColorBlack = "40"
bg4bColorRed = "41"
bg4bColorGreen = "42"
bg4bColorYellow = "43"
bg4bColorBlue = "44"
bg4bColorMagenta = "45"
bg4bColorCyan = "46"
bg4bColorWhite = "47"
bgColorDefault = "49"
fg4bColorBrightBlack = "90"
fg4bColorBrightRed = "91"
fg4bColorBrightGreen = "92"
fg4bColorBrightYellow = "93"
fg4bColorBrightBlue = "94"
fg4bColorBrightMagenta = "95"
fg4bColorBrightCyan = "96"
fg4bColorBrightWhite = "97"
bg4bColorBrightBlack = "100"
bg4bColorBrightRed = "101"
bg4bColorBrightGreen = "102"
bg4bColorBrightYellow = "103"
bg4bColorBrightBlue = "104"
bg4bColorBrightMagenta = "105"
bg4bColorBrightCyan = "106"
bg4bColorBrightWhite = "107"
fg = "38"
fgColor = fg + attrDelimiter + "5"
fgColorRgb = fg + attrDelimiter + "2"
bg = "48"
bgColor = bg + attrDelimiter + "5"
bgColorRgb = bg + attrDelimiter + "2"
resetSeq = escapeSeq + beginSeq + reset + endSeq
)
// Predefined color values that can be used to specify a color
// in functions expecting a single int.
const (
BLACK = iota
RED
GREEN
YELLOW
BLUE
MAGENTA
CYAN
WHITE
BRIGHTBLACK
BRIGHTRED
BRIGHTGREEN
BRIGHTYELLOW
BRIGHTBLUE
BRIGHTMAGENTA
BRIGHTCYAN
BRIGHTWHITE
)
func applyTo(seq, s string) string {
var sb strings.Builder
seq = strings.TrimSuffix(seq, attrDelimiter)
sb.Grow(len(escapeSeq) + len(beginSeq) + len(seq) + len(endSeq) + len(s) + len(resetSeq))
sb.WriteString(escapeSeq)
sb.WriteString(beginSeq)
sb.WriteString(seq)
sb.WriteString(endSeq)
sb.WriteString(s)
sb.WriteString(resetSeq)
return sb.String()
}
func composeRgbColor(r, g, b int) string {
return fmt.Sprintf("%d;%d;%d", r, g, b)
}