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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
|
//
// Copyright (c) 2016-2020 The Aurora Authors. All rights reserved.
// This program is free software. It comes without any warranty,
// to the extent permitted by applicable law. You can redistribute
// it and/or modify it under the terms of the Unlicense. See LICENSE
// file for more details or see below.
//
//
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// For more information, please refer to <http://unlicense.org/>
//
package aurora
import (
"fmt"
"strconv"
"unicode/utf8"
)
// A Value represents any printable value
// with it's color
type Value interface {
// String returns string with colors. If there are any color
// or format the string will be terminated with \033[0m
fmt.Stringer
// Format implements fmt.Formatter interface
fmt.Formatter
// Color returns value's color
Color() Color
// Value returns value's value (welcome to the tautology club)
Value() interface{}
// internals
tail() Color
setTail(Color) Value
// Bleach returns copy of original value without colors
//
// Deprecated: use Reset instead.
Bleach() Value
// Reset colors and formats
Reset() Value
//
// Formats
//
//
// Bold or increased intensity (1).
Bold() Value
// Faint, decreased intensity, reset the Bold (2).
Faint() Value
//
// DoublyUnderline or Bold off, double-underline
// per ECMA-48 (21). It depends.
DoublyUnderline() Value
// Fraktur, rarely supported (20).
Fraktur() Value
//
// Italic, not widely supported, sometimes
// treated as inverse (3).
Italic() Value
// Underline (4).
Underline() Value
//
// SlowBlink, blinking less than 150
// per minute (5).
SlowBlink() Value
// RapidBlink, blinking 150+ per minute,
// not widely supported (6).
RapidBlink() Value
// Blink is alias for the SlowBlink.
Blink() Value
//
// Reverse video, swap foreground and
// background colors (7).
Reverse() Value
// Inverse is alias for the Reverse
Inverse() Value
//
// Conceal, hidden, not widely supported (8).
Conceal() Value
// Hidden is alias for the Conceal
Hidden() Value
//
// CrossedOut, characters legible, but
// marked for deletion (9).
CrossedOut() Value
// StrikeThrough is alias for the CrossedOut.
StrikeThrough() Value
//
// Framed (51).
Framed() Value
// Encircled (52).
Encircled() Value
//
// Overlined (53).
Overlined() Value
//
// Foreground colors
//
//
// Black foreground color (30)
Black() Value
// Red foreground color (31)
Red() Value
// Green foreground color (32)
Green() Value
// Yellow foreground color (33)
Yellow() Value
// Brown foreground color (33)
//
// Deprecated: use Yellow instead, following specification
Brown() Value
// Blue foreground color (34)
Blue() Value
// Magenta foreground color (35)
Magenta() Value
// Cyan foreground color (36)
Cyan() Value
// White foreground color (37)
White() Value
//
// Bright foreground colors
//
// BrightBlack foreground color (90)
BrightBlack() Value
// BrightRed foreground color (91)
BrightRed() Value
// BrightGreen foreground color (92)
BrightGreen() Value
// BrightYellow foreground color (93)
BrightYellow() Value
// BrightBlue foreground color (94)
BrightBlue() Value
// BrightMagenta foreground color (95)
BrightMagenta() Value
// BrightCyan foreground color (96)
BrightCyan() Value
// BrightWhite foreground color (97)
BrightWhite() Value
//
// Other
//
// Index of pre-defined 8-bit foreground color
// from 0 to 255 (38;5;n).
//
// 0- 7: standard colors (as in ESC [ 30–37 m)
// 8- 15: high intensity colors (as in ESC [ 90–97 m)
// 16-231: 6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)
// 232-255: grayscale from black to white in 24 steps
//
Index(n uint8) Value
// Gray from 0 to 24.
Gray(n uint8) Value
//
// Background colors
//
//
// BgBlack background color (40)
BgBlack() Value
// BgRed background color (41)
BgRed() Value
// BgGreen background color (42)
BgGreen() Value
// BgYellow background color (43)
BgYellow() Value
// BgBrown background color (43)
//
// Deprecated: use BgYellow instead, following specification
BgBrown() Value
// BgBlue background color (44)
BgBlue() Value
// BgMagenta background color (45)
BgMagenta() Value
// BgCyan background color (46)
BgCyan() Value
// BgWhite background color (47)
BgWhite() Value
//
// Bright background colors
//
// BgBrightBlack background color (100)
BgBrightBlack() Value
// BgBrightRed background color (101)
BgBrightRed() Value
// BgBrightGreen background color (102)
BgBrightGreen() Value
// BgBrightYellow background color (103)
BgBrightYellow() Value
// BgBrightBlue background color (104)
BgBrightBlue() Value
// BgBrightMagenta background color (105)
BgBrightMagenta() Value
// BgBrightCyan background color (106)
BgBrightCyan() Value
// BgBrightWhite background color (107)
BgBrightWhite() Value
//
// Other
//
// BgIndex of 8-bit pre-defined background color
// from 0 to 255 (48;5;n).
//
// 0- 7: standard colors (as in ESC [ 40–47 m)
// 8- 15: high intensity colors (as in ESC [100–107 m)
// 16-231: 6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)
// 232-255: grayscale from black to white in 24 steps
//
BgIndex(n uint8) Value
// BgGray from 0 to 24.
BgGray(n uint8) Value
//
// Special
//
// Colorize removes existing colors and
// formats of the argument and applies given.
Colorize(color Color) Value
}
// Value without colors
type valueClear struct {
value interface{}
}
func (vc valueClear) String() string { return fmt.Sprint(vc.value) }
func (vc valueClear) Color() Color { return 0 }
func (vc valueClear) Value() interface{} { return vc.value }
func (vc valueClear) tail() Color { return 0 }
func (vc valueClear) setTail(Color) Value { return vc }
func (vc valueClear) Bleach() Value { return vc }
func (vc valueClear) Reset() Value { return vc }
func (vc valueClear) Bold() Value { return vc }
func (vc valueClear) Faint() Value { return vc }
func (vc valueClear) DoublyUnderline() Value { return vc }
func (vc valueClear) Fraktur() Value { return vc }
func (vc valueClear) Italic() Value { return vc }
func (vc valueClear) Underline() Value { return vc }
func (vc valueClear) SlowBlink() Value { return vc }
func (vc valueClear) RapidBlink() Value { return vc }
func (vc valueClear) Blink() Value { return vc }
func (vc valueClear) Reverse() Value { return vc }
func (vc valueClear) Inverse() Value { return vc }
func (vc valueClear) Conceal() Value { return vc }
func (vc valueClear) Hidden() Value { return vc }
func (vc valueClear) CrossedOut() Value { return vc }
func (vc valueClear) StrikeThrough() Value { return vc }
func (vc valueClear) Framed() Value { return vc }
func (vc valueClear) Encircled() Value { return vc }
func (vc valueClear) Overlined() Value { return vc }
func (vc valueClear) Black() Value { return vc }
func (vc valueClear) Red() Value { return vc }
func (vc valueClear) Green() Value { return vc }
func (vc valueClear) Yellow() Value { return vc }
func (vc valueClear) Brown() Value { return vc }
func (vc valueClear) Blue() Value { return vc }
func (vc valueClear) Magenta() Value { return vc }
func (vc valueClear) Cyan() Value { return vc }
func (vc valueClear) White() Value { return vc }
func (vc valueClear) BrightBlack() Value { return vc }
func (vc valueClear) BrightRed() Value { return vc }
func (vc valueClear) BrightGreen() Value { return vc }
func (vc valueClear) BrightYellow() Value { return vc }
func (vc valueClear) BrightBlue() Value { return vc }
func (vc valueClear) BrightMagenta() Value { return vc }
func (vc valueClear) BrightCyan() Value { return vc }
func (vc valueClear) BrightWhite() Value { return vc }
func (vc valueClear) Index(uint8) Value { return vc }
func (vc valueClear) Gray(uint8) Value { return vc }
func (vc valueClear) BgBlack() Value { return vc }
func (vc valueClear) BgRed() Value { return vc }
func (vc valueClear) BgGreen() Value { return vc }
func (vc valueClear) BgYellow() Value { return vc }
func (vc valueClear) BgBrown() Value { return vc }
func (vc valueClear) BgBlue() Value { return vc }
func (vc valueClear) BgMagenta() Value { return vc }
func (vc valueClear) BgCyan() Value { return vc }
func (vc valueClear) BgWhite() Value { return vc }
func (vc valueClear) BgBrightBlack() Value { return vc }
func (vc valueClear) BgBrightRed() Value { return vc }
func (vc valueClear) BgBrightGreen() Value { return vc }
func (vc valueClear) BgBrightYellow() Value { return vc }
func (vc valueClear) BgBrightBlue() Value { return vc }
func (vc valueClear) BgBrightMagenta() Value { return vc }
func (vc valueClear) BgBrightCyan() Value { return vc }
func (vc valueClear) BgBrightWhite() Value { return vc }
func (vc valueClear) BgIndex(uint8) Value { return vc }
func (vc valueClear) BgGray(uint8) Value { return vc }
func (vc valueClear) Colorize(Color) Value { return vc }
func (vc valueClear) Format(s fmt.State, verb rune) {
// it's enough for many cases (%-+020.10f)
// % - 1
// availFlags - 3 (5)
// width - 2
// prec - 3 (.23)
// verb - 1
// --------------
// 10
format := make([]byte, 1, 10)
format[0] = '%'
var f byte
for i := 0; i < len(availFlags); i++ {
if f = availFlags[i]; s.Flag(int(f)) {
format = append(format, f)
}
}
var width, prec int
var ok bool
if width, ok = s.Width(); ok {
format = strconv.AppendInt(format, int64(width), 10)
}
if prec, ok = s.Precision(); ok {
format = append(format, '.')
format = strconv.AppendInt(format, int64(prec), 10)
}
if verb > utf8.RuneSelf {
format = append(format, string(verb)...)
} else {
format = append(format, byte(verb))
}
fmt.Fprintf(s, string(format), vc.value)
}
// Value within colors
type value struct {
value interface{} // value as it
color Color // this color
tailColor Color // tail color
}
func (v value) String() string {
if v.color != 0 {
if v.tailColor != 0 {
return esc + v.color.Nos(true) + "m" +
fmt.Sprint(v.value) +
esc + v.tailColor.Nos(true) + "m"
}
return esc + v.color.Nos(false) + "m" + fmt.Sprint(v.value) + clear
}
return fmt.Sprint(v.value)
}
func (v value) Color() Color { return v.color }
func (v value) Bleach() Value {
v.color, v.tailColor = 0, 0
return v
}
func (v value) Reset() Value {
v.color, v.tailColor = 0, 0
return v
}
func (v value) tail() Color { return v.tailColor }
func (v value) setTail(t Color) Value {
v.tailColor = t
return v
}
func (v value) Value() interface{} { return v.value }
func (v value) Format(s fmt.State, verb rune) {
// it's enough for many cases (%-+020.10f)
// % - 1
// availFlags - 3 (5)
// width - 2
// prec - 3 (.23)
// verb - 1
// --------------
// 10
// +
// \033[ 5
// 0;1;3;4;5;7;8;9;20;21;51;52;53 30
// 38;5;216 8
// 48;5;216 8
// m 1
// +
// \033[0m 7
//
// x2 (possible tail color)
//
// 10 + 59 * 2 = 128
format := make([]byte, 0, 128)
if v.color != 0 {
format = append(format, esc...)
format = v.color.appendNos(format, v.tailColor != 0)
format = append(format, 'm')
}
format = append(format, '%')
var f byte
for i := 0; i < len(availFlags); i++ {
if f = availFlags[i]; s.Flag(int(f)) {
format = append(format, f)
}
}
var width, prec int
var ok bool
if width, ok = s.Width(); ok {
format = strconv.AppendInt(format, int64(width), 10)
}
if prec, ok = s.Precision(); ok {
format = append(format, '.')
format = strconv.AppendInt(format, int64(prec), 10)
}
if verb > utf8.RuneSelf {
format = append(format, string(verb)...)
} else {
format = append(format, byte(verb))
}
if v.color != 0 {
if v.tailColor != 0 {
// set next (previous) format clearing current one
format = append(format, esc...)
format = v.tailColor.appendNos(format, true)
format = append(format, 'm')
} else {
format = append(format, clear...) // just clear
}
}
fmt.Fprintf(s, string(format), v.value)
}
func (v value) Bold() Value {
v.color = (v.color &^ FaintFm) | BoldFm
return v
}
func (v value) Faint() Value {
v.color = (v.color &^ BoldFm) | FaintFm
return v
}
func (v value) DoublyUnderline() Value {
v.color |= DoublyUnderlineFm
return v
}
func (v value) Fraktur() Value {
v.color |= FrakturFm
return v
}
func (v value) Italic() Value {
v.color |= ItalicFm
return v
}
func (v value) Underline() Value {
v.color |= UnderlineFm
return v
}
func (v value) SlowBlink() Value {
v.color = (v.color &^ RapidBlinkFm) | SlowBlinkFm
return v
}
func (v value) RapidBlink() Value {
v.color = (v.color &^ SlowBlinkFm) | RapidBlinkFm
return v
}
func (v value) Blink() Value {
return v.SlowBlink()
}
func (v value) Reverse() Value {
v.color |= ReverseFm
return v
}
func (v value) Inverse() Value {
return v.Reverse()
}
func (v value) Conceal() Value {
v.color |= ConcealFm
return v
}
func (v value) Hidden() Value {
return v.Conceal()
}
func (v value) CrossedOut() Value {
v.color |= CrossedOutFm
return v
}
func (v value) StrikeThrough() Value {
return v.CrossedOut()
}
func (v value) Framed() Value {
v.color |= FramedFm
return v
}
func (v value) Encircled() Value {
v.color |= EncircledFm
return v
}
func (v value) Overlined() Value {
v.color |= OverlinedFm
return v
}
func (v value) Black() Value {
v.color = (v.color &^ maskFg) | BlackFg
return v
}
func (v value) Red() Value {
v.color = (v.color &^ maskFg) | RedFg
return v
}
func (v value) Green() Value {
v.color = (v.color &^ maskFg) | GreenFg
return v
}
func (v value) Yellow() Value {
v.color = (v.color &^ maskFg) | YellowFg
return v
}
func (v value) Brown() Value {
return v.Yellow()
}
func (v value) Blue() Value {
v.color = (v.color &^ maskFg) | BlueFg
return v
}
func (v value) Magenta() Value {
v.color = (v.color &^ maskFg) | MagentaFg
return v
}
func (v value) Cyan() Value {
v.color = (v.color &^ maskFg) | CyanFg
return v
}
func (v value) White() Value {
v.color = (v.color &^ maskFg) | WhiteFg
return v
}
func (v value) BrightBlack() Value {
v.color = (v.color &^ maskFg) | BrightFg | BlackFg
return v
}
func (v value) BrightRed() Value {
v.color = (v.color &^ maskFg) | BrightFg | RedFg
return v
}
func (v value) BrightGreen() Value {
v.color = (v.color &^ maskFg) | BrightFg | GreenFg
return v
}
func (v value) BrightYellow() Value {
v.color = (v.color &^ maskFg) | BrightFg | YellowFg
return v
}
func (v value) BrightBlue() Value {
v.color = (v.color &^ maskFg) | BrightFg | BlueFg
return v
}
func (v value) BrightMagenta() Value {
v.color = (v.color &^ maskFg) | BrightFg | MagentaFg
return v
}
func (v value) BrightCyan() Value {
v.color = (v.color &^ maskFg) | BrightFg | CyanFg
return v
}
func (v value) BrightWhite() Value {
v.color = (v.color &^ maskFg) | BrightFg | WhiteFg
return v
}
func (v value) Index(n uint8) Value {
v.color = (v.color &^ maskFg) | (Color(n) << shiftFg) | flagFg
return v
}
func (v value) Gray(n uint8) Value {
if n > 23 {
n = 23
}
v.color = (v.color &^ maskFg) | (Color(232+n) << shiftFg) | flagFg
return v
}
func (v value) BgBlack() Value {
v.color = (v.color &^ maskBg) | BlackBg
return v
}
func (v value) BgRed() Value {
v.color = (v.color &^ maskBg) | RedBg
return v
}
func (v value) BgGreen() Value {
v.color = (v.color &^ maskBg) | GreenBg
return v
}
func (v value) BgYellow() Value {
v.color = (v.color &^ maskBg) | YellowBg
return v
}
func (v value) BgBrown() Value {
return v.BgYellow()
}
func (v value) BgBlue() Value {
v.color = (v.color &^ maskBg) | BlueBg
return v
}
func (v value) BgMagenta() Value {
v.color = (v.color &^ maskBg) | MagentaBg
return v
}
func (v value) BgCyan() Value {
v.color = (v.color &^ maskBg) | CyanBg
return v
}
func (v value) BgWhite() Value {
v.color = (v.color &^ maskBg) | WhiteBg
return v
}
func (v value) BgBrightBlack() Value {
v.color = (v.color &^ maskBg) | BrightBg | BlackBg
return v
}
func (v value) BgBrightRed() Value {
v.color = (v.color &^ maskBg) | BrightBg | RedBg
return v
}
func (v value) BgBrightGreen() Value {
v.color = (v.color &^ maskBg) | BrightBg | GreenBg
return v
}
func (v value) BgBrightYellow() Value {
v.color = (v.color &^ maskBg) | BrightBg | YellowBg
return v
}
func (v value) BgBrightBlue() Value {
v.color = (v.color &^ maskBg) | BrightBg | BlueBg
return v
}
func (v value) BgBrightMagenta() Value {
v.color = (v.color &^ maskBg) | BrightBg | MagentaBg
return v
}
func (v value) BgBrightCyan() Value {
v.color = (v.color &^ maskBg) | BrightBg | CyanBg
return v
}
func (v value) BgBrightWhite() Value {
v.color = (v.color &^ maskBg) | BrightBg | WhiteBg
return v
}
func (v value) BgIndex(n uint8) Value {
v.color = (v.color &^ maskBg) | (Color(n) << shiftBg) | flagBg
return v
}
func (v value) BgGray(n uint8) Value {
if n > 23 {
n = 23
}
v.color = (v.color &^ maskBg) | (Color(232+n) << shiftBg) | flagBg
return v
}
func (v value) Colorize(color Color) Value {
v.color = color
return v
}
|