github.com/gagliardetto/solana-go@v1.11.0/text/tools.go (about) 1 // Copyright 2021 github.com/gagliardetto 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package text 16 17 import ( 18 "fmt" 19 "hash" 20 "hash/fnv" 21 "math" 22 "strings" 23 "sync" 24 ) 25 26 var DisableColors = false 27 28 func S(a ...interface{}) string { 29 return fmt.Sprint(a...) 30 } 31 32 func Sf(format string, a ...interface{}) string { 33 return fmt.Sprintf(format, a...) 34 } 35 36 func Ln(a ...interface{}) string { 37 return fmt.Sprintln(a...) 38 } 39 40 // Lnsf is alias of fmt.Sprintln(fmt.Sprintf()) 41 func Lnsf(format string, a ...interface{}) string { 42 return Ln(Sf(format, a...)) 43 } 44 45 // LnsfI is alias of fmt.Sprintln(fmt.Sprintf()) 46 func LnsfI(indent int, format string, a ...interface{}) string { 47 return Ln(Sf(strings.Repeat(" ", indent)+format, a...)) 48 } 49 50 // CC concats strings 51 func CC(elems ...string) string { 52 return strings.Join(elems, "") 53 } 54 55 func Black(str string) string { 56 if DisableColors { 57 return str 58 } 59 return FgString(str, 0, 0, 0) 60 } 61 62 func White(str string) string { 63 if DisableColors { 64 return str 65 } 66 return FgString(str, 255, 255, 255) 67 } 68 69 func BlackBG(str string) string { 70 if DisableColors { 71 return str 72 } 73 return BgString(str, 0, 0, 0) 74 } 75 76 func WhiteBG(str string) string { 77 if DisableColors { 78 return str 79 } 80 return Black(BgString(str, 255, 255, 255)) 81 } 82 83 func Lime(str string) string { 84 if DisableColors { 85 return str 86 } 87 return FgString(str, 252, 255, 43) 88 } 89 90 func LimeBG(str string) string { 91 if DisableColors { 92 return str 93 } 94 return Black(BgString(str, 252, 255, 43)) 95 } 96 97 func Yellow(str string) string { 98 if DisableColors { 99 return str 100 } 101 return BlackBG(FgString(str, 255, 255, 0)) 102 } 103 104 func YellowBG(str string) string { 105 return Black(BgString(str, 255, 255, 0)) 106 } 107 108 func Orange(str string) string { 109 if DisableColors { 110 return str 111 } 112 return FgString(str, 255, 165, 0) 113 } 114 115 func OrangeBG(str string) string { 116 if DisableColors { 117 return str 118 } 119 return Black(BgString(str, 255, 165, 0)) 120 } 121 122 func Red(str string) string { 123 if DisableColors { 124 return str 125 } 126 return FgString(str, 255, 0, 0) 127 } 128 129 func RedBG(str string) string { 130 if DisableColors { 131 return str 132 } 133 return White(BgString(str, 220, 20, 60)) 134 } 135 136 // light blue? 137 func Shakespeare(str string) string { 138 if DisableColors { 139 return str 140 } 141 return FgString(str, 82, 179, 217) 142 } 143 144 func ShakespeareBG(str string) string { 145 if DisableColors { 146 return str 147 } 148 return White(BgString(str, 82, 179, 217)) 149 } 150 151 func Purple(str string) string { 152 if DisableColors { 153 return str 154 } 155 return FgString(str, 255, 0, 255) 156 } 157 158 func PurpleBG(str string) string { 159 if DisableColors { 160 return str 161 } 162 return Black(BgString(str, 255, 0, 255)) 163 } 164 165 func Indigo(str string) string { 166 if DisableColors { 167 return str 168 } 169 return FgString(str, 75, 0, 130) 170 } 171 172 func IndigoBG(str string) string { 173 if DisableColors { 174 return str 175 } 176 return BgString(str, 75, 0, 130) 177 } 178 179 func Bold(str string) string { 180 if DisableColors { 181 return str 182 } 183 return foreachLine(str, func(idx int, line string) string { 184 return fmt.Sprintf("\033[1m%s\033[0m", line) 185 }) 186 } 187 188 type sf func(int, string) string 189 190 func foreachLine(str string, transform sf) (out string) { 191 for idx, line := range strings.Split(str, "\n") { 192 out += transform(idx, line) 193 } 194 return 195 } 196 197 func HighlightRedBG(str, substr string) string { 198 return HighlightAnyCase(str, substr, RedBG) 199 } 200 201 func HighlightLimeBG(str, substr string) string { 202 return HighlightAnyCase(str, substr, LimeBG) 203 } 204 205 func HighlightAnyCase(str, substr string, colorer func(string) string) string { 206 substr = strings.ToLower(substr) 207 str = strings.ToLower(str) 208 209 hiSubstr := colorer(substr) 210 return strings.Replace(str, substr, hiSubstr, -1) 211 } 212 213 func StringToColor(str string) func(string) string { 214 hs := HashString(str) 215 r, g, b, _ := calcColor(hs) 216 217 bgColor := WhiteBG 218 if IsLight(r, g, b) { 219 bgColor = BlackBG 220 } 221 return func(str string) string { 222 return bgColor(FgString(str, uint8(r), uint8(g), uint8(b))) 223 } 224 } 225 226 func StringToColorBG(str string) func(string) string { 227 hs := HashString(str) 228 r, g, b, _ := calcColor(hs) 229 230 textColor := White 231 if IsLight(r, g, b) { 232 textColor = Black 233 } 234 return func(str string) string { 235 return textColor(BgString(str, uint8(r), uint8(g), uint8(b))) 236 } 237 } 238 239 func Colorize(str string) string { 240 if DisableColors { 241 return str 242 } 243 colorizer := StringToColor(str) 244 return colorizer(str) 245 } 246 247 func ColorizeBG(str string) string { 248 if DisableColors { 249 return str 250 } 251 colorizer := StringToColorBG(str) 252 return colorizer(str) 253 } 254 255 func calcColor(color uint64) (red, green, blue, alpha uint64) { 256 alpha = color & 0xFF 257 blue = (color >> 8) & 0xFF 258 green = (color >> 16) & 0xFF 259 red = (color >> 24) & 0xFF 260 261 return red, green, blue, alpha 262 } 263 264 // IsLight returns whether the color is perceived to be a light color 265 func IsLight(rr, gg, bb uint64) bool { 266 r := float64(rr) 267 g := float64(gg) 268 b := float64(bb) 269 270 hsp := math.Sqrt(0.299*math.Pow(r, 2) + 0.587*math.Pow(g, 2) + 0.114*math.Pow(b, 2)) 271 272 return hsp > 130 273 } 274 275 var hasherPool *sync.Pool 276 277 func init() { 278 hasherPool = &sync.Pool{ 279 New: func() interface{} { 280 return fnv.New64a() 281 }, 282 } 283 } 284 285 func HashString(s string) uint64 { 286 h := hasherPool.Get().(hash.Hash64) 287 defer hasherPool.Put(h) 288 h.Reset() 289 _, err := h.Write([]byte(s)) 290 if err != nil { 291 panic(err) 292 } 293 return h.Sum64() 294 }