event.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. "time"
  16. "github.com/morikuni/aec"
  17. )
  18. // EventStatus indicates the status of an action
  19. type EventStatus int
  20. func (s EventStatus) color() aec.ANSI {
  21. switch s {
  22. case Done:
  23. return aec.GreenF
  24. case Warning:
  25. return aec.YellowF.With(aec.Bold)
  26. case Error:
  27. return aec.RedF.With(aec.Bold)
  28. default:
  29. return aec.DefaultF
  30. }
  31. }
  32. const (
  33. // Working means that the current task is working
  34. Working EventStatus = iota
  35. // Done means that the current task is done
  36. Done
  37. // Warning means that the current task has warning
  38. Warning
  39. // Error means that the current task has errored
  40. Error
  41. )
  42. // Event represents a progress event.
  43. type Event struct {
  44. ID string
  45. ParentID string
  46. Text string
  47. Status EventStatus
  48. StatusText string
  49. Current int64
  50. Percent int
  51. Total int64
  52. startTime time.Time
  53. endTime time.Time
  54. spinner *spinner
  55. }
  56. // ErrorMessageEvent creates a new Error Event with message
  57. func ErrorMessageEvent(id string, msg string) Event {
  58. return NewEvent(id, Error, msg)
  59. }
  60. // ErrorEvent creates a new Error Event
  61. func ErrorEvent(id string) Event {
  62. return NewEvent(id, Error, "Error")
  63. }
  64. // CreatingEvent creates a new Create in progress Event
  65. func CreatingEvent(id string) Event {
  66. return NewEvent(id, Working, "Creating")
  67. }
  68. // StartingEvent creates a new Starting in progress Event
  69. func StartingEvent(id string) Event {
  70. return NewEvent(id, Working, "Starting")
  71. }
  72. // StartedEvent creates a new Started in progress Event
  73. func StartedEvent(id string) Event {
  74. return NewEvent(id, Done, "Started")
  75. }
  76. // Waiting creates a new waiting event
  77. func Waiting(id string) Event {
  78. return NewEvent(id, Working, "Waiting")
  79. }
  80. // Healthy creates a new healthy event
  81. func Healthy(id string) Event {
  82. return NewEvent(id, Done, "Healthy")
  83. }
  84. // Exited creates a new exited event
  85. func Exited(id string) Event {
  86. return NewEvent(id, Done, "Exited")
  87. }
  88. // RestartingEvent creates a new Restarting in progress Event
  89. func RestartingEvent(id string) Event {
  90. return NewEvent(id, Working, "Restarting")
  91. }
  92. // RestartedEvent creates a new Restarted in progress Event
  93. func RestartedEvent(id string) Event {
  94. return NewEvent(id, Done, "Restarted")
  95. }
  96. // RunningEvent creates a new Running in progress Event
  97. func RunningEvent(id string) Event {
  98. return NewEvent(id, Done, "Running")
  99. }
  100. // CreatedEvent creates a new Created (done) Event
  101. func CreatedEvent(id string) Event {
  102. return NewEvent(id, Done, "Created")
  103. }
  104. // StoppingEvent creates a new Stopping in progress Event
  105. func StoppingEvent(id string) Event {
  106. return NewEvent(id, Working, "Stopping")
  107. }
  108. // StoppedEvent creates a new Stopping in progress Event
  109. func StoppedEvent(id string) Event {
  110. return NewEvent(id, Done, "Stopped")
  111. }
  112. // KillingEvent creates a new Killing in progress Event
  113. func KillingEvent(id string) Event {
  114. return NewEvent(id, Working, "Killing")
  115. }
  116. // KilledEvent creates a new Killed in progress Event
  117. func KilledEvent(id string) Event {
  118. return NewEvent(id, Done, "Killed")
  119. }
  120. // RemovingEvent creates a new Removing in progress Event
  121. func RemovingEvent(id string) Event {
  122. return NewEvent(id, Working, "Removing")
  123. }
  124. // RemovedEvent creates a new removed (done) Event
  125. func RemovedEvent(id string) Event {
  126. return NewEvent(id, Done, "Removed")
  127. }
  128. // NewEvent new event
  129. func NewEvent(id string, status EventStatus, statusText string) Event {
  130. return Event{
  131. ID: id,
  132. Status: status,
  133. StatusText: statusText,
  134. }
  135. }
  136. func (e *Event) stop() {
  137. e.endTime = time.Now()
  138. e.spinner.Stop()
  139. }
  140. var (
  141. spinnerDone = aec.Apply("✔", aec.GreenF)
  142. spinnerWarning = aec.Apply("!", aec.YellowF)
  143. spinnerError = aec.Apply("✘", aec.RedF)
  144. )
  145. func (e *Event) Spinner() any {
  146. switch e.Status {
  147. case Done:
  148. return spinnerDone
  149. case Warning:
  150. return spinnerWarning
  151. case Error:
  152. return spinnerError
  153. default:
  154. return e.spinner.String()
  155. }
  156. }