color.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. package color
  2. import (
  3. "bytes"
  4. "fmt"
  5. )
  6. type (
  7. inner func(interface{}, []string, *Color) 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. // H hidden emphasis style
  58. H = "8"
  59. // S strikeout emphasis style
  60. S = "9"
  61. )
  62. var (
  63. black = outer(Blk)
  64. red = outer(Rd)
  65. green = outer(Grn)
  66. yellow = outer(Yel)
  67. blue = outer(Blu)
  68. magenta = outer(Mgn)
  69. cyan = outer(Cyn)
  70. white = outer(Wht)
  71. grey = outer(Gry)
  72. blackBg = outer(BlkBg)
  73. redBg = outer(RdBg)
  74. greenBg = outer(GrnBg)
  75. yellowBg = outer(YelBg)
  76. blueBg = outer(BluBg)
  77. magentaBg = outer(MgnBg)
  78. cyanBg = outer(CynBg)
  79. whiteBg = outer(WhtBg)
  80. reset = outer(R)
  81. bold = outer(B)
  82. dim = outer(D)
  83. italic = outer(I)
  84. underline = outer(U)
  85. inverse = outer(In)
  86. hidden = outer(H)
  87. strikeout = outer(S)
  88. global = New()
  89. )
  90. func outer(n string) inner {
  91. return func(msg interface{}, styles []string, c *Color) string {
  92. // TODO: Drop fmt to boost performance?
  93. if c.disabled {
  94. return fmt.Sprintf("%v", msg)
  95. }
  96. b := new(bytes.Buffer)
  97. b.WriteString("\x1b[")
  98. b.WriteString(n)
  99. for _, s := range styles {
  100. b.WriteString(";")
  101. b.WriteString(s)
  102. }
  103. b.WriteString("m")
  104. return fmt.Sprintf("%s%v\x1b[0m", b.String(), msg)
  105. }
  106. }
  107. type (
  108. Color struct {
  109. disabled bool
  110. }
  111. )
  112. // New creates a Color instance.
  113. func New() *Color {
  114. return &Color{}
  115. }
  116. // Disable disables the colors and styles.
  117. func (c *Color) Disable() {
  118. c.disabled = true
  119. }
  120. // Enable enables the colors and styles.
  121. func (c *Color) Enable() {
  122. c.disabled = false
  123. }
  124. func (c *Color) Black(msg interface{}, styles ...string) string {
  125. return black(msg, styles, c)
  126. }
  127. func (c *Color) Red(msg interface{}, styles ...string) string {
  128. return red(msg, styles, c)
  129. }
  130. func (c *Color) Green(msg interface{}, styles ...string) string {
  131. return green(msg, styles, c)
  132. }
  133. func (c *Color) Yellow(msg interface{}, styles ...string) string {
  134. return yellow(msg, styles, c)
  135. }
  136. func (c *Color) Blue(msg interface{}, styles ...string) string {
  137. return blue(msg, styles, c)
  138. }
  139. func (c *Color) Magenta(msg interface{}, styles ...string) string {
  140. return magenta(msg, styles, c)
  141. }
  142. func (c *Color) Cyan(msg interface{}, styles ...string) string {
  143. return cyan(msg, styles, c)
  144. }
  145. func (c *Color) White(msg interface{}, styles ...string) string {
  146. return white(msg, styles, c)
  147. }
  148. func (c *Color) Grey(msg interface{}, styles ...string) string {
  149. return grey(msg, styles, c)
  150. }
  151. func (c *Color) BlackBg(msg interface{}, styles ...string) string {
  152. return blackBg(msg, styles, c)
  153. }
  154. func (c *Color) RedBg(msg interface{}, styles ...string) string {
  155. return redBg(msg, styles, c)
  156. }
  157. func (c *Color) GreenBg(msg interface{}, styles ...string) string {
  158. return greenBg(msg, styles, c)
  159. }
  160. func (c *Color) YellowBg(msg interface{}, styles ...string) string {
  161. return yellowBg(msg, styles, c)
  162. }
  163. func (c *Color) BlueBg(msg interface{}, styles ...string) string {
  164. return blueBg(msg, styles, c)
  165. }
  166. func (c *Color) MagentaBg(msg interface{}, styles ...string) string {
  167. return magentaBg(msg, styles, c)
  168. }
  169. func (c *Color) CyanBg(msg interface{}, styles ...string) string {
  170. return cyanBg(msg, styles, c)
  171. }
  172. func (c *Color) WhiteBg(msg interface{}, styles ...string) string {
  173. return whiteBg(msg, styles, c)
  174. }
  175. func (c *Color) Reset(msg interface{}, styles ...string) string {
  176. return reset(msg, styles, c)
  177. }
  178. func (c *Color) Bold(msg interface{}, styles ...string) string {
  179. return bold(msg, styles, c)
  180. }
  181. func (c *Color) Dim(msg interface{}, styles ...string) string {
  182. return dim(msg, styles, c)
  183. }
  184. func (c *Color) Italic(msg interface{}, styles ...string) string {
  185. return italic(msg, styles, c)
  186. }
  187. func (c *Color) Underline(msg interface{}, styles ...string) string {
  188. return underline(msg, styles, c)
  189. }
  190. func (c *Color) Inverse(msg interface{}, styles ...string) string {
  191. return inverse(msg, styles, c)
  192. }
  193. func (c *Color) Hidden(msg interface{}, styles ...string) string {
  194. return hidden(msg, styles, c)
  195. }
  196. func (c *Color) Strikeout(msg interface{}, styles ...string) string {
  197. return strikeout(msg, styles, c)
  198. }
  199. func Disable() {
  200. global.Disable()
  201. }
  202. func Enable() {
  203. global.Enable()
  204. }
  205. func Black(msg interface{}, styles ...string) string {
  206. return global.Black(msg, styles...)
  207. }
  208. func Red(msg interface{}, styles ...string) string {
  209. return global.Red(msg, styles...)
  210. }
  211. func Green(msg interface{}, styles ...string) string {
  212. return global.Green(msg, styles...)
  213. }
  214. func Yellow(msg interface{}, styles ...string) string {
  215. return global.Yellow(msg, styles...)
  216. }
  217. func Blue(msg interface{}, styles ...string) string {
  218. return global.Blue(msg, styles...)
  219. }
  220. func Magenta(msg interface{}, styles ...string) string {
  221. return global.Magenta(msg, styles...)
  222. }
  223. func Cyan(msg interface{}, styles ...string) string {
  224. return global.Cyan(msg, styles...)
  225. }
  226. func White(msg interface{}, styles ...string) string {
  227. return global.White(msg, styles...)
  228. }
  229. func Grey(msg interface{}, styles ...string) string {
  230. return global.Grey(msg, styles...)
  231. }
  232. func BlackBg(msg interface{}, styles ...string) string {
  233. return global.BlackBg(msg, styles...)
  234. }
  235. func RedBg(msg interface{}, styles ...string) string {
  236. return global.RedBg(msg, styles...)
  237. }
  238. func GreenBg(msg interface{}, styles ...string) string {
  239. return global.GreenBg(msg, styles...)
  240. }
  241. func YellowBg(msg interface{}, styles ...string) string {
  242. return global.YellowBg(msg, styles...)
  243. }
  244. func BlueBg(msg interface{}, styles ...string) string {
  245. return global.BlueBg(msg, styles...)
  246. }
  247. func MagentaBg(msg interface{}, styles ...string) string {
  248. return global.MagentaBg(msg, styles...)
  249. }
  250. func CyanBg(msg interface{}, styles ...string) string {
  251. return global.CyanBg(msg, styles...)
  252. }
  253. func WhiteBg(msg interface{}, styles ...string) string {
  254. return global.WhiteBg(msg, styles...)
  255. }
  256. func Reset(msg interface{}, styles ...string) string {
  257. return global.Reset(msg, styles...)
  258. }
  259. func Bold(msg interface{}, styles ...string) string {
  260. return global.Bold(msg, styles...)
  261. }
  262. func Dim(msg interface{}, styles ...string) string {
  263. return global.Dim(msg, styles...)
  264. }
  265. func Italic(msg interface{}, styles ...string) string {
  266. return global.Italic(msg, styles...)
  267. }
  268. func Underline(msg interface{}, styles ...string) string {
  269. return global.Underline(msg, styles...)
  270. }
  271. func Inverse(msg interface{}, styles ...string) string {
  272. return global.Inverse(msg, styles...)
  273. }
  274. func Hidden(msg interface{}, styles ...string) string {
  275. return global.Hidden(msg, styles...)
  276. }
  277. func Strikeout(msg interface{}, styles ...string) string {
  278. return global.Strikeout(msg, styles...)
  279. }