stopping.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Copyright 2024 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 formatter
  14. import (
  15. "fmt"
  16. "strings"
  17. "time"
  18. "github.com/buger/goterm"
  19. "github.com/docker/compose/v2/pkg/api"
  20. "github.com/docker/compose/v2/pkg/progress"
  21. )
  22. type Stopping struct {
  23. api.LogConsumer
  24. enabled bool
  25. spinner *progress.Spinner
  26. ticker *time.Ticker
  27. startedAt time.Time
  28. }
  29. func NewStopping(l api.LogConsumer) *Stopping {
  30. s := &Stopping{}
  31. s.LogConsumer = logDecorator{
  32. decorated: l,
  33. Before: s.clear,
  34. After: s.print,
  35. }
  36. return s
  37. }
  38. func (s *Stopping) ApplicationTermination() {
  39. if progress.Mode != progress.ModeAuto {
  40. // User explicitly opted for output format
  41. return
  42. }
  43. if disableAnsi {
  44. return
  45. }
  46. s.enabled = true
  47. s.spinner = progress.NewSpinner()
  48. hideCursor()
  49. s.startedAt = time.Now()
  50. s.ticker = time.NewTicker(100 * time.Millisecond)
  51. go func() {
  52. for {
  53. <-s.ticker.C
  54. s.print()
  55. }
  56. }()
  57. }
  58. func (s *Stopping) Close() {
  59. showCursor()
  60. if s.ticker != nil {
  61. s.ticker.Stop()
  62. }
  63. s.clear()
  64. }
  65. func (s *Stopping) clear() {
  66. if !s.enabled {
  67. return
  68. }
  69. height := goterm.Height()
  70. carriageReturn()
  71. saveCursor()
  72. // clearLine()
  73. for i := 0; i < height; i++ {
  74. moveCursorDown(1)
  75. clearLine()
  76. }
  77. restoreCursor()
  78. }
  79. const stoppingBanner = "Gracefully Stopping... (press Ctrl+C again to force)"
  80. func (s *Stopping) print() {
  81. if !s.enabled {
  82. return
  83. }
  84. height := goterm.Height()
  85. width := goterm.Width()
  86. carriageReturn()
  87. saveCursor()
  88. moveCursor(height, 0)
  89. clearLine()
  90. elapsed := time.Since(s.startedAt).Seconds()
  91. timer := fmt.Sprintf("%.1fs ", elapsed)
  92. pad := width - len(timer) - len(stoppingBanner) - 5
  93. fmt.Printf("%s %s %s %s",
  94. progress.CountColor(s.spinner.String()),
  95. stoppingBanner,
  96. strings.Repeat(" ", pad),
  97. progress.TimerColor(timer),
  98. )
  99. carriageReturn()
  100. restoreCursor()
  101. }