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

import (
4
	"bufio"
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
	// buffer output to minimize flickering on all terminals
	w *bufio.Writer
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 {
27
	p := &Progress{w: bufio.NewWriter(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
func (p *Progress) Stop() bool {
	stopped := p.stop()
	if stopped {
		fmt.Fprint(p.w, "\n")
53
		p.w.Flush()
54
55
56
57
	}
	return stopped
}

Michael Yang's avatar
Michael Yang committed
58
func (p *Progress) StopAndClear() bool {
59
60
	defer p.w.Flush()

Michael Yang's avatar
Michael Yang committed
61
62
63
	fmt.Fprint(p.w, "\033[?25l")
	defer fmt.Fprint(p.w, "\033[?25h")

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

	return stopped
Michael Yang's avatar
Michael Yang committed
76
77
78
79
80
81
82
83
84
}

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

89
	defer p.w.Flush()
90

91
	// eliminate flickering on terminals that support synchronized output
92
93
	fmt.Fprint(p.w, "\033[?2026h")
	defer fmt.Fprint(p.w, "\033[?2026l")
94

95
96
	fmt.Fprint(p.w, "\033[?25l")
	defer fmt.Fprint(p.w, "\033[?25h")
Michael Yang's avatar
Michael Yang committed
97

98
99
	// move the cursor back to the beginning
	for range p.pos - 1 {
100
		fmt.Fprint(p.w, "\033[A")
Michael Yang's avatar
Michael Yang committed
101
	}
102
	fmt.Fprint(p.w, "\033[1G")
Michael Yang's avatar
Michael Yang committed
103

104
105
	// render progress lines
	for i, state := range p.states {
106
		fmt.Fprint(p.w, state.String(), "\033[K")
107
		if i < len(p.states)-1 {
108
			fmt.Fprint(p.w, "\n")
109
		}
Michael Yang's avatar
Michael Yang committed
110
111
	}

112
	p.pos = len(p.states)
Michael Yang's avatar
Michael Yang committed
113
114
115
116
117
118
119
120
}

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