progress.go 2.16 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
	"fmt"
	"io"
7
	"os"
Michael Yang's avatar
Michael Yang committed
8
9
	"sync"
	"time"
10
11
12
13
14
15
16

	"golang.org/x/term"
)

const (
	defaultTermWidth  = 80
	defaultTermHeight = 24
Michael Yang's avatar
Michael Yang committed
17
18
19
20
21
22
23
)

type State interface {
	String() string
}

type Progress struct {
24
25
26
	mu sync.Mutex
	// buffer output to minimize flickering on all terminals
	w *bufio.Writer
27

Michael Yang's avatar
Michael Yang committed
28
29
30
31
32
33
34
	pos int

	ticker *time.Ticker
	states []State
}

func NewProgress(w io.Writer) *Progress {
35
	p := &Progress{w: bufio.NewWriter(w)}
Michael Yang's avatar
Michael Yang committed
36
37
38
39
	go p.start()
	return p
}

40
func (p *Progress) stop() bool {
41
42
43
44
45
46
	for _, state := range p.states {
		if spinner, ok := state.(*Spinner); ok {
			spinner.Stop()
		}
	}

Michael Yang's avatar
Michael Yang committed
47
48
49
50
	if p.ticker != nil {
		p.ticker.Stop()
		p.ticker = nil
		p.render()
Michael Yang's avatar
Michael Yang committed
51
		return true
Michael Yang's avatar
Michael Yang committed
52
	}
Michael Yang's avatar
Michael Yang committed
53
54
55
56

	return false
}

57
58
59
60
func (p *Progress) Stop() bool {
	stopped := p.stop()
	if stopped {
		fmt.Fprint(p.w, "\n")
61
		p.w.Flush()
62
63
64
65
	}
	return stopped
}

Michael Yang's avatar
Michael Yang committed
66
func (p *Progress) StopAndClear() bool {
67
68
	defer p.w.Flush()

Michael Yang's avatar
Michael Yang committed
69
70
71
	fmt.Fprint(p.w, "\033[?25l")
	defer fmt.Fprint(p.w, "\033[?25h")

72
	stopped := p.stop()
Michael Yang's avatar
Michael Yang committed
73
	if stopped {
Jeffrey Morgan's avatar
Jeffrey Morgan committed
74
		// clear all progress lines
Michael Yang's avatar
lint  
Michael Yang committed
75
		for i := range p.pos {
76
77
78
79
			if i > 0 {
				fmt.Fprint(p.w, "\033[A")
			}
			fmt.Fprint(p.w, "\033[2K\033[1G")
Michael Yang's avatar
Michael Yang committed
80
		}
Michael Yang's avatar
Michael Yang committed
81
82
83
	}

	return stopped
Michael Yang's avatar
Michael Yang committed
84
85
86
87
88
89
90
91
92
}

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
93
func (p *Progress) render() {
94
95
96
97
98
	_, termHeight, err := term.GetSize(int(os.Stderr.Fd()))
	if err != nil {
		termHeight = defaultTermHeight
	}

Michael Yang's avatar
Michael Yang committed
99
100
101
	p.mu.Lock()
	defer p.mu.Unlock()

102
	defer p.w.Flush()
103

104
	// eliminate flickering on terminals that support synchronized output
105
106
	fmt.Fprint(p.w, "\033[?2026h")
	defer fmt.Fprint(p.w, "\033[?2026l")
107

108
109
	fmt.Fprint(p.w, "\033[?25l")
	defer fmt.Fprint(p.w, "\033[?25h")
Michael Yang's avatar
Michael Yang committed
110

111
112
	// move the cursor back to the beginning
	for range p.pos - 1 {
113
		fmt.Fprint(p.w, "\033[A")
Michael Yang's avatar
Michael Yang committed
114
	}
115
	fmt.Fprint(p.w, "\033[1G")
Michael Yang's avatar
Michael Yang committed
116

117
	// render progress lines
118
119
120
	maxHeight := min(len(p.states), termHeight)
	for i := len(p.states) - maxHeight; i < len(p.states); i++ {
		fmt.Fprint(p.w, p.states[i].String(), "\033[K")
121
		if i < len(p.states)-1 {
122
			fmt.Fprint(p.w, "\n")
123
		}
Michael Yang's avatar
Michael Yang committed
124
125
	}

126
	p.pos = len(p.states)
Michael Yang's avatar
Michael Yang committed
127
128
129
130
131
132
133
134
}

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