Codebase list golang-github-vbauerster-mpb / c9bcbbb
Add speed decorator Vladimir Stolyarov authored 8 years ago Vladimir Bauer committed 8 years ago
1 changed file(s) with 81 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
265265 }
266266 return int64(ceil)
267267 }
268
269 // SpeedNoUnit returns raw I/O operation speed decorator.
270 //
271 // `unitFormat` printf compatible verb for value, like "%f" or "%d"
272 //
273 // `width` width reservation to apply, ignored if `DwidthSync` bit is set
274 //
275 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
276 //
277 // unitFormat example:
278 //
279 // "%.1f" = "1.0" or "% .1f" = "1.0"
280 func SpeedNoUnit(unitFormat string, width, conf int) DecoratorFunc {
281 return speed(unitFormat, 0, width, conf)
282 }
283
284 // SpeedKibiByte returns human friendly I/O operation speed decorator,
285 //
286 // `unitFormat` printf compatible verb for value, like "%f" or "%d"
287 //
288 // `width` width reservation to apply, ignored if `DwidthSync` bit is set
289 //
290 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
291 //
292 // unitFormat example:
293 //
294 // "%.1f" = "1.0MiB/s" or "% .1f" = "1.0 MiB/s"
295 func SpeedKibiByte(unitFormat string, width, conf int) DecoratorFunc {
296 return speed(unitFormat, unitKiB, width, conf)
297 }
298
299 // SpeedKiloByte returns human friendly I/O operation speed decorator,
300 //
301 // `unitFormat` printf compatible verb for value, like "%f" or "%d"
302 //
303 // `width` width reservation to apply, ignored if `DwidthSync` bit is set
304 //
305 // `conf` bit set config, [DidentRight|DwidthSync|DextraSpace]
306 //
307 // unitFormat example:
308 //
309 // "%.1f" = "1.0MB/s" or "% .1f" = "1.0 MB/s"
310 func SpeedKiloByte(unitFormat string, width, conf int) DecoratorFunc {
311 return speed(unitFormat, unitKB, width, conf)
312 }
313
314 func speed(unitFormat string, unit, width, conf int) DecoratorFunc {
315 format := "%%"
316 if (conf & DidentRight) != 0 {
317 format += "-"
318 }
319 format += "%ds"
320
321 return func(s *Statistics, widthAccumulator chan<- int, widthDistributor <-chan int) string {
322 var str string
323
324 speed := float64(s.Current) / s.TimeElapsed.Seconds()
325
326 if math.IsNaN(speed) || math.IsInf(speed, 0) {
327 speed = .0
328 }
329
330 switch unit {
331 case unitKiB:
332 str = fmt.Sprintf(unitFormat, SpeedKiB(speed))
333 case unitKB:
334 str = fmt.Sprintf(unitFormat, SpeedKB(speed))
335 default:
336 str = fmt.Sprintf(unitFormat, speed)
337 }
338 if (conf & DwidthSync) != 0 {
339 widthAccumulator <- utf8.RuneCountInString(str)
340 max := <-widthDistributor
341 if (conf & DextraSpace) != 0 {
342 max++
343 }
344 return fmt.Sprintf(fmt.Sprintf(format, max), str)
345 }
346 return fmt.Sprintf(fmt.Sprintf(format, width), str)
347 }
348 }