summaryrefslogtreecommitdiff
path: root/vendor/gopkg.in/cheggaaa/pb.v1/pb.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/gopkg.in/cheggaaa/pb.v1/pb.go')
-rw-r--r--vendor/gopkg.in/cheggaaa/pb.v1/pb.go193
1 files changed, 126 insertions, 67 deletions
diff --git a/vendor/gopkg.in/cheggaaa/pb.v1/pb.go b/vendor/gopkg.in/cheggaaa/pb.v1/pb.go
index f1f15bff3..e03291b89 100644
--- a/vendor/gopkg.in/cheggaaa/pb.v1/pb.go
+++ b/vendor/gopkg.in/cheggaaa/pb.v1/pb.go
@@ -13,7 +13,7 @@ import (
)
// Current version
-const Version = "1.0.6"
+const Version = "1.0.27"
const (
// Default refresh rate - 200ms
@@ -37,18 +37,17 @@ func New(total int) *ProgressBar {
// Create new progress bar object using int64 as total
func New64(total int64) *ProgressBar {
pb := &ProgressBar{
- Total: total,
- RefreshRate: DEFAULT_REFRESH_RATE,
- ShowPercent: true,
- ShowCounters: true,
- ShowBar: true,
- ShowTimeLeft: true,
- ShowFinalTime: true,
- Units: U_NO,
- ManualUpdate: false,
- finish: make(chan struct{}),
- currentValue: -1,
- mu: new(sync.Mutex),
+ Total: total,
+ RefreshRate: DEFAULT_REFRESH_RATE,
+ ShowPercent: true,
+ ShowCounters: true,
+ ShowBar: true,
+ ShowTimeLeft: true,
+ ShowElapsedTime: false,
+ ShowFinalTime: true,
+ Units: U_NO,
+ ManualUpdate: false,
+ finish: make(chan struct{}),
}
return pb.Format(FORMAT)
}
@@ -67,13 +66,14 @@ func StartNew(total int) *ProgressBar {
type Callback func(out string)
type ProgressBar struct {
- current int64 // current must be first member of struct (https://code.google.com/p/go/issues/detail?id=5278)
+ current int64 // current must be first member of struct (https://code.google.com/p/go/issues/detail?id=5278)
+ previous int64
Total int64
RefreshRate time.Duration
ShowPercent, ShowCounters bool
ShowSpeed, ShowTimeLeft, ShowBar bool
- ShowFinalTime bool
+ ShowFinalTime, ShowElapsedTime bool
Output io.Writer
Callback Callback
NotPrint bool
@@ -91,13 +91,14 @@ type ProgressBar struct {
finish chan struct{}
isFinish bool
- startTime time.Time
- startValue int64
- currentValue int64
+ startTime time.Time
+ startValue int64
+
+ changeTime time.Time
prefix, postfix string
- mu *sync.Mutex
+ mu sync.Mutex
lastPrint string
BarStart string
@@ -112,8 +113,8 @@ type ProgressBar struct {
// Start print
func (pb *ProgressBar) Start() *ProgressBar {
pb.startTime = time.Now()
- pb.startValue = pb.current
- if pb.Total == 0 {
+ pb.startValue = atomic.LoadInt64(&pb.current)
+ if atomic.LoadInt64(&pb.Total) == 0 {
pb.ShowTimeLeft = false
pb.ShowPercent = false
pb.AutoStat = false
@@ -158,12 +159,16 @@ func (pb *ProgressBar) Add64(add int64) int64 {
// Set prefix string
func (pb *ProgressBar) Prefix(prefix string) *ProgressBar {
+ pb.mu.Lock()
+ defer pb.mu.Unlock()
pb.prefix = prefix
return pb
}
// Set postfix string
func (pb *ProgressBar) Postfix(postfix string) *ProgressBar {
+ pb.mu.Lock()
+ defer pb.mu.Unlock()
pb.postfix = postfix
return pb
}
@@ -173,7 +178,7 @@ func (pb *ProgressBar) Postfix(postfix string) *ProgressBar {
// Example: bar.Format("[\x00=\x00>\x00-\x00]") // \x00 is the delimiter
func (pb *ProgressBar) Format(format string) *ProgressBar {
var formatEntries []string
- if len(format) == 5 {
+ if utf8.RuneCountInString(format) == 5 {
formatEntries = strings.Split(format, "")
} else {
formatEntries = strings.Split(format, "\x00")
@@ -221,7 +226,9 @@ func (pb *ProgressBar) Finish() {
//Protect multiple calls
pb.finishOnce.Do(func() {
close(pb.finish)
- pb.write(atomic.LoadInt64(&pb.current))
+ pb.write(atomic.LoadInt64(&pb.Total), atomic.LoadInt64(&pb.current))
+ pb.mu.Lock()
+ defer pb.mu.Unlock()
switch {
case pb.Output != nil:
fmt.Fprintln(pb.Output)
@@ -232,6 +239,13 @@ func (pb *ProgressBar) Finish() {
})
}
+// IsFinished return boolean
+func (pb *ProgressBar) IsFinished() bool {
+ pb.mu.Lock()
+ defer pb.mu.Unlock()
+ return pb.isFinish
+}
+
// End print and write string 'str'
func (pb *ProgressBar) FinishPrint(str string) {
pb.Finish()
@@ -262,16 +276,18 @@ func (pb *ProgressBar) NewProxyReader(r io.Reader) *Reader {
return &Reader{r, pb}
}
-func (pb *ProgressBar) write(current int64) {
+func (pb *ProgressBar) write(total, current int64) {
+ pb.mu.Lock()
+ defer pb.mu.Unlock()
width := pb.GetWidth()
- var percentBox, countersBox, timeLeftBox, speedBox, barBox, end, out string
+ var percentBox, countersBox, timeLeftBox, timeSpentBox, speedBox, barBox, end, out string
// percents
if pb.ShowPercent {
var percent float64
- if pb.Total > 0 {
- percent = float64(current) / (float64(pb.Total) / float64(100))
+ if total > 0 {
+ percent = float64(current) / (float64(total) / float64(100))
} else {
percent = float64(current) / float64(100)
}
@@ -281,17 +297,24 @@ func (pb *ProgressBar) write(current int64) {
// counters
if pb.ShowCounters {
current := Format(current).To(pb.Units).Width(pb.UnitsWidth)
- if pb.Total > 0 {
- total := Format(pb.Total).To(pb.Units).Width(pb.UnitsWidth)
- countersBox = fmt.Sprintf(" %s / %s ", current, total)
+ if total > 0 {
+ totalS := Format(total).To(pb.Units).Width(pb.UnitsWidth)
+ countersBox = fmt.Sprintf(" %s / %s ", current, totalS)
} else {
countersBox = fmt.Sprintf(" %s / ? ", current)
}
}
// time left
- fromStart := time.Now().Sub(pb.startTime)
currentFromStart := current - pb.startValue
+ fromStart := time.Now().Sub(pb.startTime)
+ lastChangeTime := pb.changeTime
+ fromChange := lastChangeTime.Sub(pb.startTime)
+
+ if pb.ShowElapsedTime {
+ timeSpentBox = fmt.Sprintf(" %s ", (fromStart/time.Second)*time.Second)
+ }
+
select {
case <-pb.finish:
if pb.ShowFinalTime {
@@ -301,17 +324,20 @@ func (pb *ProgressBar) write(current int64) {
}
default:
if pb.ShowTimeLeft && currentFromStart > 0 {
- perEntry := fromStart / time.Duration(currentFromStart)
+ perEntry := fromChange / time.Duration(currentFromStart)
var left time.Duration
- if pb.Total > 0 {
- left = time.Duration(pb.Total-currentFromStart) * perEntry
+ if total > 0 {
+ left = time.Duration(total-currentFromStart) * perEntry
+ left -= time.Since(lastChangeTime)
left = (left / time.Second) * time.Second
} else {
left = time.Duration(currentFromStart) * perEntry
left = (left / time.Second) * time.Second
}
- timeLeft := Format(int64(left)).To(U_DURATION).String()
- timeLeftBox = fmt.Sprintf(" %s", timeLeft)
+ if left > 0 {
+ timeLeft := Format(int64(left)).To(U_DURATION).String()
+ timeLeftBox = fmt.Sprintf(" %s", timeLeft)
+ }
}
}
@@ -326,30 +352,38 @@ func (pb *ProgressBar) write(current int64) {
speedBox = " " + Format(int64(speed)).To(pb.Units).Width(pb.UnitsWidth).PerSec().String()
}
- barWidth := escapeAwareRuneCountInString(countersBox + pb.BarStart + pb.BarEnd + percentBox + timeLeftBox + speedBox + pb.prefix + pb.postfix)
+ barWidth := escapeAwareRuneCountInString(countersBox + pb.BarStart + pb.BarEnd + percentBox + timeSpentBox + timeLeftBox + speedBox + pb.prefix + pb.postfix)
// bar
if pb.ShowBar {
size := width - barWidth
if size > 0 {
- if pb.Total > 0 {
- curCount := int(math.Ceil((float64(current) / float64(pb.Total)) * float64(size)))
- emptCount := size - curCount
+ if total > 0 {
+ curSize := int(math.Ceil((float64(current) / float64(total)) * float64(size)))
+ emptySize := size - curSize
barBox = pb.BarStart
- if emptCount < 0 {
- emptCount = 0
+ if emptySize < 0 {
+ emptySize = 0
}
- if curCount > size {
- curCount = size
+ if curSize > size {
+ curSize = size
}
- if emptCount <= 0 {
- barBox += strings.Repeat(pb.Current, curCount)
- } else if curCount > 0 {
- barBox += strings.Repeat(pb.Current, curCount-1) + pb.CurrentN
+
+ cursorLen := escapeAwareRuneCountInString(pb.Current)
+ if emptySize <= 0 {
+ barBox += strings.Repeat(pb.Current, curSize/cursorLen)
+ } else if curSize > 0 {
+ cursorEndLen := escapeAwareRuneCountInString(pb.CurrentN)
+ cursorRepetitions := (curSize - cursorEndLen) / cursorLen
+ barBox += strings.Repeat(pb.Current, cursorRepetitions)
+ barBox += pb.CurrentN
}
- barBox += strings.Repeat(pb.Empty, emptCount) + pb.BarEnd
+
+ emptyLen := escapeAwareRuneCountInString(pb.Empty)
+ barBox += strings.Repeat(pb.Empty, emptySize/emptyLen)
+ barBox += pb.BarEnd
} else {
- barBox = pb.BarStart
pos := size - int(current)%int(size)
+ barBox = pb.BarStart
if pos-1 > 0 {
barBox += strings.Repeat(pb.Empty, pos-1)
}
@@ -363,17 +397,18 @@ func (pb *ProgressBar) write(current int64) {
}
// check len
- out = pb.prefix + countersBox + barBox + percentBox + speedBox + timeLeftBox + pb.postfix
- if escapeAwareRuneCountInString(out) < width {
- end = strings.Repeat(" ", width-utf8.RuneCountInString(out))
+ out = pb.prefix + timeSpentBox + countersBox + barBox + percentBox + speedBox + timeLeftBox + pb.postfix
+
+ if cl := escapeAwareRuneCountInString(out); cl < width {
+ end = strings.Repeat(" ", width-cl)
}
// and print!
- pb.mu.Lock()
pb.lastPrint = out + end
- pb.mu.Unlock()
+ isFinish := pb.isFinish
+
switch {
- case pb.isFinish:
+ case isFinish:
return
case pb.Output != nil:
fmt.Fprint(pb.Output, "\r"+out+end)
@@ -406,24 +441,55 @@ func (pb *ProgressBar) GetWidth() int {
// Write the current state of the progressbar
func (pb *ProgressBar) Update() {
c := atomic.LoadInt64(&pb.current)
- if pb.AlwaysUpdate || c != pb.currentValue {
- pb.write(c)
- pb.currentValue = c
+ p := atomic.LoadInt64(&pb.previous)
+ t := atomic.LoadInt64(&pb.Total)
+ if p != c {
+ pb.mu.Lock()
+ pb.changeTime = time.Now()
+ pb.mu.Unlock()
+ atomic.StoreInt64(&pb.previous, c)
}
+ pb.write(t, c)
if pb.AutoStat {
if c == 0 {
pb.startTime = time.Now()
pb.startValue = 0
- } else if c >= pb.Total && pb.isFinish != true {
+ } else if c >= t && pb.isFinish != true {
pb.Finish()
}
}
}
+// String return the last bar print
func (pb *ProgressBar) String() string {
+ pb.mu.Lock()
+ defer pb.mu.Unlock()
return pb.lastPrint
}
+// SetTotal atomically sets new total count
+func (pb *ProgressBar) SetTotal(total int) *ProgressBar {
+ return pb.SetTotal64(int64(total))
+}
+
+// SetTotal64 atomically sets new total count
+func (pb *ProgressBar) SetTotal64(total int64) *ProgressBar {
+ atomic.StoreInt64(&pb.Total, total)
+ return pb
+}
+
+// Reset bar and set new total count
+// Does effect only on finished bar
+func (pb *ProgressBar) Reset(total int) *ProgressBar {
+ pb.mu.Lock()
+ defer pb.mu.Unlock()
+ if pb.isFinish {
+ pb.SetTotal(total).Set(0)
+ atomic.StoreInt64(&pb.previous, 0)
+ }
+ return pb
+}
+
// Internal loop for refreshing the progressbar
func (pb *ProgressBar) refresher() {
for {
@@ -435,10 +501,3 @@ func (pb *ProgressBar) refresher() {
}
}
}
-
-type window struct {
- Row uint16
- Col uint16
- Xpixel uint16
- Ypixel uint16
-}