colors.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. "os"
  17. "strconv"
  18. "github.com/mattn/go-isatty"
  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. // Never use ANSI codes
  32. Never = "never"
  33. // Always use ANSI codes
  34. Always = "always"
  35. // Auto detect terminal is a tty and can use ANSI codes
  36. Auto = "auto"
  37. )
  38. // SetANSIMode configure formatter for colored output on ANSI-compliant console
  39. func SetANSIMode(ansi string) {
  40. if !useAnsi(ansi) {
  41. nextColor = func() colorFunc {
  42. return monochrome
  43. }
  44. }
  45. }
  46. func useAnsi(ansi string) bool {
  47. switch ansi {
  48. case Always:
  49. return true
  50. case Auto:
  51. return isatty.IsTerminal(os.Stdout.Fd())
  52. }
  53. return false
  54. }
  55. // colorFunc use ANSI codes to render colored text on console
  56. type colorFunc func(s string) string
  57. var monochrome = func(s string) string {
  58. return s
  59. }
  60. func ansiColor(code, s string) string {
  61. return fmt.Sprintf("%s%s%s", ansi(code), s, ansi("0"))
  62. }
  63. func ansi(code string) string {
  64. return fmt.Sprintf("\033[%sm", code)
  65. }
  66. func makeColorFunc(code string) colorFunc {
  67. return func(s string) string {
  68. return ansiColor(code, s)
  69. }
  70. }
  71. var nextColor = rainbowColor
  72. func rainbowColor() colorFunc {
  73. return <-loop
  74. }
  75. var loop = make(chan colorFunc)
  76. func init() {
  77. colors := map[string]colorFunc{}
  78. for i, name := range names {
  79. colors[name] = makeColorFunc(strconv.Itoa(30 + i))
  80. colors["intense_"+name] = makeColorFunc(strconv.Itoa(30+i) + ";1")
  81. }
  82. go func() {
  83. i := 0
  84. rainbow := []colorFunc{
  85. colors["cyan"],
  86. colors["yellow"],
  87. colors["green"],
  88. colors["magenta"],
  89. colors["blue"],
  90. colors["intense_cyan"],
  91. colors["intense_yellow"],
  92. colors["intense_green"],
  93. colors["intense_magenta"],
  94. colors["intense_blue"],
  95. }
  96. for {
  97. loop <- rainbow[i]
  98. i = (i + 1) % len(rainbow)
  99. }
  100. }()
  101. }