| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package term |
| 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 |
import ( |
| 35 |
"errors" |
| 36 |
"fmt" |
| 37 |
"math/rand" |
| 38 |
"strconv" |
| 39 |
) |
| 40 |
|
| 41 |
type stringer interface { |
| 42 |
String() string |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
var colorEnable = true |
| 47 |
|
| 48 |
|
| 49 |
func ColorEnable() { |
| 50 |
colorEnable = true |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
func ColorDisable() { |
| 55 |
colorEnable = false |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
const ( |
| 60 |
CSI = "\033[" |
| 61 |
FgBlack = "30" |
| 62 |
FgRed = "31" |
| 63 |
FgGreen = "32" |
| 64 |
FgYellow = "33" |
| 65 |
FgBlue = "34" |
| 66 |
FgMagenta = "35" |
| 67 |
FgCyan = "36" |
| 68 |
FgWhite = "37" |
| 69 |
FgDefault = "39" |
| 70 |
F256 = "38" |
| 71 |
BgBlack = "40" |
| 72 |
BgRed = "41" |
| 73 |
BgGreen = "42" |
| 74 |
BgYellow = "43" |
| 75 |
BgBlue = "44" |
| 76 |
BgMagenta = "45" |
| 77 |
BgCyan = "46" |
| 78 |
BgWhite = "47" |
| 79 |
BgDefault = "49" |
| 80 |
Bg256 = "48" |
| 81 |
Blink = "5" |
| 82 |
Ital = "3" |
| 83 |
Underln = "4" |
| 84 |
Faint = "2" |
| 85 |
Bld = "1" |
| 86 |
NoMode = "0" |
| 87 |
) |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
type Green string |
| 94 |
|
| 95 |
|
| 96 |
type Blue string |
| 97 |
|
| 98 |
|
| 99 |
type Red string |
| 100 |
|
| 101 |
|
| 102 |
type Yellow string |
| 103 |
|
| 104 |
|
| 105 |
type Magenta string |
| 106 |
|
| 107 |
|
| 108 |
type Cyan string |
| 109 |
|
| 110 |
|
| 111 |
type White string |
| 112 |
|
| 113 |
|
| 114 |
type Black string |
| 115 |
|
| 116 |
|
| 117 |
type Random string |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
type BGreen string |
| 123 |
|
| 124 |
|
| 125 |
type BBlue string |
| 126 |
|
| 127 |
|
| 128 |
type BRed string |
| 129 |
|
| 130 |
|
| 131 |
type BYellow string |
| 132 |
|
| 133 |
|
| 134 |
type BRandom string |
| 135 |
|
| 136 |
|
| 137 |
type BMagenta string |
| 138 |
|
| 139 |
|
| 140 |
type BCyan string |
| 141 |
|
| 142 |
|
| 143 |
type BWhite string |
| 144 |
|
| 145 |
|
| 146 |
type BBlack string |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
type Color string |
| 152 |
|
| 153 |
|
| 154 |
type ColorRandom string |
| 155 |
|
| 156 |
|
| 157 |
type Color256Random string |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
type Blinking string |
| 163 |
|
| 164 |
|
| 165 |
type Underline string |
| 166 |
|
| 167 |
|
| 168 |
type Bold string |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
type Italic string |
| 174 |
|
| 175 |
|
| 176 |
func colType(col stringer) string { |
| 177 |
nMode := FgDefault |
| 178 |
var mode, res string |
| 179 |
switch c := col.(type) { |
| 180 |
case Black: |
| 181 |
mode = FgBlack |
| 182 |
res = string(c) |
| 183 |
case Red: |
| 184 |
mode = FgRed |
| 185 |
res = string(c) |
| 186 |
case Green: |
| 187 |
mode = FgGreen |
| 188 |
res = string(c) |
| 189 |
case Yellow: |
| 190 |
mode = FgYellow |
| 191 |
res = string(c) |
| 192 |
case Blue: |
| 193 |
mode = FgBlue |
| 194 |
res = string(c) |
| 195 |
case Magenta: |
| 196 |
mode = FgMagenta |
| 197 |
res = string(c) |
| 198 |
case Cyan: |
| 199 |
mode = FgCyan |
| 200 |
res = string(c) |
| 201 |
case White: |
| 202 |
mode = FgWhite |
| 203 |
res = string(c) |
| 204 |
case BBlack: |
| 205 |
mode = BgBlack |
| 206 |
res = string(c) |
| 207 |
case BRed: |
| 208 |
mode = BgRed |
| 209 |
nMode = BgDefault |
| 210 |
res = string(c) |
| 211 |
case BGreen: |
| 212 |
mode = BgGreen |
| 213 |
nMode = BgDefault |
| 214 |
res = string(c) |
| 215 |
case BYellow: |
| 216 |
mode = BgYellow |
| 217 |
nMode = BgDefault |
| 218 |
res = string(c) |
| 219 |
case BBlue: |
| 220 |
mode = BgBlue |
| 221 |
nMode = BgDefault |
| 222 |
res = string(c) |
| 223 |
case BMagenta: |
| 224 |
mode = BgMagenta |
| 225 |
nMode = BgDefault |
| 226 |
res = string(c) |
| 227 |
case BCyan: |
| 228 |
mode = BgCyan |
| 229 |
nMode = BgDefault |
| 230 |
res = string(c) |
| 231 |
case BWhite: |
| 232 |
mode = BgWhite |
| 233 |
nMode = BgDefault |
| 234 |
res = string(c) |
| 235 |
case Blinking: |
| 236 |
mode = Blink |
| 237 |
nMode = NoMode |
| 238 |
res = string(c) |
| 239 |
case Italic: |
| 240 |
mode = Ital |
| 241 |
nMode = NoMode |
| 242 |
res = string(c) |
| 243 |
case Underline: |
| 244 |
mode = Underln |
| 245 |
nMode = NoMode |
| 246 |
res = string(c) |
| 247 |
case Bold: |
| 248 |
nMode = NoMode |
| 249 |
mode = Bld |
| 250 |
res = string(c) |
| 251 |
default: |
| 252 |
return "unsupported type" |
| 253 |
} |
| 254 |
if !colorEnable { |
| 255 |
return res |
| 256 |
} |
| 257 |
return CSI + mode + "m" + res + CSI + nMode + "m" |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
func (c Green) String() string { |
| 265 |
return colType(c) |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
func Greenf(format string, a ...interface{}) string { |
| 270 |
return colType(Green(fmt.Sprintf(format, a...))) |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
func (c Blue) String() string { |
| 275 |
return colType(c) |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
func Bluef(format string, a ...interface{}) string { |
| 280 |
return colType(Blue(fmt.Sprintf(format, a...))) |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
func (c Red) String() string { |
| 285 |
return colType(c) |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
func Redf(format string, a ...interface{}) string { |
| 290 |
return colType(Red(fmt.Sprintf(format, a...))) |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
func (c Yellow) String() string { |
| 295 |
return colType(c) |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
func Yellowf(format string, a ...interface{}) string { |
| 300 |
return colType(Yellow(fmt.Sprintf(format, a...))) |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
func (c Magenta) String() string { |
| 305 |
return colType(c) |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
func Magentaf(format string, a ...interface{}) string { |
| 310 |
return colType(Magenta(fmt.Sprintf(format, a...))) |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
func (c White) String() string { |
| 315 |
return colType(c) |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
func Whitef(format string, a ...interface{}) string { |
| 320 |
return colType(White(fmt.Sprintf(format, a...))) |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
func (c Black) String() string { |
| 325 |
return colType(c) |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
func Blackf(format string, a ...interface{}) string { |
| 330 |
return colType(Black(fmt.Sprintf(format, a...))) |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
func (c Cyan) String() string { |
| 335 |
return colType(c) |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
func Cyanf(format string, a ...interface{}) string { |
| 340 |
return colType(Cyan(fmt.Sprintf(format, a...))) |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
func (c BGreen) String() string { |
| 347 |
return colType(c) |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
func BGreenf(format string, a ...interface{}) string { |
| 352 |
return colType(BGreen(fmt.Sprintf(format, a...))) |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
func (c BBlue) String() string { |
| 357 |
return colType(c) |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
func BBluef(format string, a ...interface{}) string { |
| 362 |
return colType(BBlue(fmt.Sprintf(format, a...))) |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
func (c BRed) String() string { |
| 367 |
return colType(c) |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
func BRedf(format string, a ...interface{}) string { |
| 372 |
return colType(BRed(fmt.Sprintf(format, a...))) |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
func (c BYellow) String() string { |
| 377 |
return colType(c) |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
func BYellowf(format string, a ...interface{}) string { |
| 382 |
return colType(BYellow(fmt.Sprintf(format, a...))) |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
func (c BMagenta) String() string { |
| 387 |
return colType(c) |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
func BMagentaf(format string, a ...interface{}) string { |
| 392 |
return colType(BMagenta(fmt.Sprintf(format, a...))) |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
func (c BWhite) String() string { |
| 397 |
return colType(c) |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
func BWhitef(format string, a ...interface{}) string { |
| 402 |
return colType(BWhite(fmt.Sprintf(format, a...))) |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
func (c BBlack) String() string { |
| 407 |
return colType(c) |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
func BBlackf(format string, a ...interface{}) string { |
| 412 |
return colType(BBlack(fmt.Sprintf(format, a...))) |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
func (c BCyan) String() string { |
| 417 |
return colType(c) |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
func BCyanf(format string, a ...interface{}) string { |
| 422 |
return colType(BCyan(fmt.Sprintf(format, a...))) |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
func (c Blinking) String() string { |
| 429 |
return colType(c) |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
func (c Underline) String() string { |
| 434 |
return colType(c) |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
func (c Bold) String() string { |
| 439 |
return colType(c) |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
func (c Italic) String() string { |
| 444 |
return colType(c) |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
func NewColor(str string, fg string, bg string) (Color, error) { |
| 450 |
if fg != "" { |
| 451 |
ifg, err := strconv.Atoi(fg) |
| 452 |
if err != nil { |
| 453 |
return Color(""), err |
| 454 |
} |
| 455 |
if ifg < 30 && ifg > 37 { |
| 456 |
return Color(""), errors.New("fg: " + fg + "not a valid color 30-37") |
| 457 |
} |
| 458 |
} else { |
| 459 |
fg = FgDefault |
| 460 |
} |
| 461 |
if bg != "" { |
| 462 |
ibg, err := strconv.Atoi(bg) |
| 463 |
if err != nil { |
| 464 |
return Color(""), err |
| 465 |
} |
| 466 |
if ibg < 40 && ibg > 47 { |
| 467 |
return Color(""), errors.New("fg: " + fg + "not a valid color 40-47") |
| 468 |
} |
| 469 |
} else { |
| 470 |
bg = BgDefault |
| 471 |
} |
| 472 |
return Color(CSI + fg + ";" + bg + "m" + str + CSI + FgDefault + ";" + BgDefault + "m"), nil |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
func (c Color) String() string { |
| 477 |
if !colorEnable { |
| 478 |
clean := make([]byte, 0, len(c)) |
| 479 |
src := []byte(c) |
| 480 |
L1: |
| 481 |
for i := 0; i < len(src); i++ { |
| 482 |
|
| 483 |
if len(src) < i+4 { |
| 484 |
clean = append(clean, src[i:]...) |
| 485 |
return string(clean) |
| 486 |
} |
| 487 |
if string(src[i:i+2]) == CSI { |
| 488 |
|
| 489 |
s := i |
| 490 |
|
| 491 |
for i += 2; i < len(src); i++ { |
| 492 |
switch src[i] { |
| 493 |
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ';': |
| 494 |
|
| 495 |
continue |
| 496 |
case 'm': |
| 497 |
|
| 498 |
continue L1 |
| 499 |
default: |
| 500 |
|
| 501 |
i = s |
| 502 |
break |
| 503 |
} |
| 504 |
} |
| 505 |
} |
| 506 |
clean = append(clean, src[i]) |
| 507 |
} |
| 508 |
return string(clean) |
| 509 |
} |
| 510 |
return string(c) |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
func NewColor256(str string, fg string, bg string) (Color, error) { |
| 515 |
if fg != "" { |
| 516 |
ifg, err := strconv.Atoi(fg) |
| 517 |
if err != nil { |
| 518 |
return Color(""), err |
| 519 |
} |
| 520 |
if ifg < 0 && ifg > 256 { |
| 521 |
return Color(""), errors.New("fg: " + fg + " not a valid color 0-256") |
| 522 |
} |
| 523 |
} |
| 524 |
if bg != "" { |
| 525 |
ibg, err := strconv.Atoi(bg) |
| 526 |
if err != nil { |
| 527 |
return Color(""), err |
| 528 |
} |
| 529 |
if ibg < 0 && ibg > 256 { |
| 530 |
return Color(""), errors.New("bg: " + bg + " not a valid color 0-256") |
| 531 |
} |
| 532 |
} |
| 533 |
tstr := CSI + F256 + ";5;" + fg + ";" + Bg256 + ";5;" + bg + "m" |
| 534 |
tstr += str |
| 535 |
return Color(tstr + CSI + FgDefault + ";5;" + BgDefault + ";5;" + "m"), nil |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
func NewColorRGB(str string, red uint8, green uint8, blue uint8) Color { |
| 541 |
ired := strconv.Itoa(int(red)) |
| 542 |
igreen := strconv.Itoa(int(green)) |
| 543 |
iblue := strconv.Itoa(int(blue)) |
| 544 |
tstr := CSI + F256 + ";2;" + ired + ";" + igreen + ";" + iblue + "m" |
| 545 |
tstr += str |
| 546 |
return Color(tstr + CSI + FgDefault + ";5;" + BgDefault + ";5;" + "m") |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
func (c ColorRandom) String() string { |
| 551 |
if !colorEnable { |
| 552 |
return string(c) |
| 553 |
} |
| 554 |
ifg := rand.Int()%8 + 30 |
| 555 |
ibg := rand.Int()%8 + 40 |
| 556 |
res := CSI + strconv.Itoa(ifg) + ";" + strconv.Itoa(ibg) + "m" |
| 557 |
res += string(c) |
| 558 |
res += CSI + strconv.Itoa(ifg) + ";" + strconv.Itoa(ibg) + "m" |
| 559 |
return res |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
func (c Random) String() string { |
| 564 |
if !colorEnable { |
| 565 |
return string(c) |
| 566 |
} |
| 567 |
ifg := int(rand.Int()%8 + 30) |
| 568 |
res := CSI + strconv.Itoa(ifg) + "m" |
| 569 |
res += string(c) + strconv.Itoa(int(ifg)) |
| 570 |
res += CSI + FgDefault + "m" |
| 571 |
return res |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
func (c BRandom) String() string { |
| 576 |
if !colorEnable { |
| 577 |
return string(c) |
| 578 |
} |
| 579 |
ibg := rand.Int()%8 + 40 |
| 580 |
res := CSI + strconv.Itoa(ibg) + "m" |
| 581 |
res += string(c) + strconv.Itoa(ibg) |
| 582 |
res += CSI + BgDefault + "m" |
| 583 |
return res |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
func NewCombo(s string, mods ...string) Color { |
| 588 |
var col, bcol, mod bool |
| 589 |
modstr := CSI |
| 590 |
tracking := make(map[string]bool) |
| 591 |
for _, m := range mods { |
| 592 |
switch m { |
| 593 |
case FgBlack, FgRed, FgGreen, FgYellow, FgBlue, FgMagenta, FgCyan, FgWhite: |
| 594 |
if col { |
| 595 |
continue |
| 596 |
} |
| 597 |
col = true |
| 598 |
case BgBlack, BgRed, BgGreen, BgYellow, BgBlue, BgMagenta, BgCyan, BgWhite: |
| 599 |
if bcol { |
| 600 |
continue |
| 601 |
} |
| 602 |
bcol = true |
| 603 |
case Bld, Faint, Ital, Underln, Blink: |
| 604 |
if tracking[m] { |
| 605 |
continue |
| 606 |
} |
| 607 |
tracking[m] = true |
| 608 |
mod = true |
| 609 |
default: |
| 610 |
continue |
| 611 |
} |
| 612 |
modstr += m + ";" |
| 613 |
} |
| 614 |
end := CSI |
| 615 |
if col { |
| 616 |
end += FgDefault |
| 617 |
} |
| 618 |
if bcol { |
| 619 |
if col { |
| 620 |
end += ";" |
| 621 |
} |
| 622 |
end += BgDefault |
| 623 |
} |
| 624 |
if mod { |
| 625 |
if col || bcol { |
| 626 |
end += ";" |
| 627 |
} |
| 628 |
end += NoMode |
| 629 |
} |
| 630 |
end += "m" |
| 631 |
modstr = modstr[:len(modstr)-1] + "m" |
| 632 |
modstr += s |
| 633 |
modstr += end |
| 634 |
return Color(modstr) |
| 635 |
} |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
func TestTerm() string { |
| 640 |
res := "Standard 8:\n" |
| 641 |
res += "Fg:\t" |
| 642 |
for c := 30; c < 38; c++ { |
| 643 |
tres, _ := NewColor("#", strconv.Itoa(c), "") |
| 644 |
res += tres.String() |
| 645 |
} |
| 646 |
res += "\nBg:\t" |
| 647 |
for c := 40; c < 48; c++ { |
| 648 |
tres, _ := NewColor(" ", "", strconv.Itoa(c)) |
| 649 |
res += tres.String() |
| 650 |
} |
| 651 |
res += "\nStandard 16:\t" |
| 652 |
for c := 0; c < 16; c++ { |
| 653 |
tcol, _ := NewColor256(" ", "", strconv.Itoa(c)) |
| 654 |
res += tcol.String() |
| 655 |
} |
| 656 |
res += "\n" |
| 657 |
res += "256 Col:\n" |
| 658 |
|
| 659 |
for row, base := 1, 0; row <= 6; row++ { |
| 660 |
base = (row * 6) + 9 |
| 661 |
for cubes := 1; cubes <= 6; cubes++ { |
| 662 |
for column := 1; column <= 6; column++ { |
| 663 |
tcol, _ := NewColor256(" ", "", strconv.Itoa(base+column)) |
| 664 |
res += tcol.String() |
| 665 |
} |
| 666 |
base += 36 |
| 667 |
} |
| 668 |
res += "\n" |
| 669 |
} |
| 670 |
|
| 671 |
res += "Grayscales:\n" |
| 672 |
for c := 232; c <= 255; c++ { |
| 673 |
tcol, _ := NewColor256(" ", "", strconv.Itoa(c)) |
| 674 |
res += tcol.String() |
| 675 |
} |
| 676 |
return res |
| 677 |
} |