-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathprogress.go
199 lines (161 loc) · 4.53 KB
/
progress.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package progress
import (
"bufio"
"fmt"
"os"
"unicode"
"github.com/cheggaaa/pb"
"golang.org/x/crypto/ssh/terminal"
)
// Meter is an interface to show progress to the user
type Meter interface {
// Start progress with max "total" steps
Start(label string, total float64)
// set progress to the "current" step
Set(current float64)
// set "total" steps needed
SetTotal(total float64)
// Finish the progress display
Finished()
// Indicate indefinite activity by showing a spinner
Spin(msg string)
// interface for writer
Write(p []byte) (n int, err error)
// notify the user of miscellaneous events
Notify(string)
}
// NullProgress is a Meter that does nothing
type NullProgress struct {
}
// Start does nothing
func (t *NullProgress) Start(label string, total float64) {
}
// Set does nothing
func (t *NullProgress) Set(current float64) {
}
// SetTotal does nothing
func (t *NullProgress) SetTotal(total float64) {
}
// Finished does nothing
func (t *NullProgress) Finished() {
}
// Write does nothing
func (t *NullProgress) Write(p []byte) (n int, err error) {
return len(p), nil
}
// Notify does nothing
func (t *NullProgress) Notify(string) {}
// Spin does nothing
func (t *NullProgress) Spin(msg string) {
}
const clearUntilEOL = "\033[K"
// TextProgress show progress on the terminal
type TextProgress struct {
Meter
pbar *pb.ProgressBar
spinStep int
}
// NewTextProgress returns a new TextProgress type
func NewTextProgress() *TextProgress {
return &TextProgress{}
}
// Start starts showing progress
func (t *TextProgress) Start(label string, total float64) {
t.pbar = pb.New64(int64(total))
t.pbar.ShowSpeed = true
t.pbar.Units = pb.U_BYTES
t.pbar.Prefix(label)
t.pbar.Start()
}
// Set sets the progress to the current value
func (t *TextProgress) Set(current float64) {
t.pbar.Set(int(current))
}
// SetTotal set the total steps needed
func (t *TextProgress) SetTotal(total float64) {
t.pbar.Total = int64(total)
}
// Finished stops displaying the progress
func (t *TextProgress) Finished() {
if t.pbar != nil {
// workaround silly pb that always does a fmt.Println() on
// finish (unless NotPrint is set)
t.pbar.NotPrint = true
t.pbar.Finish()
t.pbar.NotPrint = false
}
fmt.Printf("\r\033[K")
}
// Write is there so that progress can implment a Writer and can be
// used to display progress of io operations
func (t *TextProgress) Write(p []byte) (n int, err error) {
return t.pbar.Write(p)
}
// Spin advances a spinner, i.e. can be used to show progress for operations
// that have a unknown duration
func (t *TextProgress) Spin(msg string) {
states := `|/-\`
// clear until end of line
fmt.Printf("\r[%c] %s%s", states[t.spinStep], msg, clearUntilEOL)
t.spinStep++
if t.spinStep >= len(states) {
t.spinStep = 0
}
}
// Agreed asks the user whether they agree to the given license text
func (t *TextProgress) Agreed(intro, license string) bool {
if _, err := fmt.Println(intro); err != nil {
return false
}
// XXX: send it through a pager instead of this ugly thing
if _, err := fmt.Println(license); err != nil {
return false
}
reader := bufio.NewReader(os.Stdin)
if _, err := fmt.Print("Do you agree? [y/n] "); err != nil {
return false
}
r, _, err := reader.ReadRune()
if err != nil {
return false
}
return unicode.ToLower(r) == 'y'
}
// Notify the user of miscellaneous events
func (*TextProgress) Notify(msg string) {
fmt.Printf("\r%s%s\n", msg, clearUntilEOL)
}
// MakeProgressBar creates an appropriate progress (which may be a
// NullProgress bar if there is no associated terminal).
func MakeProgressBar() Meter {
var pbar Meter
if attachedToTerminal() {
pbar = NewTextProgress()
} else {
pbar = &NullProgress{}
}
return pbar
}
// attachedToTerminal returns true if the calling process is attached to
// a terminal device.
var attachedToTerminal = func() bool {
fd := int(os.Stdin.Fd())
return terminal.IsTerminal(fd)
}