progress.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package progress
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "sync"
  7. "time"
  8. )
  9. type State interface {
  10. String() string
  11. }
  12. type Progress struct {
  13. mu sync.Mutex
  14. // buffer output to minimize flickering on all terminals
  15. w *bufio.Writer
  16. pos int
  17. ticker *time.Ticker
  18. states []State
  19. }
  20. func NewProgress(w io.Writer) *Progress {
  21. p := &Progress{w: bufio.NewWriter(w)}
  22. go p.start()
  23. return p
  24. }
  25. func (p *Progress) stop() bool {
  26. for _, state := range p.states {
  27. if spinner, ok := state.(*Spinner); ok {
  28. spinner.Stop()
  29. }
  30. }
  31. if p.ticker != nil {
  32. p.ticker.Stop()
  33. p.ticker = nil
  34. p.render()
  35. return true
  36. }
  37. return false
  38. }
  39. func (p *Progress) Stop() bool {
  40. stopped := p.stop()
  41. if stopped {
  42. fmt.Fprint(p.w, "\n")
  43. p.w.Flush()
  44. }
  45. return stopped
  46. }
  47. func (p *Progress) StopAndClear() bool {
  48. defer p.w.Flush()
  49. fmt.Fprint(p.w, "\033[?25l")
  50. defer fmt.Fprint(p.w, "\033[?25h")
  51. stopped := p.stop()
  52. if stopped {
  53. // clear all progress lines
  54. for i := range p.pos {
  55. if i > 0 {
  56. fmt.Fprint(p.w, "\033[A")
  57. }
  58. fmt.Fprint(p.w, "\033[2K\033[1G")
  59. }
  60. }
  61. return stopped
  62. }
  63. func (p *Progress) Add(key string, state State) {
  64. p.mu.Lock()
  65. defer p.mu.Unlock()
  66. p.states = append(p.states, state)
  67. }
  68. func (p *Progress) render() {
  69. p.mu.Lock()
  70. defer p.mu.Unlock()
  71. defer p.w.Flush()
  72. // eliminate flickering on terminals that support synchronized output
  73. fmt.Fprint(p.w, "\033[?2026h")
  74. defer fmt.Fprint(p.w, "\033[?2026l")
  75. fmt.Fprint(p.w, "\033[?25l")
  76. defer fmt.Fprint(p.w, "\033[?25h")
  77. // move the cursor back to the beginning
  78. for range p.pos - 1 {
  79. fmt.Fprint(p.w, "\033[A")
  80. }
  81. fmt.Fprint(p.w, "\033[1G")
  82. // render progress lines
  83. for i, state := range p.states {
  84. fmt.Fprint(p.w, state.String(), "\033[K")
  85. if i < len(p.states)-1 {
  86. fmt.Fprint(p.w, "\n")
  87. }
  88. }
  89. p.pos = len(p.states)
  90. }
  91. func (p *Progress) start() {
  92. p.ticker = time.NewTicker(100 * time.Millisecond)
  93. for range p.ticker.C {
  94. p.render()
  95. }
  96. }