color.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package color
  2. import (
  3. "bytes"
  4. "fmt"
  5. )
  6. type (
  7. inner func(interface{}, ...string) string
  8. )
  9. // Color styles
  10. const (
  11. // Blk Black text style
  12. Blk = "30"
  13. // Rd red text style
  14. Rd = "31"
  15. // Grn green text style
  16. Grn = "32"
  17. // Yel yellow text style
  18. Yel = "33"
  19. // Blu blue text style
  20. Blu = "34"
  21. // Mgn magenta text style
  22. Mgn = "35"
  23. // Cyn cyan text style
  24. Cyn = "36"
  25. // Wht white text style
  26. Wht = "37"
  27. // Gry grey text style
  28. Gry = "90"
  29. // BlkBg black background style
  30. BlkBg = "40"
  31. // RdBg red background style
  32. RdBg = "41"
  33. // GrnBg green background style
  34. GrnBg = "42"
  35. // YelBg yellow background style
  36. YelBg = "43"
  37. // BluBg blue background style
  38. BluBg = "44"
  39. // MgnBg magenta background style
  40. MgnBg = "45"
  41. // CynBg cyan background style
  42. CynBg = "46"
  43. // WhtBg white background style
  44. WhtBg = "47"
  45. // R reset emphasis style
  46. R = "0"
  47. // B bold emphasis style
  48. B = "1"
  49. // D dim emphasis style
  50. D = "2"
  51. // I italic emphasis style
  52. I = "3"
  53. // U underline emphasis style
  54. U = "4"
  55. // In inverse emphasis style
  56. In = "7"
  57. // Hd hidden emphasis style
  58. Hd = "8"
  59. // So strikeout emphasis style
  60. So = "9"
  61. )
  62. // Color functions
  63. var (
  64. // Text color
  65. Black = outer(Blk)
  66. Red = outer(Rd)
  67. Green = outer(Grn)
  68. Yellow = outer(Yel)
  69. Blue = outer(Blu)
  70. Magenta = outer(Mgn)
  71. Cyan = outer(Cyn)
  72. White = outer(Wht)
  73. Grey = outer(Gry)
  74. // Background color
  75. BlackBg = outer(BlkBg)
  76. RedBg = outer(RdBg)
  77. GreenBg = outer(GrnBg)
  78. YellowBg = outer(YelBg)
  79. BlueBg = outer(BluBg)
  80. MagentaBg = outer(MgnBg)
  81. CyanBg = outer(CynBg)
  82. WhiteBg = outer(WhtBg)
  83. // Emphasis
  84. Reset = outer(R)
  85. Bold = outer(B)
  86. Dim = outer(D)
  87. Italic = outer(I)
  88. Underline = outer(U)
  89. Inverse = outer(In)
  90. Hidden = outer(Hd)
  91. Strikeout = outer(So)
  92. )
  93. func outer(n string) inner {
  94. return func(m interface{}, style ...string) string {
  95. b := new(bytes.Buffer)
  96. b.WriteString("\x1b[")
  97. b.WriteString(n)
  98. for _, s := range style {
  99. b.WriteString(";")
  100. b.WriteString(s)
  101. }
  102. b.WriteString("m")
  103. // TODO: Replace fmt for performance
  104. return fmt.Sprintf("%s%v\x1b[0m", b.String(), m)
  105. }
  106. }