colors.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // SetANSIMode configure formatter for colored output on ANSI-compliant console
  50. func SetANSIMode(streams command.Streams, ansi string) {
  51. if !useAnsi(streams, ansi) {
  52. nextColor = func() colorFunc {
  53. return monochrome
  54. }
  55. disableAnsi = true
  56. }
  57. }
  58. func useAnsi(streams command.Streams, ansi string) bool {
  59. switch ansi {
  60. case Always:
  61. return true
  62. case Auto:
  63. return streams.Out().IsTerminal()
  64. }
  65. return false
  66. }
  67. // colorFunc use ANSI codes to render colored text on console
  68. type colorFunc func(s string) string
  69. var monochrome = func(s string) string {
  70. return s
  71. }
  72. func ansiColor(code, s string, formatOpts ...string) string {
  73. return fmt.Sprintf("%s%s%s", ansiColorCode(code, formatOpts...), s, ansiColorCode("0"))
  74. }
  75. // Everything about ansiColorCode color https://hyperskill.org/learn/step/18193
  76. func ansiColorCode(code string, formatOpts ...string) string {
  77. var sb strings.Builder
  78. sb.WriteString("\033[")
  79. for _, c := range formatOpts {
  80. sb.WriteString(c)
  81. sb.WriteString(";")
  82. }
  83. sb.WriteString(code)
  84. sb.WriteString("m")
  85. return sb.String()
  86. }
  87. func makeColorFunc(code string) colorFunc {
  88. return func(s string) string {
  89. return ansiColor(code, s)
  90. }
  91. }
  92. var (
  93. nextColor = rainbowColor
  94. rainbow []colorFunc
  95. currentIndex = 0
  96. mutex sync.Mutex
  97. )
  98. func rainbowColor() colorFunc {
  99. mutex.Lock()
  100. defer mutex.Unlock()
  101. result := rainbow[currentIndex]
  102. currentIndex = (currentIndex + 1) % len(rainbow)
  103. return result
  104. }
  105. func init() {
  106. colors := map[string]colorFunc{}
  107. for i, name := range names {
  108. colors[name] = makeColorFunc(strconv.Itoa(30 + i))
  109. colors["intense_"+name] = makeColorFunc(strconv.Itoa(30+i) + ";1")
  110. }
  111. rainbow = []colorFunc{
  112. colors["cyan"],
  113. colors["yellow"],
  114. colors["green"],
  115. colors["magenta"],
  116. colors["blue"],
  117. colors["intense_cyan"],
  118. colors["intense_yellow"],
  119. colors["intense_green"],
  120. colors["intense_magenta"],
  121. colors["intense_blue"],
  122. }
  123. }