Imported Upstream version 1.0.5
Peter Colberg
7 years ago
12 | 12 |
)
|
13 | 13 |
|
14 | 14 |
// Current version
|
15 | |
const Version = "1.0.2"
|
|
15 |
const Version = "1.0.5"
|
16 | 16 |
|
17 | 17 |
const (
|
18 | 18 |
// Default refresh rate - 200ms
|
|
80 | 80 |
Width int
|
81 | 81 |
ForceWidth bool
|
82 | 82 |
ManualUpdate bool
|
|
83 |
AutoStat bool
|
83 | 84 |
|
84 | 85 |
// Default width for the time box.
|
85 | 86 |
UnitsWidth int
|
|
114 | 115 |
if pb.Total == 0 {
|
115 | 116 |
pb.ShowTimeLeft = false
|
116 | 117 |
pb.ShowPercent = false
|
|
118 |
pb.AutoStat = false
|
117 | 119 |
}
|
118 | 120 |
if !pb.ManualUpdate {
|
119 | 121 |
pb.Update() // Initial printing of the bar before running the bar refresher.
|
|
125 | 127 |
// Increment current value
|
126 | 128 |
func (pb *ProgressBar) Increment() int {
|
127 | 129 |
return pb.Add(1)
|
|
130 |
}
|
|
131 |
|
|
132 |
// Get current value
|
|
133 |
func (pb *ProgressBar) Get() int64 {
|
|
134 |
c := atomic.LoadInt64(&pb.current)
|
|
135 |
return c
|
128 | 136 |
}
|
129 | 137 |
|
130 | 138 |
// Set current value
|
|
241 | 249 |
}
|
242 | 250 |
|
243 | 251 |
// Create new proxy reader over bar
|
|
252 |
// Takes io.Reader or io.ReadCloser
|
244 | 253 |
func (pb *ProgressBar) NewProxyReader(r io.Reader) *Reader {
|
245 | 254 |
return &Reader{r, pb}
|
246 | 255 |
}
|
|
397 | 406 |
pb.write(c)
|
398 | 407 |
pb.currentValue = c
|
399 | 408 |
}
|
|
409 |
if pb.AutoStat {
|
|
410 |
if c == 0 {
|
|
411 |
pb.startTime = time.Now()
|
|
412 |
pb.startValue = 0
|
|
413 |
} else if c >= pb.Total && pb.isFinish != true {
|
|
414 |
pb.Finish()
|
|
415 |
}
|
|
416 |
}
|
400 | 417 |
}
|
401 | 418 |
|
402 | 419 |
func (pb *ProgressBar) String() string {
|
2 | 2 |
import (
|
3 | 3 |
"strings"
|
4 | 4 |
"testing"
|
|
5 |
"time"
|
5 | 6 |
|
6 | 7 |
"github.com/fatih/color"
|
7 | 8 |
"github.com/mattn/go-colorable"
|
|
55 | 56 |
bar.Finish()
|
56 | 57 |
bar.Finish()
|
57 | 58 |
}
|
|
59 |
|
|
60 |
func Test_AutoStat(t *testing.T) {
|
|
61 |
bar := New(5)
|
|
62 |
bar.AutoStat = true
|
|
63 |
bar.Start()
|
|
64 |
time.Sleep(2 * time.Second)
|
|
65 |
//real start work
|
|
66 |
for i := 0; i < 5; i++ {
|
|
67 |
time.Sleep(500 * time.Millisecond)
|
|
68 |
bar.Increment()
|
|
69 |
}
|
|
70 |
//real finish work
|
|
71 |
time.Sleep(2 * time.Second)
|
|
72 |
bar.Finish()
|
|
73 |
}
|
14 | 14 |
r.bar.Add(n)
|
15 | 15 |
return
|
16 | 16 |
}
|
|
17 |
|
|
18 |
// Close the reader when it implements io.Closer
|
|
19 |
func (r *Reader) Close() (err error) {
|
|
20 |
if closer, ok := r.Reader.(io.Closer); ok {
|
|
21 |
return closer.Close()
|
|
22 |
}
|
|
23 |
return
|
|
24 |
}
|