colors.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. "strings"
  18. "sync"
  19. "github.com/docker/cli/cli/command"
  20. )
  21. var names = []string{
  22. "grey",
  23. "red",
  24. "green",
  25. "yellow",
  26. "blue",
  27. "magenta",
  28. "cyan",
  29. "white",
  30. }
  31. const (
  32. BOLD = "1"
  33. FAINT = "2"
  34. ITALIC = "3"
  35. UNDERLINE = "4"
  36. )
  37. const (
  38. RESET = "0"
  39. CYAN = "36"
  40. )
  41. const (
  42. // Never use ANSI codes
  43. Never = "never"
  44. // Always use ANSI codes
  45. Always = "always"
  46. // Auto detect terminal is a tty and can use ANSI codes
  47. Auto = "auto"
  48. )
  49. // ansiColorOffset is the offset for basic foreground colors in ANSI escape codes.
  50. const ansiColorOffset = 30
  51. // SetANSIMode configure formatter for colored output on ANSI-compliant console
  52. func SetANSIMode(streams command.Streams, ansi string) {
  53. if !useAnsi(streams, ansi) {
  54. nextColor = func() colorFunc {
  55. return monochrome
  56. }
  57. disableAnsi = true
  58. }
  59. }
  60. func useAnsi(streams command.Streams, ansi string) bool {
  61. switch ansi {
  62. case Always:
  63. return true
  64. case Auto:
  65. return streams.Out().IsTerminal()
  66. }
  67. return false
  68. }
  69. // colorFunc use ANSI codes to render colored text on console
  70. type colorFunc func(s string) string
  71. var monochrome = func(s string) string {
  72. return s
  73. }
  74. func ansiColor(code, s string, formatOpts ...string) string {
  75. return fmt.Sprintf("%s%s%s", ansiColorCode(code, formatOpts...), s, ansiColorCode("0"))
  76. }
  77. // Everything about ansiColorCode color https://hyperskill.org/learn/step/18193
  78. func ansiColorCode(code string, formatOpts ...string) string {
  79. var sb strings.Builder
  80. sb.WriteString("\033[")
  81. for _, c := range formatOpts {
  82. sb.WriteString(c)
  83. sb.WriteString(";")
  84. }
  85. sb.WriteString(code)
  86. sb.WriteString("m")
  87. return sb.String()
  88. }
  89. func makeColorFunc(code string) colorFunc {
  90. return func(s string) string {
  91. return ansiColor(code, s)
  92. }
  93. }
  94. var (
  95. nextColor = rainbowColor
  96. rainbow []colorFunc
  97. currentIndex = 0
  98. mutex sync.Mutex
  99. )
  100. func rainbowColor() colorFunc {
  101. mutex.Lock()
  102. defer mutex.Unlock()
  103. result := rainbow[currentIndex]
  104. currentIndex = (currentIndex + 1) % len(rainbow)
  105. return result
  106. }
  107. func init() {
  108. colors := map[string]colorFunc{}
  109. for i, name := range names {
  110. colors[name] = makeColorFunc(strconv.Itoa(ansiColorOffset + i))
  111. colors["intense_"+name] = makeColorFunc(strconv.Itoa(ansiColorOffset+i) + ";1")
  112. }
  113. rainbow = []colorFunc{
  114. colors["cyan"],
  115. colors["yellow"],
  116. colors["green"],
  117. colors["magenta"],
  118. colors["blue"],
  119. colors["intense_cyan"],
  120. colors["intense_yellow"],
  121. colors["intense_green"],
  122. colors["intense_magenta"],
  123. colors["intense_blue"],
  124. }
  125. }