| 0 | 0 |
package uiprogress
|
| 1 | 1 |
|
| 2 | |
import (
|
| 3 | |
"bytes"
|
| 4 | |
"errors"
|
| 5 | |
"fmt"
|
| 6 | |
"sync"
|
| 7 | |
"time"
|
| 8 | |
|
| 9 | |
"github.com/gosuri/uiprogress/util/strutil"
|
| 10 | |
)
|
|
2 |
import "bytes"
|
| 11 | 3 |
|
| 12 | 4 |
var (
|
| 13 | 5 |
// Fill is the default character representing completed progress
|
|
| 29 | 21 |
Width = 70
|
| 30 | 22 |
|
| 31 | 23 |
// ErrMaxCurrentReached is error when trying to set current value that exceeds the total value
|
| 32 | |
ErrMaxCurrentReached = errors.New("errors: current value is greater total value")
|
|
24 |
// ErrMaxCurrentReached = errors.New("errors: current value is greater total value")
|
| 33 | 25 |
)
|
| 34 | 26 |
|
| 35 | 27 |
// Bar represents a progress bar
|
|
| 52 | 44 |
// Empty is the character that represents the empty progress. Default is '-'
|
| 53 | 45 |
Empty byte
|
| 54 | 46 |
|
| 55 | |
// TimeStated is time progress began
|
| 56 | |
TimeStarted time.Time
|
| 57 | |
|
| 58 | 47 |
// Width is the width of the progress bar
|
| 59 | 48 |
Width int
|
| 60 | 49 |
|
| 61 | |
// timeElased is the time elapsed for the progress
|
| 62 | |
timeElapsed time.Duration
|
| 63 | |
current int
|
|
50 |
currentUpdateCh chan int
|
| 64 | 51 |
|
| 65 | |
mtx *sync.RWMutex
|
|
52 |
redrawRequestCh chan redrawRequest
|
| 66 | 53 |
|
| 67 | |
appendFuncs []DecoratorFunc
|
| 68 | |
prependFuncs []DecoratorFunc
|
|
54 |
// appendFuncs []DecoratorFunc
|
|
55 |
// prependFuncs []DecoratorFunc
|
| 69 | 56 |
}
|
| 70 | 57 |
|
| 71 | 58 |
// DecoratorFunc is a function that can be prepended and appended to the progress bar
|
|
| 73 | 60 |
|
| 74 | 61 |
// NewBar returns a new progress bar
|
| 75 | 62 |
func NewBar(total int) *Bar {
|
| 76 | |
return &Bar{
|
| 77 | |
Total: total,
|
| 78 | |
Width: Width,
|
| 79 | |
LeftEnd: LeftEnd,
|
| 80 | |
RightEnd: RightEnd,
|
| 81 | |
Head: Head,
|
| 82 | |
Fill: Fill,
|
| 83 | |
Empty: Empty,
|
|
63 |
b := &Bar{
|
|
64 |
Total: total,
|
|
65 |
Width: Width,
|
|
66 |
LeftEnd: LeftEnd,
|
|
67 |
RightEnd: RightEnd,
|
|
68 |
Head: Head,
|
|
69 |
Fill: Fill,
|
|
70 |
Empty: Empty,
|
|
71 |
currentUpdateCh: make(chan int),
|
|
72 |
redrawRequestCh: make(chan redrawRequest),
|
|
73 |
}
|
|
74 |
go b.server()
|
|
75 |
return b
|
|
76 |
}
|
| 84 | 77 |
|
| 85 | |
mtx: &sync.RWMutex{},
|
|
78 |
type redrawRequest struct {
|
|
79 |
bufch chan []byte
|
|
80 |
}
|
|
81 |
|
|
82 |
func (b *Bar) server() {
|
|
83 |
var current int
|
|
84 |
// blockStartTime := time.Now()
|
|
85 |
// timePerItemEstimate time.Duration
|
|
86 |
// remainingTime time.Duration
|
|
87 |
for {
|
|
88 |
select {
|
|
89 |
case n := <-b.currentUpdateCh:
|
|
90 |
current += n
|
|
91 |
// blockStartTime = time.Now()
|
|
92 |
case r := <-b.redrawRequestCh:
|
|
93 |
r.bufch <- b.buffer(current)
|
|
94 |
}
|
| 86 | 95 |
}
|
| 87 | 96 |
}
|
| 88 | 97 |
|
| 89 | |
// Set the current count of the bar. It returns ErrMaxCurrentReached when trying n exceeds the total value. This is atomic operation and concurancy safe.
|
| 90 | |
func (b *Bar) Set(n int) error {
|
| 91 | |
b.mtx.Lock()
|
| 92 | |
defer b.mtx.Unlock()
|
| 93 | |
|
| 94 | |
if n > b.Total {
|
| 95 | |
return ErrMaxCurrentReached
|
| 96 | |
}
|
| 97 | |
b.current = n
|
| 98 | |
return nil
|
|
98 |
func (b *Bar) Update(n int) {
|
|
99 |
b.currentUpdateCh <- n
|
| 99 | 100 |
}
|
| 100 | 101 |
|
| 101 | |
// Incr increments the current value by 1, time elapsed to current time and returns true. It returns false if the cursor has reached or exceeds total value.
|
| 102 | |
func (b *Bar) Incr() bool {
|
| 103 | |
b.mtx.Lock()
|
| 104 | |
defer b.mtx.Unlock()
|
| 105 | |
|
| 106 | |
n := b.current + 1
|
| 107 | |
if n > b.Total {
|
| 108 | |
return false
|
| 109 | |
}
|
| 110 | |
var t time.Time
|
| 111 | |
if b.TimeStarted == t {
|
| 112 | |
b.TimeStarted = time.Now()
|
| 113 | |
}
|
| 114 | |
b.timeElapsed = time.Since(b.TimeStarted)
|
| 115 | |
b.current = n
|
| 116 | |
return true
|
| 117 | |
}
|
| 118 | |
|
| 119 | |
// Current returns the current progress of the bar
|
| 120 | |
func (b *Bar) Current() int {
|
| 121 | |
b.mtx.RLock()
|
| 122 | |
defer b.mtx.RUnlock()
|
| 123 | |
return b.current
|
| 124 | |
}
|
| 125 | |
|
| 126 | |
// AppendFunc runs the decorator function and renders the output on the right of the progress bar
|
| 127 | |
func (b *Bar) AppendFunc(f DecoratorFunc) *Bar {
|
| 128 | |
b.mtx.Lock()
|
| 129 | |
defer b.mtx.Unlock()
|
| 130 | |
b.appendFuncs = append(b.appendFuncs, f)
|
| 131 | |
return b
|
| 132 | |
}
|
| 133 | |
|
| 134 | |
// AppendCompleted appends the completion percent to the progress bar
|
| 135 | |
func (b *Bar) AppendCompleted() *Bar {
|
| 136 | |
b.AppendFunc(func(b *Bar) string {
|
| 137 | |
return b.CompletedPercentString()
|
| 138 | |
})
|
| 139 | |
return b
|
| 140 | |
}
|
| 141 | |
|
| 142 | |
// AppendElapsed appends the time elapsed the be progress bar
|
| 143 | |
func (b *Bar) AppendElapsed() *Bar {
|
| 144 | |
b.AppendFunc(func(b *Bar) string {
|
| 145 | |
return strutil.PadLeft(b.TimeElapsedString(), 5, ' ')
|
| 146 | |
})
|
| 147 | |
return b
|
| 148 | |
}
|
| 149 | |
|
| 150 | |
// PrependFunc runs decorator function and render the output left the progress bar
|
| 151 | |
func (b *Bar) PrependFunc(f DecoratorFunc) *Bar {
|
| 152 | |
b.mtx.Lock()
|
| 153 | |
defer b.mtx.Unlock()
|
| 154 | |
b.prependFuncs = append(b.prependFuncs, f)
|
| 155 | |
return b
|
| 156 | |
}
|
| 157 | |
|
| 158 | |
// PrependCompleted prepends the precent completed to the progress bar
|
| 159 | |
func (b *Bar) PrependCompleted() *Bar {
|
| 160 | |
b.PrependFunc(func(b *Bar) string {
|
| 161 | |
return b.CompletedPercentString()
|
| 162 | |
})
|
| 163 | |
return b
|
| 164 | |
}
|
| 165 | |
|
| 166 | |
// PrependElapsed prepends the time elapsed to the begining of the bar
|
| 167 | |
func (b *Bar) PrependElapsed() *Bar {
|
| 168 | |
b.PrependFunc(func(b *Bar) string {
|
| 169 | |
return strutil.PadLeft(b.TimeElapsedString(), 5, ' ')
|
| 170 | |
})
|
| 171 | |
return b
|
| 172 | |
}
|
| 173 | |
|
| 174 | |
// Bytes returns the byte presentation of the progress bar
|
| 175 | |
func (b *Bar) Bytes() []byte {
|
| 176 | |
completedWidth := int(float64(b.Width) * (b.CompletedPercent() / 100.00))
|
|
102 |
func (b *Bar) buffer(current int) []byte {
|
|
103 |
completedPercent := int(100 * float64(current) / float64(b.Total))
|
|
104 |
completedWidth := completedPercent * b.Width / 100
|
| 177 | 105 |
|
| 178 | 106 |
// add fill and empty bits
|
| 179 | 107 |
var buf bytes.Buffer
|
|
| 194 | 122 |
pb[0], pb[len(pb)-1] = b.LeftEnd, b.RightEnd
|
| 195 | 123 |
|
| 196 | 124 |
// render append functions to the right of the bar
|
| 197 | |
for _, f := range b.appendFuncs {
|
| 198 | |
pb = append(pb, ' ')
|
| 199 | |
pb = append(pb, []byte(f(b))...)
|
| 200 | |
}
|
|
125 |
// for _, f := range b.appendFuncs {
|
|
126 |
// pb = append(pb, ' ')
|
|
127 |
// pb = append(pb, []byte(f(b))...)
|
|
128 |
// }
|
| 201 | 129 |
|
| 202 | 130 |
// render prepend functions to the left of the bar
|
| 203 | |
for _, f := range b.prependFuncs {
|
| 204 | |
args := []byte(f(b))
|
| 205 | |
args = append(args, ' ')
|
| 206 | |
pb = append(args, pb...)
|
| 207 | |
}
|
|
131 |
// for _, f := range b.prependFuncs {
|
|
132 |
// args := []byte(f(b))
|
|
133 |
// args = append(args, ' ')
|
|
134 |
// pb = append(args, pb...)
|
|
135 |
// }
|
| 208 | 136 |
return pb
|
| 209 | 137 |
}
|
| 210 | 138 |
|
| 211 | 139 |
// String returns the string representation of the bar
|
| 212 | 140 |
func (b *Bar) String() string {
|
| 213 | |
return string(b.Bytes())
|
|
141 |
bufch := make(chan []byte)
|
|
142 |
b.redrawRequestCh <- redrawRequest{bufch}
|
|
143 |
return string(<-bufch)
|
| 214 | 144 |
}
|
| 215 | 145 |
|
| 216 | 146 |
// CompletedPercent return the percent completed
|
| 217 | |
func (b *Bar) CompletedPercent() float64 {
|
| 218 | |
return (float64(b.Current()) / float64(b.Total)) * 100.00
|
| 219 | |
}
|
|
147 |
// func (b *Bar) CompletedPercent() int {
|
|
148 |
// return int(100 * float64(b.current) / float64(b.Total))
|
|
149 |
// }
|
|
150 |
|
|
151 |
// AppendFunc runs the decorator function and renders the output on the right of the progress bar
|
|
152 |
// func (b *Bar) AppendFunc(f DecoratorFunc) *Bar {
|
|
153 |
// b.appendFuncs = append(b.appendFuncs, f)
|
|
154 |
// return b
|
|
155 |
// }
|
|
156 |
|
|
157 |
// PrependFunc runs decorator function and render the output left the progress bar
|
|
158 |
// func (b *Bar) PrependFunc(f DecoratorFunc) *Bar {
|
|
159 |
// b.prependFuncs = append(b.prependFuncs, f)
|
|
160 |
// return b
|
|
161 |
// }
|
|
162 |
|
|
163 |
// AppendCompleted appends the completion percent to the progress bar
|
|
164 |
// func (b *Bar) AppendCompleted() *Bar {
|
|
165 |
// b.AppendFunc(func(b *Bar) string {
|
|
166 |
// return b.CompletedPercentString()
|
|
167 |
// })
|
|
168 |
// return b
|
|
169 |
// }
|
| 220 | 170 |
|
| 221 | 171 |
// CompletedPercentString returns the formatted string representation of the completed percent
|
| 222 | |
func (b *Bar) CompletedPercentString() string {
|
| 223 | |
return fmt.Sprintf("%3.f%%", b.CompletedPercent())
|
| 224 | |
}
|
|
172 |
// func (b *Bar) CompletedPercentString() string {
|
|
173 |
// return fmt.Sprintf("%3d%%", b.CompletedPercent())
|
|
174 |
// }
|
| 225 | 175 |
|
| 226 | |
// TimeElapsed returns the time elapsed
|
| 227 | |
func (b *Bar) TimeElapsed() time.Duration {
|
| 228 | |
b.mtx.RLock()
|
| 229 | |
defer b.mtx.RUnlock()
|
| 230 | |
return b.timeElapsed
|
| 231 | |
}
|
| 232 | |
|
| 233 | |
// TimeElapsedString returns the formatted string represenation of the time elapsed
|
| 234 | |
func (b *Bar) TimeElapsedString() string {
|
| 235 | |
return strutil.PrettyTime(b.TimeElapsed())
|
| 236 | |
}
|
|
176 |
// PrependElapsed prepends the time elapsed to the begining of the bar
|
|
177 |
// func (b *Bar) PrependElapsed() *Bar {
|
|
178 |
// b.PrependFunc(func(b *Bar) string {
|
|
179 |
// return strutil.PadLeft(b.TimeElapsedString(), 5, ' ')
|
|
180 |
// })
|
|
181 |
// return b
|
|
182 |
// }
|