Codebase list golang-github-vbauerster-mpb / 893d2a8
Refactoring: decorators minWidth to width Vladimir Bauer 8 years ago
1 changed file(s) with 45 addition(s) and 45 deletion(s). Raw diff Collapse all Expand all
5050 //
5151 // `fn` DecoratorFunc to wrap
5252 //
53 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set
54 //
55 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
56 func OnComplete(fn DecoratorFunc, message string, minWidth, conf int) DecoratorFunc {
57 msgDecorator := StaticName(message, minWidth, conf)
53 // `width` width to apply, if `DwidthSync` bit is not set
54 //
55 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
56 func OnComplete(fn DecoratorFunc, message string, width, conf int) DecoratorFunc {
57 msgDecorator := StaticName(message, width, conf)
5858 return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
5959 if s.Completed {
6060 return msgDecorator(s, widthAccumulator, widthDistributor)
6767 //
6868 // `name` string to display
6969 //
70 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set
71 //
72 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
73 func StaticName(name string, minWidth, conf int) DecoratorFunc {
70 // `width` width to apply, if `DwidthSync` bit is not set
71 //
72 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
73 func StaticName(name string, width, conf int) DecoratorFunc {
7474 nameFn := func(*Statistics) string {
7575 return name
7676 }
77 return DynamicName(nameFn, minWidth, conf)
77 return DynamicName(nameFn, width, conf)
7878 }
7979
8080 // DynamicName returns dynamic name/message decorator.
8181 //
8282 // `messageFn` callback function to get dynamic string message
8383 //
84 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set
85 //
86 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
87 func DynamicName(messageFn func(*Statistics) string, minWidth, conf int) DecoratorFunc {
84 // `width` width to apply, if `DwidthSync` bit is not set
85 //
86 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
87 func DynamicName(messageFn func(*Statistics) string, width, conf int) DecoratorFunc {
8888 format := "%%"
8989 if (conf & DidentRight) != 0 {
9090 format += "-"
100100 }
101101 return fmt.Sprintf(fmt.Sprintf(format, max), name)
102102 }
103 return fmt.Sprintf(fmt.Sprintf(format, minWidth), name)
103 return fmt.Sprintf(fmt.Sprintf(format, width), name)
104104 }
105105 }
106106
108108 //
109109 // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d"
110110 //
111 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set
112 //
113 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
114 func CountersNoUnit(pairFormat string, minWidth, conf int) DecoratorFunc {
115 return counters(pairFormat, 0, minWidth, conf)
111 // `width` width to apply, if `DwidthSync` bit is not set
112 //
113 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
114 func CountersNoUnit(pairFormat string, width, conf int) DecoratorFunc {
115 return counters(pairFormat, 0, width, conf)
116116 }
117117
118118 // CountersKibiByte returns human friendly byte counters decorator, where counters unit is multiple by 1024.
119119 //
120120 // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d"
121121 //
122 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set
122 // `width` width to apply, if `DwidthSync` bit is not set
123123 //
124124 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
125125 //
126126 // pairFormat example:
127127 //
128128 // "%.1f / %.1f" = "1.0MiB / 12.0MiB" or "% .1f / % .1f" = "1.0 MiB / 12.0 MiB"
129 func CountersKibiByte(pairFormat string, minWidth, conf int) DecoratorFunc {
130 return counters(pairFormat, unitKiB, minWidth, conf)
129 func CountersKibiByte(pairFormat string, width, conf int) DecoratorFunc {
130 return counters(pairFormat, unitKiB, width, conf)
131131 }
132132
133133 // CountersKiloByte returns human friendly byte counters decorator, where counters unit is multiple by 1000.
134134 //
135135 // `pairFormat` printf compatible verbs for current and total, like "%f" or "%d"
136136 //
137 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set
137 // `width` width to apply, if `DwidthSync` bit is not set
138138 //
139139 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
140140 //
141141 // pairFormat example:
142142 //
143143 // "%.1f / %.1f" = "1.0MB / 12.0MB" or "% .1f / % .1f" = "1.0 MB / 12.0 MB"
144 func CountersKiloByte(pairFormat string, minWidth, conf int) DecoratorFunc {
145 return counters(pairFormat, unitKB, minWidth, conf)
146 }
147
148 func counters(pairFormat string, unit counterUnit, minWidth, conf int) DecoratorFunc {
144 func CountersKiloByte(pairFormat string, width, conf int) DecoratorFunc {
145 return counters(pairFormat, unitKB, width, conf)
146 }
147
148 func counters(pairFormat string, unit counterUnit, width, conf int) DecoratorFunc {
149149 format := "%%"
150150 if (conf & DidentRight) != 0 {
151151 format += "-"
169169 }
170170 return fmt.Sprintf(fmt.Sprintf(format, max), str)
171171 }
172 return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
172 return fmt.Sprintf(fmt.Sprintf(format, width), str)
173173 }
174174 }
175175
176176 // ETA returns exponential-weighted-moving-average ETA decorator.
177177 //
178 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set
179 //
180 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
181 func ETA(minWidth, conf int) DecoratorFunc {
178 // `width` width to apply, if `DwidthSync` bit is not set
179 //
180 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
181 func ETA(width, conf int) DecoratorFunc {
182182 format := "%%"
183183 if (conf & DidentRight) != 0 {
184184 format += "-"
194194 }
195195 return fmt.Sprintf(fmt.Sprintf(format, max), str)
196196 }
197 return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
197 return fmt.Sprintf(fmt.Sprintf(format, width), str)
198198 }
199199 }
200200
201201 // Elapsed returns elapsed time decorator.
202202 //
203 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set
204 //
205 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
206 func Elapsed(minWidth, conf int) DecoratorFunc {
203 // `width` width to apply, if `DwidthSync` bit is not set
204 //
205 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
206 func Elapsed(width, conf int) DecoratorFunc {
207207 format := "%%"
208208 if (conf & DidentRight) != 0 {
209209 format += "-"
219219 }
220220 return fmt.Sprintf(fmt.Sprintf(format, max), str)
221221 }
222 return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
222 return fmt.Sprintf(fmt.Sprintf(format, width), str)
223223 }
224224 }
225225
226226 // Percentage returns percentage decorator.
227227 //
228 // `minWidth` minimum width to apply, if `DwidthSync` bit is not set
229 //
230 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
231 func Percentage(minWidth, conf int) DecoratorFunc {
228 // `width` width to apply, if `DwidthSync` bit is not set
229 //
230 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
231 func Percentage(width, conf int) DecoratorFunc {
232232 format := "%%"
233233 if (conf & DidentRight) != 0 {
234234 format += "-"
244244 }
245245 return fmt.Sprintf(fmt.Sprintf(format, max), str)
246246 }
247 return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
247 return fmt.Sprintf(fmt.Sprintf(format, width), str)
248248 }
249249 }
250250