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