Codebase list golang-github-vbauerster-mpb / f597af1
Refactoring decorators godoc Vladimir Bauer 8 years ago
1 changed file(s) with 47 addition(s) and 33 deletion(s). Raw diff Collapse all Expand all
4545 // DecoratorFunc is a function that can be prepended and appended to the progress bar
4646 type DecoratorFunc func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string
4747
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.
48 // OnComplete returns decorator, which wraps provided `fn` decorator, with sole
49 // purpose to display final on cemplete message.
50 //
51 // `fn` DecoratorFunc to wrap
52 //
53 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set
54 //
55 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
5256 func OnComplete(fn DecoratorFunc, message string, minWidth, conf int) DecoratorFunc {
5357 msgDecorator := StaticName(message, minWidth, conf)
5458 return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
5963 }
6064 }
6165
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 // StaticName returns static name/message decorator.
67 //
68 // `name` string to display
69 //
70 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set
71 //
72 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
6673 func StaticName(name string, minWidth, conf int) DecoratorFunc {
6774 nameFn := func(*Statistics) string {
6875 return name
7077 return DynamicName(nameFn, minWidth, conf)
7178 }
7279
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.
80 // DynamicName returns dynamic name/message decorator.
81 //
82 // `messageFn` callback function to get dynamic string message
83 //
84 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set
85 //
86 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
7787 func DynamicName(messageFn func(*Statistics) string, minWidth, conf int) DecoratorFunc {
7888 format := "%%"
7989 if (conf & DidentRight) != 0 {
96106
97107 // CountersNoUnit returns raw counters decorator
98108 //
99 // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d".
100 //
101 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set.
109 // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d"
110 //
111 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set
102112 //
103113 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
104114 func CountersNoUnit(pairFormat string, minWidth, conf int) DecoratorFunc {
107117
108118 // CountersKibiByte returns human friendly byte counters decorator, where counters unit is multiple by 1024.
109119 //
110 // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d".
111 //
112 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set.
120 // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d"
121 //
122 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set
113123 //
114124 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
115125 //
122132
123133 // CountersKiloByte returns human friendly byte counters decorator, where counters unit is multiple by 1000.
124134 //
125 // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d".
126 //
127 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set.
135 // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d"
136 //
137 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set
128138 //
129139 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
130140 //
163173 }
164174 }
165175
166 // ETA provides exponential-weighted-moving-average ETA decorator.
167 // If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
168 // `DwidthSync` is effective with multiple bars only, if set decorator will participate
169 // in width synchronization process with other decorators in the same column group.
176 // ETA returns exponential-weighted-moving-average ETA decorator.
177 //
178 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set
179 //
180 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
170181 func ETA(minWidth, conf int) DecoratorFunc {
171182 format := "%%"
172183 if (conf & DidentRight) != 0 {
187198 }
188199 }
189200
190 // Elapsed provides elapsed time decorator.
191 // If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
192 // `DwidthSync` is effective with multiple bars only, if set decorator will participate
193 // in width synchronization process with other decorators in the same column group.
201 // Elapsed returns elapsed time decorator.
202 //
203 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set
204 //
205 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
194206 func Elapsed(minWidth, conf int) DecoratorFunc {
195207 format := "%%"
196208 if (conf & DidentRight) != 0 {
211223 }
212224 }
213225
214 // Percentage provides percentage decorator.
215 // If you set `DwidthSync` bit in `conf` param, `minWidth` param is ignored.
216 // `DwidthSync` is effective with multiple bars only, if set decorator will participate
217 // in width synchronization process with other decorators in the same column group.
226 // Percentage returns percentage decorator.
227 //
228 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set
229 //
230 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
218231 func Percentage(minWidth, conf int) DecoratorFunc {
219232 format := "%%"
220233 if (conf & DidentRight) != 0 {
235248 }
236249 }
237250
251 // CalcPercentage is a helper function, to calculate percentage.
238252 func CalcPercentage(total, current, width int64) (perc int64) {
239253 if total <= 0 {
240254 return 0