color.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. switch part {
  34. case "BLACK", "BLA":
  35. if bg {
  36. result = append(result, 40)
  37. } else {
  38. result = append(result, 30)
  39. }
  40. case "RED":
  41. if bg {
  42. result = append(result, 41)
  43. } else {
  44. result = append(result, 31)
  45. }
  46. case "GREEN", "GRE":
  47. if bg {
  48. result = append(result, 42)
  49. } else {
  50. result = append(result, 32)
  51. }
  52. case "BROWN", "BRO":
  53. if bg {
  54. result = append(result, 43)
  55. } else {
  56. result = append(result, 33)
  57. }
  58. case "YELLOW", "YEL":
  59. if bg {
  60. result = append(result, 43)
  61. } else {
  62. result = append(result, 33)
  63. }
  64. case "BLUE", "BLU":
  65. if bg {
  66. result = append(result, 44)
  67. } else {
  68. result = append(result, 34)
  69. }
  70. case "MAGENTA", "MAG":
  71. if bg {
  72. result = append(result, 45)
  73. } else {
  74. result = append(result, 35)
  75. }
  76. case "CYAN", "CYA":
  77. if bg {
  78. result = append(result, 46)
  79. } else {
  80. result = append(result, 36)
  81. }
  82. case "WHITE", "WHI":
  83. if bg {
  84. result = append(result, 47)
  85. } else {
  86. result = append(result, 37)
  87. }
  88. case "BOLD", "BOL", "BRIGHT", "BRI":
  89. result = append(result, 1)
  90. case "ON":
  91. bg = true
  92. case "BLINK", "BLI":
  93. result = append(result, 5)
  94. case "INVERT", "INVERSE", "INV":
  95. result = append(result, 7)
  96. default:
  97. fmt.Println("ColorText Unknown:", part)
  98. }
  99. }
  100. return Color(result...)
  101. }