"vscode:/vscode.git/clone" did not exist on "1effba4c7030615136a34608ce9f8ddbebd0934a"
progress.go 1.93 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
	// buffer output to minimize flickering on all terminals
87
88
89
	p.buf.Reset()
	defer p.buf.WriteTo(p.w)

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

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

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

103
104
	// render progress lines
	for i, state := range p.states {
105
106
		fmt.Fprint(&p.buf, state.String())
		fmt.Fprintf(&p.buf, "\033[K")
107
		if i < len(p.states)-1 {
108
			fmt.Fprint(&p.buf, "\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()
	}
}