progress.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package progress
  2. import (
  3. "fmt"
  4. "io"
  5. "sync"
  6. "time"
  7. )
  8. type State interface {
  9. String() string
  10. }
  11. type Progress struct {
  12. mu sync.Mutex
  13. pos int
  14. w io.Writer
  15. ticker *time.Ticker
  16. states []State
  17. }
  18. func NewProgress(w io.Writer) *Progress {
  19. p := &Progress{w: w}
  20. go p.start()
  21. return p
  22. }
  23. func (p *Progress) Stop() bool {
  24. for _, state := range p.states {
  25. if spinner, ok := state.(*Spinner); ok {
  26. spinner.Stop()
  27. }
  28. }
  29. if p.ticker != nil {
  30. p.ticker.Stop()
  31. p.ticker = nil
  32. p.render()
  33. return true
  34. }
  35. return false
  36. }
  37. func (p *Progress) StopAndClear() bool {
  38. fmt.Fprint(p.w, "\033[?25l")
  39. defer fmt.Fprint(p.w, "\033[?25h")
  40. stopped := p.Stop()
  41. if stopped {
  42. // clear the progress bar by:
  43. // 1. for each line in the progress:
  44. // a. move the cursor up one line
  45. // b. clear the line
  46. for i := 0; i < p.pos; i++ {
  47. fmt.Fprint(p.w, "\033[A\033[2K")
  48. }
  49. }
  50. return stopped
  51. }
  52. func (p *Progress) Add(key string, state State) {
  53. p.mu.Lock()
  54. defer p.mu.Unlock()
  55. p.states = append(p.states, state)
  56. }
  57. func (p *Progress) render() error {
  58. p.mu.Lock()
  59. defer p.mu.Unlock()
  60. fmt.Fprint(p.w, "\033[?25l")
  61. defer fmt.Fprint(p.w, "\033[?25h")
  62. if p.pos > 0 {
  63. fmt.Fprintf(p.w, "\033[%dA", p.pos)
  64. }
  65. for _, state := range p.states {
  66. fmt.Fprintln(p.w, state.String())
  67. }
  68. if len(p.states) > 0 {
  69. p.pos = len(p.states)
  70. }
  71. return nil
  72. }
  73. func (p *Progress) start() {
  74. p.ticker = time.NewTicker(100 * time.Millisecond)
  75. for range p.ticker.C {
  76. p.render()
  77. }
  78. }