color.go 2.3 KB

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