Codebase list golang-github-vbauerster-mpb / b975b91
DslowMotion Vladimir Bauer 8 years ago
2 changed file(s) with 115 addition(s) and 66 deletion(s). Raw diff Collapse all Expand all
77 )
88
99 const (
10 // DidentRight specifies identation direction.
10 // DidentRight bit specifies identation direction.
1111 // |foo |b | With DidentRight
1212 // | foo| b| Without DidentRight
1313 DidentRight = 1 << iota
1616 // Effective on multiple bars only.
1717 DwidthSync
1818
19 // DextraSpace adds extra space, makes sense with DwidthSync only.
19 // DextraSpace bit adds extra space, makes sense with DwidthSync only.
2020 // When DidentRight bit set, the space will be added to the right,
2121 // otherwise to the left.
2222 DextraSpace
23
24 // DslowMotion bit instructs decorator to skip refreshing its output
25 // every second tick. Normally not necessary, but if flickering is too fast,
26 // you may try it out.
27 DslowMotion
2328
2429 // DSyncSpace is shortcut for DwidthSync|DextraSpace
2530 DSyncSpace = DwidthSync | DextraSpace
9398 format += "-"
9499 }
95100 format += "%ds"
96 return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
97 name := messageFn(s)
98 if (conf & DwidthSync) != 0 {
99 widthAccumulator <- utf8.RuneCountInString(name)
100 max := <-widthDistributor
101 if (conf & DextraSpace) != 0 {
102 max++
103 }
104 return fmt.Sprintf(fmt.Sprintf(format, max), name)
105 }
106 return fmt.Sprintf(fmt.Sprintf(format, width), name)
101 count, times := 0, 1
102 if (conf & DslowMotion) != 0 {
103 times++
104 }
105 var out string
106 return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
107 if count == 0 || s.Completed {
108 count = times
109 out = messageFn(s)
110 }
111 count--
112 if (conf & DwidthSync) != 0 {
113 widthAccumulator <- utf8.RuneCountInString(out)
114 max := <-widthDistributor
115 if (conf & DextraSpace) != 0 {
116 max++
117 }
118 return fmt.Sprintf(fmt.Sprintf(format, max), out)
119 }
120 return fmt.Sprintf(fmt.Sprintf(format, width), out)
107121 }
108122 }
109123
154168 format += "-"
155169 }
156170 format += "%ds"
157 return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
158 var str string
159 switch unit {
160 case unitKiB:
161 str = fmt.Sprintf(pairFormat, CounterKiB(s.Current), CounterKiB(s.Total))
162 case unitKB:
163 str = fmt.Sprintf(pairFormat, CounterKB(s.Current), CounterKB(s.Total))
164 default:
165 str = fmt.Sprintf(pairFormat, s.Current, s.Total)
166 }
167 if (conf & DwidthSync) != 0 {
168 widthAccumulator <- utf8.RuneCountInString(str)
169 max := <-widthDistributor
170 if (conf & DextraSpace) != 0 {
171 max++
172 }
173 return fmt.Sprintf(fmt.Sprintf(format, max), str)
174 }
175 return fmt.Sprintf(fmt.Sprintf(format, width), str)
171 count, times := 0, 1
172 if (conf & DslowMotion) != 0 {
173 times++
174 }
175 var out string
176 return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
177 if count == 0 || s.Completed {
178 count = times
179 switch unit {
180 case unitKiB:
181 out = fmt.Sprintf(pairFormat, CounterKiB(s.Current), CounterKiB(s.Total))
182 case unitKB:
183 out = fmt.Sprintf(pairFormat, CounterKB(s.Current), CounterKB(s.Total))
184 default:
185 out = fmt.Sprintf(pairFormat, s.Current, s.Total)
186 }
187 }
188 count--
189 if (conf & DwidthSync) != 0 {
190 widthAccumulator <- utf8.RuneCountInString(out)
191 max := <-widthDistributor
192 if (conf & DextraSpace) != 0 {
193 max++
194 }
195 return fmt.Sprintf(fmt.Sprintf(format, max), out)
196 }
197 return fmt.Sprintf(fmt.Sprintf(format, width), out)
176198 }
177199 }
178200
187209 format += "-"
188210 }
189211 format += "%ds"
190 return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
191 str := fmt.Sprint(time.Duration(s.Eta().Seconds()) * time.Second)
192 if (conf & DwidthSync) != 0 {
193 widthAccumulator <- utf8.RuneCountInString(str)
194 max := <-widthDistributor
195 if (conf & DextraSpace) != 0 {
196 max++
197 }
198 return fmt.Sprintf(fmt.Sprintf(format, max), str)
199 }
200 return fmt.Sprintf(fmt.Sprintf(format, width), str)
212 count, times := 0, 1
213 if (conf & DslowMotion) != 0 {
214 times++
215 }
216 var out string
217 return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
218 if count == 0 || s.Completed {
219 count = times
220 out = fmt.Sprint(time.Duration(s.Eta().Seconds()) * time.Second)
221 }
222 count--
223 if (conf & DwidthSync) != 0 {
224 widthAccumulator <- utf8.RuneCountInString(out)
225 max := <-widthDistributor
226 if (conf & DextraSpace) != 0 {
227 max++
228 }
229 return fmt.Sprintf(fmt.Sprintf(format, max), out)
230 }
231 return fmt.Sprintf(fmt.Sprintf(format, width), out)
201232 }
202233 }
203234
212243 format += "-"
213244 }
214245 format += "%ds"
215 return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
216 str := fmt.Sprint(time.Duration(s.TimeElapsed.Seconds()) * time.Second)
217 if (conf & DwidthSync) != 0 {
218 widthAccumulator <- utf8.RuneCountInString(str)
219 max := <-widthDistributor
220 if (conf & DextraSpace) != 0 {
221 max++
222 }
223 return fmt.Sprintf(fmt.Sprintf(format, max), str)
224 }
225 return fmt.Sprintf(fmt.Sprintf(format, width), str)
246 count, times := 0, 1
247 if (conf & DslowMotion) != 0 {
248 times++
249 }
250 var out string
251 return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
252 if count == 0 || s.Completed {
253 count = times
254 out = fmt.Sprint(time.Duration(s.TimeElapsed.Seconds()) * time.Second)
255 }
256 count--
257 if (conf & DwidthSync) != 0 {
258 widthAccumulator <- utf8.RuneCountInString(out)
259 max := <-widthDistributor
260 if (conf & DextraSpace) != 0 {
261 max++
262 }
263 return fmt.Sprintf(fmt.Sprintf(format, max), out)
264 }
265 return fmt.Sprintf(fmt.Sprintf(format, width), out)
226266 }
227267 }
228268
237277 format += "-"
238278 }
239279 format += "%ds"
240 return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
241 str := fmt.Sprintf("%d %%", CalcPercentage(s.Total, s.Current, 100))
242 if (conf & DwidthSync) != 0 {
243 widthAccumulator <- utf8.RuneCountInString(str)
244 max := <-widthDistributor
245 if (conf & DextraSpace) != 0 {
246 max++
247 }
248 return fmt.Sprintf(fmt.Sprintf(format, max), str)
249 }
250 return fmt.Sprintf(fmt.Sprintf(format, width), str)
280 count, times := 0, 1
281 if (conf & DslowMotion) != 0 {
282 times++
283 }
284 var out string
285 return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
286 if count == 0 || s.Completed {
287 count = times
288 out = fmt.Sprintf("%d %%", CalcPercentage(s.Total, s.Current, 100))
289 }
290 count--
291 if (conf & DwidthSync) != 0 {
292 widthAccumulator <- utf8.RuneCountInString(out)
293 max := <-widthDistributor
294 if (conf & DextraSpace) != 0 {
295 max++
296 }
297 return fmt.Sprintf(fmt.Sprintf(format, max), out)
298 }
299 return fmt.Sprintf(fmt.Sprintf(format, width), out)
251300 }
252301 }
253302
5252 mpb.PrependDecorators(
5353 decor.StaticName(task, len(task)+1, decor.DidentRight),
5454 decor.OnComplete(decor.StaticName(job, 0, decor.DSyncSpaceR), "done!", 0, decor.DSyncSpaceR),
55 decor.OnComplete(decor.ETA(0, decor.DwidthSync), "", 0, decor.DwidthSync),
55 decor.OnComplete(decor.ETA(0, decor.DwidthSync|decor.DslowMotion), "", 0, decor.DwidthSync),
5656 ),
5757 mpb.AppendDecorators(
5858 decor.OnComplete(decor.Percentage(5, 0), "", 0, 0),