colors.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package console
  2. import (
  3. "strconv"
  4. )
  5. var NAMES = []string{
  6. "grey",
  7. "red",
  8. "green",
  9. "yellow",
  10. "blue",
  11. "magenta",
  12. "cyan",
  13. "white",
  14. }
  15. var COLORS map[string]ColorFunc
  16. // ColorFunc use ANSI codes to render colored text on console
  17. type ColorFunc func(s string) string
  18. var Monochrome = func(s string) string {
  19. return s
  20. }
  21. func makeColorFunc(code string) ColorFunc {
  22. return func(s string) string {
  23. return ansiColor(code, s)
  24. }
  25. }
  26. var Rainbow = make(chan ColorFunc)
  27. func init() {
  28. COLORS = map[string]ColorFunc{}
  29. for i, name := range NAMES {
  30. COLORS[name] = makeColorFunc(strconv.Itoa(30 + i))
  31. COLORS["intense_"+name] = makeColorFunc(strconv.Itoa(30+i) + ";1")
  32. }
  33. go func() {
  34. i := 0
  35. rainbow := []ColorFunc{
  36. COLORS["cyan"],
  37. COLORS["yellow"],
  38. COLORS["green"],
  39. COLORS["magenta"],
  40. COLORS["blue"],
  41. COLORS["intense_cyan"],
  42. COLORS["intense_yellow"],
  43. COLORS["intense_green"],
  44. COLORS["intense_magenta"],
  45. COLORS["intense_blue"],
  46. }
  47. for {
  48. Rainbow <- rainbow[i]
  49. i = (i + 1) % len(rainbow)
  50. }
  51. }()
  52. }