Codebase list golang-github-vbauerster-mpb / 65f98fb
fillBar bug fixes Vladimir Bauer 9 years ago
1 changed file(s) with 19 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
11
22 import (
33 "io"
4 "math"
45 "sync"
56 "time"
67 "unicode/utf8"
432433 }
433434
434435 func fillBar(total, current int64, width int, fmtBytes barFmtBytes, rf *refill) []byte {
435 if width < 2 {
436 if width < 2 || total <= 0 {
436437 return []byte{}
437438 }
438439
439440 // bar width without leftEnd and rightEnd runes
440 barWidth := width - 1
441 barWidth := width - 2
441442
442443 completedWidth := percentage(total, current, barWidth)
443444
452453 for i := 0; i < till; i++ {
453454 buf = append(buf, rbytes...)
454455 }
455 for i := till; i < completedWidth-1; i++ {
456 for i := till; i < completedWidth; i++ {
456457 buf = append(buf, fmtBytes[rFill]...)
457458 }
458459 } else {
459 for i := 0; i < completedWidth-1; i++ {
460 for i := 0; i < completedWidth; i++ {
460461 buf = append(buf, fmtBytes[rFill]...)
461462 }
462463 }
463464
464 if completedWidth > 0 && completedWidth < barWidth {
465 if completedWidth < barWidth && completedWidth > 0 {
466 _, size := utf8.DecodeLastRune(buf)
467 buf = buf[:len(buf)-size]
465468 buf = append(buf, fmtBytes[rTip]...)
466469 }
467470
468 for i := completedWidth + 1; i < barWidth; i++ {
471 for i := completedWidth; i < barWidth; i++ {
469472 buf = append(buf, fmtBytes[rEmpty]...)
470473 }
471474
500503 }
501504
502505 func percentage(total, current int64, ratio int) int {
503 if total <= 0 {
506 if current > total {
504507 return 0
505508 }
506 return int(float64(ratio) * float64(current) / float64(total))
509 num := float64(ratio) * float64(current) / float64(total)
510 ceil := math.Ceil(num)
511 diff := ceil - num
512 // num = 2.34 will return 2
513 // num = 2.44 will return 3
514 if math.Max(diff, 0.6) == diff {
515 return int(num)
516 }
517 return int(ceil)
507518 }
508519
509520 func getSpinner() func() byte {