| 45 | 45 |
// DecoratorFunc is a function that can be prepended and appended to the progress bar
|
| 46 | 46 |
type DecoratorFunc func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string
|
| 47 | 47 |
|
| 48 | |
// StaticName to be used, when there is no plan to change the name during whole
|
| 49 | |
// life of a progress rendering process
|
| 50 | |
func StaticName(name string, minWidth int, conf byte) DecoratorFunc {
|
| 51 | |
nameFn := func(s *Statistics) string {
|
|
48 |
// OnComplete wraps provided decorator `fn` with on complete event `message`.
|
|
49 |
// If you set `DwidthSync` bit in `conf` param, `minWidth` is ignored.
|
|
50 |
// `DwidthSync` is effective with multiple bars only, if set decorator will participate
|
|
51 |
// in width synchronization process with other decorators in the same column group.
|
|
52 |
func OnComplete(fn DecoratorFunc, message string, minWidth, conf int) DecoratorFunc {
|
|
53 |
msgDecorator := StaticName(message, minWidth, conf)
|
|
54 |
return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
|
|
55 |
if s.Completed {
|
|
56 |
return msgDecorator(s, widthAccumulator, widthDistributor)
|
|
57 |
}
|
|
58 |
return fn(s, widthAccumulator, widthDistributor)
|
|
59 |
}
|
|
60 |
}
|
|
61 |
|
|
62 |
// StaticName is a simple name/message decorator.
|
|
63 |
// If you set `DwidthSync` bit in `conf` param, `minWidth` is ignored.
|
|
64 |
// `DwidthSync` is effective with multiple bars only, if set decorator will participate
|
|
65 |
// in width synchronization process with other decorators in the same column group.
|
|
66 |
func StaticName(name string, minWidth, conf int) DecoratorFunc {
|
|
67 |
nameFn := func(*Statistics) string {
|
| 52 | 68 |
return name
|
| 53 | 69 |
}
|
| 54 | 70 |
return DynamicName(nameFn, minWidth, conf)
|
| 55 | 71 |
}
|
| 56 | 72 |
|
| 57 | |
// DynamicName to be used, when there is a plan to change the name once or
|
| 58 | |
// several times during progress rendering process. If there're more than one
|
| 59 | |
// bar, and you'd like to synchronize column width, conf param should have
|
| 60 | |
// DwidthSync bit set.
|
| 61 | |
func DynamicName(nameFn func(*Statistics) string, minWidth int, conf byte) DecoratorFunc {
|
| 62 | |
format := "%%"
|
| 63 | |
if (conf & DidentRight) != 0 {
|
| 64 | |
format += "-"
|
| 65 | |
}
|
| 66 | |
format += "%ds"
|
| 67 | |
return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
|
| 68 | |
name := nameFn(s)
|
|
73 |
// DynamicName is a name/message decorator, with ability to change message via provided `messageFn`.
|
|
74 |
// If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
|
|
75 |
// `DwidthSync` is effective with multiple bars only, if set decorator will participate
|
|
76 |
// in width synchronization process with other decorators in the same column group.
|
|
77 |
func DynamicName(messageFn func(*Statistics) string, minWidth, conf int) DecoratorFunc {
|
|
78 |
format := "%%"
|
|
79 |
if (conf & DidentRight) != 0 {
|
|
80 |
format += "-"
|
|
81 |
}
|
|
82 |
format += "%ds"
|
|
83 |
return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
|
|
84 |
name := messageFn(s)
|
| 69 | 85 |
if (conf & DwidthSync) != 0 {
|
| 70 | 86 |
widthAccumulator <- utf8.RuneCountInString(name)
|
| 71 | 87 |
max := <-widthDistributor
|
|
| 79 | 95 |
}
|
| 80 | 96 |
|
| 81 | 97 |
// CountersNoUnit returns raw counters decorator
|
| 82 | |
func CountersNoUnit(pairFormat string, minWidth int, conf byte) DecoratorFunc {
|
|
98 |
// If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
|
|
99 |
// `DwidthSync` is effective with multiple bars only, if set decorator will participate
|
|
100 |
// in width synchronization process with other decorators in the same column group.
|
|
101 |
func CountersNoUnit(pairFormat string, minWidth, conf int) DecoratorFunc {
|
| 83 | 102 |
return Counters(pairFormat, 0, minWidth, conf)
|
| 84 | 103 |
}
|
| 85 | 104 |
|
| 86 | |
// CountersKibiByte returns human friendly byte counters decorator, where
|
| 87 | |
// counters unit is multiple by 1024.
|
| 88 | |
func CountersKibiByte(pairFormat string, minWidth int, conf byte) DecoratorFunc {
|
|
105 |
// CountersKibiByte returns human friendly byte counters decorator,
|
|
106 |
// where counters unit is multiple by 1024.
|
|
107 |
// `pairFormat` must contain two printf compatible verbs, like "%f" or "%d".
|
|
108 |
// First verb substituted with Current, second one with Total.
|
|
109 |
// Example: `"%.1f / %.1f" = "1.0MiB / 12.0MiB"` or `"% .1f / % .1f" = "1.0 MiB / 12.0 MiB"`.
|
|
110 |
// If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
|
|
111 |
// `DwidthSync` is effective with multiple bars only, if set decorator will participate
|
|
112 |
// in width synchronization process with other decorators in the same column group.
|
|
113 |
func CountersKibiByte(pairFormat string, minWidth, conf int) DecoratorFunc {
|
| 89 | 114 |
return Counters(pairFormat, Unit_KiB, minWidth, conf)
|
| 90 | 115 |
}
|
| 91 | 116 |
|
| 92 | 117 |
// CountersKiloByte returns human friendly byte counters decorator, where
|
| 93 | 118 |
// counters unit is multiple by 1000.
|
| 94 | |
func CountersKiloByte(pairFormat string, minWidth int, conf byte) DecoratorFunc {
|
|
119 |
// `pairFormat` must contain two printf compatible verbs, like "%f" or "%d".
|
|
120 |
// First verb substituted with Current, second one with Total.
|
|
121 |
// Example: `"%.1f / %.1f" = "1.0MiB / 12.0MiB"` or `"% .1f / % .1f" = "1.0 MiB / 12.0 MiB"`.
|
|
122 |
// If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
|
|
123 |
// `DwidthSync` is effective with multiple bars only, if set decorator will participate
|
|
124 |
// in width synchronization process with other decorators in the same column group.
|
|
125 |
func CountersKiloByte(pairFormat string, minWidth, conf int) DecoratorFunc {
|
| 95 | 126 |
return Counters(pairFormat, Unit_kB, minWidth, conf)
|
| 96 | 127 |
}
|
| 97 | 128 |
|
| 98 | 129 |
// Counters provides basic counters decorator.
|
| 99 | |
// pairFormat must contain two printf compatible verbs, like "%f" or "%d".
|
| 100 | |
// First verb substituted with Current, second one with Total. For example (assuming decor.Unit_KiB used):
|
| 101 | |
// "%.1f / %.1f" = "1.0MiB / 12.0MiB" or "% .1f / % .1f" = "1.0 MiB / 12.0 MiB"
|
| 102 | |
// unit is one of decor.Unit_KiB/decor.Unit_kB or just zero if you need raw unitless numbers.
|
| 103 | |
func Counters(pairFormat string, unit Unit, minWidth int, conf byte) DecoratorFunc {
|
|
130 |
// `pairFormat` must contain two printf compatible verbs, like "%f" or "%d".
|
|
131 |
// First verb substituted with Current, second one with Total.
|
|
132 |
// Example: `"%.1f / %.1f" = "1.0MiB / 12.0MiB"` or `"% .1f / % .1f" = "1.0 MiB / 12.0 MiB"`.
|
|
133 |
// `unit` is one of decor.Unit_KiB/decor.Unit_kB or just zero if you need raw unitless numbers.
|
|
134 |
// If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
|
|
135 |
// `DwidthSync` is effective with multiple bars only, if set decorator will participate
|
|
136 |
// in width synchronization process with other decorators in the same column group.
|
|
137 |
func Counters(pairFormat string, unit Unit, minWidth, conf int) DecoratorFunc {
|
| 104 | 138 |
format := "%%"
|
| 105 | 139 |
if (conf & DidentRight) != 0 {
|
| 106 | 140 |
format += "-"
|
|
| 129 | 163 |
}
|
| 130 | 164 |
|
| 131 | 165 |
// ETA provides exponential-weighted-moving-average ETA decorator.
|
| 132 | |
// If there're more than one bar, and you'd like to synchronize column width,
|
| 133 | |
// conf param should have DwidthSync bit set.
|
| 134 | |
func ETA(minWidth int, conf byte) DecoratorFunc {
|
|
166 |
// If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
|
|
167 |
// `DwidthSync` is effective with multiple bars only, if set decorator will participate
|
|
168 |
// in width synchronization process with other decorators in the same column group.
|
|
169 |
func ETA(minWidth, conf int) DecoratorFunc {
|
| 135 | 170 |
format := "%%"
|
| 136 | 171 |
if (conf & DidentRight) != 0 {
|
| 137 | 172 |
format += "-"
|
|
| 152 | 187 |
}
|
| 153 | 188 |
|
| 154 | 189 |
// Elapsed provides elapsed time decorator.
|
| 155 | |
// If there're more than one bar, and you'd like to synchronize column width,
|
| 156 | |
// conf param should have DwidthSync bit set.
|
| 157 | |
func Elapsed(minWidth int, conf byte) DecoratorFunc {
|
|
190 |
// If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
|
|
191 |
// `DwidthSync` is effective with multiple bars only, if set decorator will participate
|
|
192 |
// in width synchronization process with other decorators in the same column group.
|
|
193 |
func Elapsed(minWidth, conf int) DecoratorFunc {
|
| 158 | 194 |
format := "%%"
|
| 159 | 195 |
if (conf & DidentRight) != 0 {
|
| 160 | 196 |
format += "-"
|
|
| 175 | 211 |
}
|
| 176 | 212 |
|
| 177 | 213 |
// Percentage provides percentage decorator.
|
| 178 | |
// If there're more than one bar, and you'd like to synchronize column width,
|
| 179 | |
// conf param should have DwidthSync bit set.
|
| 180 | |
func Percentage(minWidth int, conf byte) DecoratorFunc {
|
|
214 |
// If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
|
|
215 |
// `DwidthSync` is effective with multiple bars only, if set decorator will participate
|
|
216 |
// in width synchronization process with other decorators in the same column group.
|
|
217 |
func Percentage(minWidth, conf int) DecoratorFunc {
|
| 181 | 218 |
format := "%%"
|
| 182 | 219 |
if (conf & DidentRight) != 0 {
|
| 183 | 220 |
format += "-"
|