tty.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Copyright 2020 Docker, Inc.
  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 progress
  14. import (
  15. "context"
  16. "fmt"
  17. "io"
  18. "runtime"
  19. "strings"
  20. "sync"
  21. "time"
  22. "github.com/buger/goterm"
  23. "github.com/morikuni/aec"
  24. )
  25. type ttyWriter struct {
  26. out io.Writer
  27. events map[string]Event
  28. eventIDs []string
  29. repeated bool
  30. numLines int
  31. done chan bool
  32. mtx *sync.RWMutex
  33. }
  34. func (w *ttyWriter) Start(ctx context.Context) error {
  35. ticker := time.NewTicker(100 * time.Millisecond)
  36. for {
  37. select {
  38. case <-ctx.Done():
  39. w.print()
  40. return ctx.Err()
  41. case <-w.done:
  42. w.print()
  43. return nil
  44. case <-ticker.C:
  45. w.print()
  46. }
  47. }
  48. }
  49. func (w *ttyWriter) Stop() {
  50. w.done <- true
  51. }
  52. func (w *ttyWriter) Event(e Event) {
  53. w.mtx.Lock()
  54. defer w.mtx.Unlock()
  55. if !contains(w.eventIDs, e.ID) {
  56. w.eventIDs = append(w.eventIDs, e.ID)
  57. }
  58. if _, ok := w.events[e.ID]; ok {
  59. event := w.events[e.ID]
  60. if event.Status != Done && e.Status == Done {
  61. event.stop()
  62. }
  63. event.Status = e.Status
  64. event.Text = e.Text
  65. event.StatusText = e.StatusText
  66. w.events[e.ID] = event
  67. } else {
  68. e.startTime = time.Now()
  69. e.spinner = newSpinner()
  70. w.events[e.ID] = e
  71. }
  72. }
  73. func (w *ttyWriter) print() {
  74. w.mtx.Lock()
  75. defer w.mtx.Unlock()
  76. if len(w.eventIDs) == 0 {
  77. return
  78. }
  79. terminalWidth := goterm.Width()
  80. b := aec.EmptyBuilder
  81. for i := 0; i <= w.numLines; i++ {
  82. b = b.Up(1)
  83. }
  84. if !w.repeated {
  85. b = b.Down(1)
  86. }
  87. w.repeated = true
  88. fmt.Fprint(w.out, b.Column(0).ANSI)
  89. // Hide the cursor while we are printing
  90. fmt.Fprint(w.out, aec.Hide)
  91. defer fmt.Fprint(w.out, aec.Show)
  92. firstLine := fmt.Sprintf("[+] Running %d/%d", numDone(w.events), w.numLines)
  93. if w.numLines != 0 && numDone(w.events) == w.numLines {
  94. firstLine = aec.Apply(firstLine, aec.BlueF)
  95. }
  96. fmt.Fprintln(w.out, firstLine)
  97. var statusPadding int
  98. for _, v := range w.eventIDs {
  99. l := len(fmt.Sprintf("%s %s", w.events[v].ID, w.events[v].Text))
  100. if statusPadding < l {
  101. statusPadding = l
  102. }
  103. }
  104. numLines := 0
  105. for _, v := range w.eventIDs {
  106. line := lineText(w.events[v], terminalWidth, statusPadding, runtime.GOOS != "windows")
  107. // nolint: errcheck
  108. fmt.Fprint(w.out, line)
  109. numLines++
  110. }
  111. w.numLines = numLines
  112. }
  113. func lineText(event Event, terminalWidth, statusPadding int, color bool) string {
  114. endTime := time.Now()
  115. if event.Status != Working {
  116. endTime = event.endTime
  117. }
  118. elapsed := endTime.Sub(event.startTime).Seconds()
  119. textLen := len(fmt.Sprintf("%s %s", event.ID, event.Text))
  120. padding := statusPadding - textLen
  121. if padding < 0 {
  122. padding = 0
  123. }
  124. text := fmt.Sprintf(" %s %s %s%s %s",
  125. event.spinner.String(),
  126. event.ID,
  127. event.Text,
  128. strings.Repeat(" ", padding),
  129. event.StatusText,
  130. )
  131. timer := fmt.Sprintf("%.1fs\n", elapsed)
  132. o := align(text, timer, terminalWidth)
  133. if color {
  134. color := aec.WhiteF
  135. if event.Status == Done {
  136. color = aec.BlueF
  137. }
  138. if event.Status == Error {
  139. color = aec.RedF
  140. }
  141. return aec.Apply(o, color)
  142. }
  143. return o
  144. }
  145. func numDone(events map[string]Event) int {
  146. i := 0
  147. for _, e := range events {
  148. if e.Status == Done {
  149. i++
  150. }
  151. }
  152. return i
  153. }
  154. func align(l, r string, w int) string {
  155. return fmt.Sprintf("%-[2]*[1]s %[3]s", l, w-len(r)-1, r)
  156. }
  157. func contains(ar []string, needle string) bool {
  158. for _, v := range ar {
  159. if needle == v {
  160. return true
  161. }
  162. }
  163. return false
  164. }