Godeps: update cheggaaa/pb to the latest version
License: MIT Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Christian Couder committed
Jun 5, 2015 at 23:48 UTC
24de361001585bf963c649e306aec992df66ebb7
7 files changed
+92
-71
Godeps/Godeps.json
+1
-1
@@ -36,7 +36,7 @@
36
},
37
{
38
"ImportPath": "github.com/cheggaaa/pb",
39
- "Rev": "e8c7cc515bfde3e267957a3b110080ceed51354e"
39
+ "Rev": "d7729fd7ec1372c15b83db39834bf842bf2d69fb"
40
},
41
{
42
"ImportPath": "github.com/codahale/hdrhistogram",
Godeps/_workspace/src/github.com/cheggaaa/pb/README.md
+2
-2
@@ -58,10 +58,10 @@ bar.ShowTimeLeft = true
58
bar.ShowSpeed = true
59
60
// sets the width of the progress bar
61
-bar.SetWith(80)
61
+bar.SetWidth(80)
62
63
// sets the width of the progress bar, but if terminal size smaller will be ignored
64
-bar.SetMaxWith(80)
64
+bar.SetMaxWidth(80)
65
66
// convert output to readable format (like KB, MB)
67
bar.SetUnits(pb.U_BYTES)
Godeps/_workspace/src/github.com/cheggaaa/pb/format.go
+8
-5
@@ -6,21 +6,24 @@ import (
6
"strings"
7
)
8
9
+type Units int
10
+
11
const (
12
// By default, without type handle
11
- U_NO = 0
13
+ U_NO Units = iota
14
// Handle as b, Kb, Mb, etc
13
- U_BYTES = 1
15
+ U_BYTES
16
)
17
18
// Format integer
17
-func Format(i int64, units int) string {
19
+func Format(i int64, units Units) string {
20
switch units {
21
case U_BYTES:
22
return FormatBytes(i)
23
+ default:
24
+ // by default just convert to string
25
+ return strconv.FormatInt(i, 10)
26
}
22
- // by default just convert to string
23
- return strconv.Itoa(int(i))
27
}
28
29
// Convert bytes to human readable string. Like a 2 MB, 64.2 KB, 52 B
Godeps/_workspace/src/github.com/cheggaaa/pb/pb.go
+72
-61
@@ -5,6 +5,7 @@ import (
5
"io"
6
"math"
7
"strings"
8
+ "sync"
9
"sync/atomic"
10
"time"
11
)
@@ -24,13 +25,13 @@ var (
25
)
26
27
// Create new progress bar object
27
-func New(total int) (pb *ProgressBar) {
28
+func New(total int) *ProgressBar {
29
return New64(int64(total))
30
}
31
32
// Create new progress bar object uding int64 as total
32
-func New64(total int64) (pb *ProgressBar) {
33
- pb = &ProgressBar{
33
+func New64(total int64) *ProgressBar {
34
+ pb := &ProgressBar{
35
Total: total,
36
RefreshRate: DEFAULT_REFRESH_RATE,
37
ShowPercent: true,
@@ -38,18 +39,17 @@ func New64(total int64) (pb *ProgressBar) {
39
ShowBar: true,
40
ShowTimeLeft: true,
41
ShowFinalTime: true,
42
+ Units: U_NO,
43
ManualUpdate: false,
44
+ isFinish: make(chan struct{}),
45
currentValue: -1,
46
}
44
- pb.Format(FORMAT)
45
- return
47
+ return pb.Format(FORMAT)
48
}
49
50
// Create new object and start
49
-func StartNew(total int) (pb *ProgressBar) {
50
- pb = New(total)
51
- pb.Start()
52
- return
51
+func StartNew(total int) *ProgressBar {
52
+ return New(total).Start()
53
}
54
55
// Callback for custom output
@@ -71,13 +71,16 @@ type ProgressBar struct {
71
Output io.Writer
72
Callback Callback
73
NotPrint bool
74
- Units int
74
+ Units Units
75
Width int
76
ForceWidth bool
77
ManualUpdate bool
78
79
- isFinish bool
79
+ finishOnce sync.Once //Guards isFinish
80
+ isFinish chan struct{}
81
+
82
startTime time.Time
83
+ startValue int64
84
currentValue int64
85
86
prefix, postfix string
@@ -90,16 +93,18 @@ type ProgressBar struct {
93
}
94
95
// Start print
93
-func (pb *ProgressBar) Start() {
96
+func (pb *ProgressBar) Start() *ProgressBar {
97
pb.startTime = time.Now()
98
+ pb.startValue = pb.current
99
if pb.Total == 0 {
100
pb.ShowBar = false
101
pb.ShowTimeLeft = false
102
pb.ShowPercent = false
99
- }
103
+ }
104
if !pb.ManualUpdate {
105
go pb.writer()
106
}
107
+ return pb
108
}
109
110
// Increment current value
@@ -108,8 +113,14 @@ func (pb *ProgressBar) Increment() int {
113
}
114
115
// Set current value
111
-func (pb *ProgressBar) Set(current int) {
112
- atomic.StoreInt64(&pb.current, int64(current))
116
+func (pb *ProgressBar) Set(current int) *ProgressBar {
117
+ return pb.Set64(int64(current))
118
+}
119
+
120
+// Set64 sets the current value as int64
121
+func (pb *ProgressBar) Set64(current int64) *ProgressBar {
122
+ atomic.StoreInt64(&pb.current, current)
123
+ return pb
124
}
125
126
// Add to current value
@@ -122,75 +133,69 @@ func (pb *ProgressBar) Add64(add int64) int64 {
133
}
134
135
// Set prefix string
125
-func (pb *ProgressBar) Prefix(prefix string) (bar *ProgressBar) {
136
+func (pb *ProgressBar) Prefix(prefix string) *ProgressBar {
137
pb.prefix = prefix
138
return pb
139
}
140
141
// Set postfix string
131
-func (pb *ProgressBar) Postfix(postfix string) (bar *ProgressBar) {
142
+func (pb *ProgressBar) Postfix(postfix string) *ProgressBar {
143
pb.postfix = postfix
144
return pb
145
}
146
147
// Set custom format for bar
148
// Example: bar.Format("[=>_]")
138
-func (pb *ProgressBar) Format(format string) (bar *ProgressBar) {
139
- bar = pb
149
+func (pb *ProgressBar) Format(format string) *ProgressBar {
150
formatEntries := strings.Split(format, "")
141
- if len(formatEntries) != 5 {
142
- return
151
+ if len(formatEntries) == 5 {
152
+ pb.BarStart = formatEntries[0]
153
+ pb.BarEnd = formatEntries[4]
154
+ pb.Empty = formatEntries[3]
155
+ pb.Current = formatEntries[1]
156
+ pb.CurrentN = formatEntries[2]
157
}
144
- pb.BarStart = formatEntries[0]
145
- pb.BarEnd = formatEntries[4]
146
- pb.Empty = formatEntries[3]
147
- pb.Current = formatEntries[1]
148
- pb.CurrentN = formatEntries[2]
149
- return
158
+ return pb
159
}
160
161
// Set bar refresh rate
153
-func (pb *ProgressBar) SetRefreshRate(rate time.Duration) (bar *ProgressBar) {
154
- bar = pb
162
+func (pb *ProgressBar) SetRefreshRate(rate time.Duration) *ProgressBar {
163
pb.RefreshRate = rate
156
- return
164
+ return pb
165
}
166
167
// Set units
168
// bar.SetUnits(U_NO) - by default
169
// bar.SetUnits(U_BYTES) - for Mb, Kb, etc
162
-func (pb *ProgressBar) SetUnits(units int) (bar *ProgressBar) {
163
- bar = pb
164
- switch units {
165
- case U_NO, U_BYTES:
166
- pb.Units = units
167
- }
168
- return
170
+func (pb *ProgressBar) SetUnits(units Units) *ProgressBar {
171
+ pb.Units = units
172
+ return pb
173
}
174
175
// Set max width, if width is bigger than terminal width, will be ignored
172
-func (pb *ProgressBar) SetMaxWidth(width int) (bar *ProgressBar) {
173
- bar = pb
176
+func (pb *ProgressBar) SetMaxWidth(width int) *ProgressBar {
177
pb.Width = width
178
pb.ForceWidth = false
176
- return
179
+ return pb
180
}
181
182
// Set bar width
180
-func (pb *ProgressBar) SetWidth(width int) (bar *ProgressBar) {
181
- bar = pb
183
+func (pb *ProgressBar) SetWidth(width int) *ProgressBar {
184
pb.Width = width
185
pb.ForceWidth = true
184
- return
186
+ return pb
187
}
188
189
// End print
190
func (pb *ProgressBar) Finish() {
189
- pb.isFinish = true
190
- pb.write(atomic.LoadInt64(&pb.current))
191
- if !pb.NotPrint {
192
- fmt.Println()
193
- }
191
+ //Protect multiple calls
192
+ pb.finishOnce.Do(func() {
193
+ close(pb.isFinish)
194
+ pb.write(atomic.LoadInt64(&pb.current))
195
+ if !pb.NotPrint {
196
+ fmt.Println()
197
+ }
198
+ })
199
}
200
201
// End print and write string 'str'
@@ -228,7 +233,7 @@ func (pb *ProgressBar) write(current int64) {
233
percent := float64(current) / (float64(pb.Total) / float64(100))
234
percentBox = fmt.Sprintf(" %#.02f %% ", percent)
235
}
231
-
236
+
237
// counters
238
if pb.ShowCounters {
239
if pb.Total > 0 {
@@ -240,22 +245,26 @@ func (pb *ProgressBar) write(current int64) {
245
246
// time left
247
fromStart := time.Now().Sub(pb.startTime)
243
- if pb.isFinish {
248
+ currentFromStart := current - pb.startValue
249
+ select {
250
+ case <-pb.isFinish:
251
if pb.ShowFinalTime {
252
left := (fromStart / time.Second) * time.Second
253
timeLeftBox = left.String()
254
}
248
- } else if pb.ShowTimeLeft && current > 0 {
249
- perEntry := fromStart / time.Duration(current)
250
- left := time.Duration(pb.Total-current) * perEntry
251
- left = (left / time.Second) * time.Second
252
- timeLeftBox = left.String()
255
+ default:
256
+ if pb.ShowTimeLeft && currentFromStart > 0 {
257
+ perEntry := fromStart / time.Duration(currentFromStart)
258
+ left := time.Duration(pb.Total-currentFromStart) * perEntry
259
+ left = (left / time.Second) * time.Second
260
+ timeLeftBox = left.String()
261
+ }
262
}
263
264
// speed
256
- if pb.ShowSpeed && current > 0 {
265
+ if pb.ShowSpeed && currentFromStart > 0 {
266
fromStart := time.Now().Sub(pb.startTime)
258
- speed := float64(current) / (float64(fromStart) / float64(time.Second))
267
+ speed := float64(currentFromStart) / (float64(fromStart) / float64(time.Second))
268
speedBox = Format(int64(speed), pb.Units) + "/s "
269
}
270
@@ -324,12 +333,14 @@ func (pb *ProgressBar) Update() {
333
334
// Internal loop for writing progressbar
335
func (pb *ProgressBar) writer() {
336
+ pb.Update()
337
for {
328
- if pb.isFinish {
329
- break
338
+ select {
339
+ case <-pb.isFinish:
340
+ return
341
+ case <-time.After(pb.RefreshRate):
342
+ pb.Update()
343
}
331
- pb.Update()
332
- time.Sleep(pb.RefreshRate)
344
}
345
}
346
Godeps/_workspace/src/github.com/cheggaaa/pb/pb_nix.go
+1
-1
@@ -1,4 +1,4 @@
1
-// +build linux darwin freebsd openbsd
1
+// +build linux darwin freebsd netbsd openbsd
2
3
package pb
4
Godeps/_workspace/src/github.com/cheggaaa/pb/pb_test.go
+7
@@ -28,3 +28,10 @@ func Test_Width(t *testing.T) {
28
bar.Increment()
29
bar.Finish()
30
}
31
+
32
+func Test_MultipleFinish(t *testing.T) {
33
+ bar := New(5000)
34
+ bar.Add(2000)
35
+ bar.Finish()
36
+ bar.Finish()
37
+}
Godeps/_workspace/src/github.com/cheggaaa/pb/pb_x.go
+1
-1
@@ -1,4 +1,4 @@
1
-// +build linux darwin freebsd openbsd solaris
1
+// +build linux darwin freebsd netbsd openbsd solaris
2
3
package pb
4