forked from gagliardetto/solana-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.go
294 lines (246 loc) · 5.57 KB
/
tools.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright 2021 github.com/gagliardetto
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package text
import (
"fmt"
"hash"
"hash/fnv"
"math"
"strings"
"sync"
)
var DisableColors = false
func S(a ...interface{}) string {
return fmt.Sprint(a...)
}
func Sf(format string, a ...interface{}) string {
return fmt.Sprintf(format, a...)
}
func Ln(a ...interface{}) string {
return fmt.Sprintln(a...)
}
// Lnsf is alias of fmt.Sprintln(fmt.Sprintf())
func Lnsf(format string, a ...interface{}) string {
return Ln(Sf(format, a...))
}
// LnsfI is alias of fmt.Sprintln(fmt.Sprintf())
func LnsfI(indent int, format string, a ...interface{}) string {
return Ln(Sf(strings.Repeat(" ", indent)+format, a...))
}
// CC concats strings
func CC(elems ...string) string {
return strings.Join(elems, "")
}
func Black(str string) string {
if DisableColors {
return str
}
return FgString(str, 0, 0, 0)
}
func White(str string) string {
if DisableColors {
return str
}
return FgString(str, 255, 255, 255)
}
func BlackBG(str string) string {
if DisableColors {
return str
}
return BgString(str, 0, 0, 0)
}
func WhiteBG(str string) string {
if DisableColors {
return str
}
return Black(BgString(str, 255, 255, 255))
}
func Lime(str string) string {
if DisableColors {
return str
}
return FgString(str, 252, 255, 43)
}
func LimeBG(str string) string {
if DisableColors {
return str
}
return Black(BgString(str, 252, 255, 43))
}
func Yellow(str string) string {
if DisableColors {
return str
}
return BlackBG(FgString(str, 255, 255, 0))
}
func YellowBG(str string) string {
return Black(BgString(str, 255, 255, 0))
}
func Orange(str string) string {
if DisableColors {
return str
}
return FgString(str, 255, 165, 0)
}
func OrangeBG(str string) string {
if DisableColors {
return str
}
return Black(BgString(str, 255, 165, 0))
}
func Red(str string) string {
if DisableColors {
return str
}
return FgString(str, 255, 0, 0)
}
func RedBG(str string) string {
if DisableColors {
return str
}
return White(BgString(str, 220, 20, 60))
}
// light blue?
func Shakespeare(str string) string {
if DisableColors {
return str
}
return FgString(str, 82, 179, 217)
}
func ShakespeareBG(str string) string {
if DisableColors {
return str
}
return White(BgString(str, 82, 179, 217))
}
func Purple(str string) string {
if DisableColors {
return str
}
return FgString(str, 255, 0, 255)
}
func PurpleBG(str string) string {
if DisableColors {
return str
}
return Black(BgString(str, 255, 0, 255))
}
func Indigo(str string) string {
if DisableColors {
return str
}
return FgString(str, 75, 0, 130)
}
func IndigoBG(str string) string {
if DisableColors {
return str
}
return BgString(str, 75, 0, 130)
}
func Bold(str string) string {
if DisableColors {
return str
}
return foreachLine(str, func(idx int, line string) string {
return fmt.Sprintf("\033[1m%s\033[0m", line)
})
}
type sf func(int, string) string
func foreachLine(str string, transform sf) (out string) {
for idx, line := range strings.Split(str, "\n") {
out += transform(idx, line)
}
return
}
func HighlightRedBG(str, substr string) string {
return HighlightAnyCase(str, substr, RedBG)
}
func HighlightLimeBG(str, substr string) string {
return HighlightAnyCase(str, substr, LimeBG)
}
func HighlightAnyCase(str, substr string, colorer func(string) string) string {
substr = strings.ToLower(substr)
str = strings.ToLower(str)
hiSubstr := colorer(substr)
return strings.Replace(str, substr, hiSubstr, -1)
}
func StringToColor(str string) func(string) string {
hs := HashString(str)
r, g, b, _ := calcColor(hs)
bgColor := WhiteBG
if IsLight(r, g, b) {
bgColor = BlackBG
}
return func(str string) string {
return bgColor(FgString(str, uint8(r), uint8(g), uint8(b)))
}
}
func StringToColorBG(str string) func(string) string {
hs := HashString(str)
r, g, b, _ := calcColor(hs)
textColor := White
if IsLight(r, g, b) {
textColor = Black
}
return func(str string) string {
return textColor(BgString(str, uint8(r), uint8(g), uint8(b)))
}
}
func Colorize(str string) string {
if DisableColors {
return str
}
colorizer := StringToColor(str)
return colorizer(str)
}
func ColorizeBG(str string) string {
if DisableColors {
return str
}
colorizer := StringToColorBG(str)
return colorizer(str)
}
func calcColor(color uint64) (red, green, blue, alpha uint64) {
alpha = color & 0xFF
blue = (color >> 8) & 0xFF
green = (color >> 16) & 0xFF
red = (color >> 24) & 0xFF
return red, green, blue, alpha
}
// IsLight returns whether the color is perceived to be a light color
func IsLight(rr, gg, bb uint64) bool {
r := float64(rr)
g := float64(gg)
b := float64(bb)
hsp := math.Sqrt(0.299*math.Pow(r, 2) + 0.587*math.Pow(g, 2) + 0.114*math.Pow(b, 2))
return hsp > 130
}
var hasherPool *sync.Pool
func init() {
hasherPool = &sync.Pool{
New: func() interface{} {
return fnv.New64a()
},
}
}
func HashString(s string) uint64 {
h := hasherPool.Get().(hash.Hash64)
defer hasherPool.Put(h)
h.Reset()
_, err := h.Write([]byte(s))
if err != nil {
panic(err)
}
return h.Sum64()
}