tty.go 3.7 KB

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