progress.go 1.64 KB
Newer Older
Michael Yang's avatar
Michael Yang committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package progress

import (
	"fmt"
	"io"
	"sync"
	"time"
)

type State interface {
	String() string
}

type Progress struct {
15
16
17
	mu sync.Mutex
	w  io.Writer

Michael Yang's avatar
Michael Yang committed
18
19
20
21
22
23
24
	pos int

	ticker *time.Ticker
	states []State
}

func NewProgress(w io.Writer) *Progress {
Michael Yang's avatar
Michael Yang committed
25
	p := &Progress{w: w}
Michael Yang's avatar
Michael Yang committed
26
27
28
29
	go p.start()
	return p
}

30
func (p *Progress) stop() bool {
31
32
33
34
35
36
	for _, state := range p.states {
		if spinner, ok := state.(*Spinner); ok {
			spinner.Stop()
		}
	}

Michael Yang's avatar
Michael Yang committed
37
38
39
40
	if p.ticker != nil {
		p.ticker.Stop()
		p.ticker = nil
		p.render()
Michael Yang's avatar
Michael Yang committed
41
		return true
Michael Yang's avatar
Michael Yang committed
42
	}
Michael Yang's avatar
Michael Yang committed
43
44
45
46

	return false
}

47
48
49
50
51
52
53
54
func (p *Progress) Stop() bool {
	stopped := p.stop()
	if stopped {
		fmt.Fprint(p.w, "\n")
	}
	return stopped
}

Michael Yang's avatar
Michael Yang committed
55
func (p *Progress) StopAndClear() bool {
Michael Yang's avatar
Michael Yang committed
56
57
58
	fmt.Fprint(p.w, "\033[?25l")
	defer fmt.Fprint(p.w, "\033[?25h")

59
	stopped := p.stop()
Michael Yang's avatar
Michael Yang committed
60
	if stopped {
Jeffrey Morgan's avatar
Jeffrey Morgan committed
61
		// clear all progress lines
Michael Yang's avatar
Michael Yang committed
62
		for i := 0; i < p.pos; i++ {
63
64
65
66
			if i > 0 {
				fmt.Fprint(p.w, "\033[A")
			}
			fmt.Fprint(p.w, "\033[2K\033[1G")
Michael Yang's avatar
Michael Yang committed
67
		}
Michael Yang's avatar
Michael Yang committed
68
69
70
	}

	return stopped
Michael Yang's avatar
Michael Yang committed
71
72
73
74
75
76
77
78
79
}

func (p *Progress) Add(key string, state State) {
	p.mu.Lock()
	defer p.mu.Unlock()

	p.states = append(p.states, state)
}

Michael Yang's avatar
Michael Yang committed
80
func (p *Progress) render() {
Michael Yang's avatar
Michael Yang committed
81
82
83
	p.mu.Lock()
	defer p.mu.Unlock()

Michael Yang's avatar
Michael Yang committed
84
85
86
	fmt.Fprint(p.w, "\033[?25l")
	defer fmt.Fprint(p.w, "\033[?25h")

87
88
89
90
91
92
	// clear already rendered progress lines
	for i := 0; i < p.pos; i++ {
		if i > 0 {
			fmt.Fprint(p.w, "\033[A")
		}
		fmt.Fprint(p.w, "\033[2K\033[1G")
Michael Yang's avatar
Michael Yang committed
93
94
	}

95
96
97
98
99
100
	// render progress lines
	for i, state := range p.states {
		fmt.Fprint(p.w, state.String())
		if i < len(p.states)-1 {
			fmt.Fprint(p.w, "\n")
		}
Michael Yang's avatar
Michael Yang committed
101
102
	}

103
	p.pos = len(p.states)
Michael Yang's avatar
Michael Yang committed
104
105
106
107
108
109
110
111
}

func (p *Progress) start() {
	p.ticker = time.NewTicker(100 * time.Millisecond)
	for range p.ticker.C {
		p.render()
	}
}