color.go 2.6 KB

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