progress.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package console
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "strconv"
  7. "strings"
  8. "github.com/sirupsen/logrus"
  9. )
  10. type resource struct {
  11. name string
  12. status string
  13. details string
  14. }
  15. type progress struct {
  16. console console
  17. resources []*resource
  18. }
  19. type ProgressWriter interface {
  20. ResourceEvent(name string, status string, details string)
  21. }
  22. func NewProgressWriter() ProgressWriter {
  23. return &progress{
  24. console: ansiConsole{os.Stdout},
  25. }
  26. }
  27. const (
  28. blue = "36;2"
  29. red = "31;1"
  30. green = "32;1"
  31. )
  32. func (p *progress) ResourceEvent(name string, status string, details string) {
  33. if logrus.IsLevelEnabled(logrus.DebugLevel) {
  34. logrus.Debugf("> %s : %s %s\n", name, status, details)
  35. return
  36. }
  37. p.console.MoveUp(len(p.resources))
  38. newResource := true
  39. for _, r := range p.resources {
  40. if r.name == name {
  41. newResource = false
  42. r.status = status
  43. r.details = details
  44. break
  45. }
  46. }
  47. if newResource {
  48. p.resources = append(p.resources, &resource{name, status, details})
  49. }
  50. var width int
  51. for _, r := range p.resources {
  52. l := len(r.name)
  53. if width < l {
  54. width = l
  55. }
  56. }
  57. for _, r := range p.resources {
  58. s := r.status
  59. if strings.HasSuffix(s, "_IN_PROGRESS") {
  60. s = p.console.WiP(s)
  61. } else if strings.HasSuffix(s, "_COMPLETE") {
  62. s = p.console.OK(s)
  63. } else if strings.HasSuffix(s, "_FAILED") {
  64. s = p.console.KO(s)
  65. }
  66. p.console.ClearLine()
  67. p.console.Printf("%-"+strconv.Itoa(width)+"s ... %s %s", r.name, s, r.details) // nolint:errcheck
  68. p.console.MoveDown(1)
  69. }
  70. }
  71. type console interface {
  72. Printf(format string, a ...interface{})
  73. MoveUp(int)
  74. MoveDown(int)
  75. ClearLine()
  76. OK(string) string
  77. KO(string) string
  78. WiP(string) string
  79. }
  80. type ansiConsole struct {
  81. out io.Writer
  82. }
  83. func (c ansiConsole) Printf(format string, a ...interface{}) {
  84. fmt.Fprintf(c.out, format, a...) // nolint:errcheck
  85. fmt.Fprintf(c.out, "\r")
  86. }
  87. func (c ansiConsole) MoveUp(i int) {
  88. if i == 0 {
  89. return
  90. }
  91. fmt.Fprintf(c.out, "\033[%dA", i) // nolint:errcheck
  92. }
  93. func (c ansiConsole) MoveDown(i int) {
  94. if i == 0 {
  95. return
  96. }
  97. fmt.Fprintf(c.out, "\033[%dB", i) // nolint:errcheck
  98. }
  99. func (c ansiConsole) ClearLine() {
  100. fmt.Fprint(c.out, "\033[2K\r") // nolint:errcheck
  101. }
  102. func (c ansiConsole) OK(s string) string {
  103. return ansiColor(green, s)
  104. }
  105. func (c ansiConsole) KO(s string) string {
  106. return ansiColor(red, s)
  107. }
  108. func (c ansiConsole) WiP(s string) string {
  109. return ansiColor(blue, s)
  110. }
  111. func ansiColor(code, s string) string {
  112. return fmt.Sprintf("%s%s%s", ansi(code), s, ansi("0"))
  113. }
  114. func ansi(code string) string {
  115. return fmt.Sprintf("\033[%sm", code)
  116. }