color.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package door
  2. import (
  3. "fmt"
  4. "log"
  5. "strings"
  6. )
  7. // Convert an array of int to \x1b[1;2;3m for ANSI Color
  8. // output.
  9. // https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
  10. // https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit
  11. func Color(arg ...int) string {
  12. var result string = "\x1b["
  13. for i := range arg {
  14. result += fmt.Sprintf("%d;", arg[i])
  15. }
  16. result = result[:len(result)-1]
  17. result += "m"
  18. return result
  19. }
  20. // BUG(bugz) INVERSE does not work.
  21. // Convert a string like "BRIGHT WHITE ON BLUE" into the expected
  22. // ANSI Color codes. You can use just the first 3 letters.
  23. // Supports BRIGHT, BOLD, BLINK and ON (to set the background color).
  24. // BLACK, RED, GREEN, BROWN / YELLOW, BLUE, MAGENTA, CYAN, WHITE
  25. // https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
  26. // https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit
  27. func ColorText(color string) string {
  28. // split on spaces, uppercase, match first 3 letter
  29. var result []int
  30. var bg bool
  31. result = append(result, 0)
  32. parts := strings.Fields(strings.ToUpper(color))
  33. for _, part := range parts {
  34. if len(part) > 3 {
  35. part = part[:3]
  36. }
  37. switch part {
  38. case "BLA":
  39. if bg {
  40. result = append(result, 40)
  41. } else {
  42. result = append(result, 30)
  43. }
  44. case "RED":
  45. if bg {
  46. result = append(result, 41)
  47. } else {
  48. result = append(result, 31)
  49. }
  50. case "GRE":
  51. if bg {
  52. result = append(result, 42)
  53. } else {
  54. result = append(result, 32)
  55. }
  56. case "BRO", "YEL":
  57. if bg {
  58. result = append(result, 43)
  59. } else {
  60. result = append(result, 33)
  61. }
  62. case "BLU":
  63. if bg {
  64. result = append(result, 44)
  65. } else {
  66. result = append(result, 34)
  67. }
  68. case "MAG":
  69. if bg {
  70. result = append(result, 45)
  71. } else {
  72. result = append(result, 35)
  73. }
  74. case "CYA":
  75. if bg {
  76. result = append(result, 46)
  77. } else {
  78. result = append(result, 36)
  79. }
  80. case "WHI":
  81. if bg {
  82. result = append(result, 47)
  83. } else {
  84. result = append(result, 37)
  85. }
  86. case "BOL", "BRI":
  87. result = append(result, 1)
  88. case "ON":
  89. bg = true
  90. case "BLI":
  91. result = append(result, 5)
  92. // Invert is't well supported in terminals
  93. // case "INV":
  94. // result = append(result, 7)
  95. default:
  96. log.Panicf("ColorText Unknown: [%s] in %s\n", part, color)
  97. }
  98. }
  99. return Color(result...)
  100. }