progress.go 1.85 KB
Newer Older
Michael Yang's avatar
Michael Yang committed
1
2
3
package progress

import (
4
	"bytes"
Michael Yang's avatar
Michael Yang committed
5
6
7
8
9
10
11
12
13
14
15
	"fmt"
	"io"
	"sync"
	"time"
)

type State interface {
	String() string
}

type Progress struct {
16
17
18
	mu  sync.Mutex
	w   io.Writer
	buf bytes.Buffer
19

Michael Yang's avatar
Michael Yang committed
20
21
22
23
24
25
26
	pos int

	ticker *time.Ticker
	states []State
}

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

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

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

	return false
}

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

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

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

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

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
82
func (p *Progress) render() {
Michael Yang's avatar
Michael Yang committed
83
84
85
	p.mu.Lock()
	defer p.mu.Unlock()

86
87
88
89
90
91
92
	// buffer the terminal update to minimize cursor flickering
	// https://gitlab.gnome.org/GNOME/vte/-/issues/2837#note_2269501
	p.buf.Reset()
	defer p.buf.WriteTo(p.w)

	fmt.Fprint(&p.buf, "\033[?25l")
	defer fmt.Fprint(&p.buf, "\033[?25h")
Michael Yang's avatar
Michael Yang committed
93

94
95
	// move the cursor back to the beginning
	for range p.pos - 1 {
96
		fmt.Fprint(&p.buf, "\033[A")
Michael Yang's avatar
Michael Yang committed
97
	}
98
	fmt.Fprint(&p.buf, "\033[1G")
Michael Yang's avatar
Michael Yang committed
99

100
101
	// render progress lines
	for i, state := range p.states {
102
103
		fmt.Fprint(&p.buf, state.String())
		fmt.Fprintf(&p.buf, "\033[K")
104
		if i < len(p.states)-1 {
105
			fmt.Fprint(&p.buf, "\n")
106
		}
Michael Yang's avatar
Michael Yang committed
107
108
	}

109
	p.pos = len(p.states)
Michael Yang's avatar
Michael Yang committed
110
111
112
113
114
115
116
117
}

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