colors.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package formatter
  14. import (
  15. "fmt"
  16. "strconv"
  17. "sync"
  18. "github.com/docker/cli/cli/command"
  19. )
  20. var names = []string{
  21. "grey",
  22. "red",
  23. "green",
  24. "yellow",
  25. "blue",
  26. "magenta",
  27. "cyan",
  28. "white",
  29. }
  30. const (
  31. BOLD = "1"
  32. FAINT = "2"
  33. ITALIC = "3"
  34. UNDERLINE = "4"
  35. )
  36. const (
  37. RESET = "0"
  38. CYAN = "36"
  39. )
  40. const (
  41. // Never use ANSI codes
  42. Never = "never"
  43. // Always use ANSI codes
  44. Always = "always"
  45. // Auto detect terminal is a tty and can use ANSI codes
  46. Auto = "auto"
  47. )
  48. // SetANSIMode configure formatter for colored output on ANSI-compliant console
  49. func SetANSIMode(streams command.Streams, ansi string) {
  50. if !useAnsi(streams, ansi) {
  51. nextColor = func() colorFunc {
  52. return monochrome
  53. }
  54. disableAnsi = true
  55. }
  56. }
  57. func useAnsi(streams command.Streams, ansi string) bool {
  58. switch ansi {
  59. case Always:
  60. return true
  61. case Auto:
  62. return streams.Out().IsTerminal()
  63. }
  64. return false
  65. }
  66. // colorFunc use ANSI codes to render colored text on console
  67. type colorFunc func(s string) string
  68. var monochrome = func(s string) string {
  69. return s
  70. }
  71. func ansiColor(code, s string, formatOpts ...string) string {
  72. return fmt.Sprintf("%s%s%s", ansiColorCode(code, formatOpts...), s, ansiColorCode("0"))
  73. }
  74. // Everything about ansiColorCode color https://hyperskill.org/learn/step/18193
  75. func ansiColorCode(code string, formatOpts ...string) string {
  76. res := "\033["
  77. for _, c := range formatOpts {
  78. res = fmt.Sprintf("%s%s;", res, c)
  79. }
  80. return fmt.Sprintf("%s%sm", res, code)
  81. }
  82. func makeColorFunc(code string) colorFunc {
  83. return func(s string) string {
  84. return ansiColor(code, s)
  85. }
  86. }
  87. var nextColor = rainbowColor
  88. var rainbow []colorFunc
  89. var currentIndex = 0
  90. var mutex sync.Mutex
  91. func rainbowColor() colorFunc {
  92. mutex.Lock()
  93. defer mutex.Unlock()
  94. result := rainbow[currentIndex]
  95. currentIndex = (currentIndex + 1) % len(rainbow)
  96. return result
  97. }
  98. func init() {
  99. colors := map[string]colorFunc{}
  100. for i, name := range names {
  101. colors[name] = makeColorFunc(strconv.Itoa(30 + i))
  102. colors["intense_"+name] = makeColorFunc(strconv.Itoa(30+i) + ";1")
  103. }
  104. rainbow = []colorFunc{
  105. colors["cyan"],
  106. colors["yellow"],
  107. colors["green"],
  108. colors["magenta"],
  109. colors["blue"],
  110. colors["intense_cyan"],
  111. colors["intense_yellow"],
  112. colors["intense_green"],
  113. colors["intense_magenta"],
  114. colors["intense_blue"],
  115. }
  116. }