tty.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 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. last.ParentID = e.ParentID
  71. w.events[e.ID] = last
  72. } else {
  73. e.startTime = time.Now()
  74. e.spinner = newSpinner()
  75. w.events[e.ID] = e
  76. }
  77. }
  78. func (w *ttyWriter) print() {
  79. w.mtx.Lock()
  80. defer w.mtx.Unlock()
  81. if len(w.eventIDs) == 0 {
  82. return
  83. }
  84. terminalWidth := goterm.Width()
  85. b := aec.EmptyBuilder
  86. for i := 0; i <= w.numLines; i++ {
  87. b = b.Up(1)
  88. }
  89. if !w.repeated {
  90. b = b.Down(1)
  91. }
  92. w.repeated = true
  93. fmt.Fprint(w.out, b.Column(0).ANSI)
  94. // Hide the cursor while we are printing
  95. fmt.Fprint(w.out, aec.Hide)
  96. defer fmt.Fprint(w.out, aec.Show)
  97. firstLine := fmt.Sprintf("[+] Running %d/%d", numDone(w.events), w.numLines)
  98. if w.numLines != 0 && numDone(w.events) == w.numLines {
  99. firstLine = aec.Apply(firstLine, aec.BlueF)
  100. }
  101. fmt.Fprintln(w.out, firstLine)
  102. var statusPadding int
  103. for _, v := range w.eventIDs {
  104. event := w.events[v]
  105. l := len(fmt.Sprintf("%s %s", event.ID, event.Text))
  106. if statusPadding < l {
  107. statusPadding = l
  108. }
  109. if event.ParentID != "" {
  110. statusPadding -= 2
  111. }
  112. }
  113. numLines := 0
  114. for _, v := range w.eventIDs {
  115. event := w.events[v]
  116. if event.ParentID != "" {
  117. continue
  118. }
  119. line := lineText(event, "", terminalWidth, statusPadding, runtime.GOOS != "windows")
  120. // nolint: errcheck
  121. fmt.Fprint(w.out, line)
  122. numLines++
  123. for _, v := range w.eventIDs {
  124. ev := w.events[v]
  125. if ev.ParentID == event.ID {
  126. line := lineText(ev, " ", terminalWidth, statusPadding, runtime.GOOS != "windows")
  127. // nolint: errcheck
  128. fmt.Fprint(w.out, line)
  129. numLines++
  130. }
  131. }
  132. }
  133. w.numLines = numLines
  134. }
  135. func lineText(event Event, pad string, terminalWidth, statusPadding int, color bool) string {
  136. endTime := time.Now()
  137. if event.Status != Working {
  138. endTime = event.startTime
  139. if (event.endTime != time.Time{}) {
  140. endTime = event.endTime
  141. }
  142. }
  143. elapsed := endTime.Sub(event.startTime).Seconds()
  144. textLen := len(fmt.Sprintf("%s %s", event.ID, event.Text))
  145. padding := statusPadding - textLen
  146. if padding < 0 {
  147. padding = 0
  148. }
  149. // calculate the max length for the status text, on errors it
  150. // is 2-3 lines long and breaks the line formating
  151. maxStatusLen := terminalWidth - textLen - statusPadding - 15
  152. status := event.StatusText
  153. // in some cases (debugging under VS Code), terminalWidth is set to zero by goterm.Width() ; ensuring we don't tweak strings with negative char index
  154. if maxStatusLen > 0 && len(status) > maxStatusLen {
  155. status = status[:maxStatusLen] + "..."
  156. }
  157. text := fmt.Sprintf("%s %s %s %s%s %s",
  158. pad,
  159. event.spinner.String(),
  160. event.ID,
  161. event.Text,
  162. strings.Repeat(" ", padding),
  163. status,
  164. )
  165. timer := fmt.Sprintf("%.1fs\n", elapsed)
  166. o := align(text, timer, terminalWidth)
  167. if color {
  168. color := aec.WhiteF
  169. if event.Status == Done {
  170. color = aec.BlueF
  171. }
  172. if event.Status == Error {
  173. color = aec.RedF
  174. }
  175. return aec.Apply(o, color)
  176. }
  177. return o
  178. }
  179. func numDone(events map[string]Event) int {
  180. i := 0
  181. for _, e := range events {
  182. if e.Status == Done {
  183. i++
  184. }
  185. }
  186. return i
  187. }
  188. func align(l, r string, w int) string {
  189. return fmt.Sprintf("%-[2]*[1]s %[3]s", l, w-len(r)-1, r)
  190. }