incorrent current update fix
Vladimir Bauer
9 years ago
| 26 | 26 | |
| 27 | 27 | // Bar represents a progress bar |
| 28 | 28 | type Bar struct { |
| 29 | // Total of the total for the progress bar | |
| 30 | Total int | |
| 29 | // total of the total for the progress bar | |
| 30 | total int | |
| 31 | 31 | |
| 32 | 32 | // LeftEnd is character in the left most part of the progress indicator. Defaults to '[' |
| 33 | 33 | LeftEnd byte |
| 49 | 49 | |
| 50 | 50 | currentUpdateCh chan int |
| 51 | 51 | |
| 52 | redrawRequestCh chan redrawRequest | |
| 52 | redrawRequestCh chan *redrawRequest | |
| 53 | 53 | |
| 54 | 54 | // appendFuncs []DecoratorFunc |
| 55 | 55 | // prependFuncs []DecoratorFunc |
| 61 | 61 | // NewBar returns a new progress bar |
| 62 | 62 | func NewBar(total int) *Bar { |
| 63 | 63 | b := &Bar{ |
| 64 | Total: total, | |
| 64 | total: total, | |
| 65 | 65 | Width: Width, |
| 66 | 66 | LeftEnd: LeftEnd, |
| 67 | 67 | RightEnd: RightEnd, |
| 69 | 69 | Fill: Fill, |
| 70 | 70 | Empty: Empty, |
| 71 | 71 | currentUpdateCh: make(chan int), |
| 72 | redrawRequestCh: make(chan redrawRequest), | |
| 72 | redrawRequestCh: make(chan *redrawRequest), | |
| 73 | 73 | } |
| 74 | 74 | go b.server() |
| 75 | 75 | return b |
| 77 | 77 | |
| 78 | 78 | type redrawRequest struct { |
| 79 | 79 | bufch chan []byte |
| 80 | } | |
| 81 | ||
| 82 | func (b *Bar) Update(n int) { | |
| 83 | b.currentUpdateCh <- n | |
| 84 | } | |
| 85 | ||
| 86 | // String returns the string representation of the bar | |
| 87 | func (b *Bar) String() string { | |
| 88 | bufch := make(chan []byte) | |
| 89 | b.redrawRequestCh <- &redrawRequest{bufch} | |
| 90 | return string(<-bufch) | |
| 80 | 91 | } |
| 81 | 92 | |
| 82 | 93 | func (b *Bar) server() { |
| 87 | 98 | for { |
| 88 | 99 | select { |
| 89 | 100 | case n := <-b.currentUpdateCh: |
| 90 | current += n | |
| 101 | if n > b.total { | |
| 102 | return | |
| 103 | } | |
| 104 | current = n | |
| 105 | // fmt.Printf("current = %+v\n", current) | |
| 91 | 106 | // blockStartTime = time.Now() |
| 92 | 107 | case r := <-b.redrawRequestCh: |
| 93 | r.bufch <- b.buffer(current) | |
| 108 | r.bufch <- b.draw(current) | |
| 94 | 109 | } |
| 95 | 110 | } |
| 96 | 111 | } |
| 97 | 112 | |
| 98 | func (b *Bar) Update(n int) { | |
| 99 | b.currentUpdateCh <- n | |
| 100 | } | |
| 101 | ||
| 102 | func (b *Bar) buffer(current int) []byte { | |
| 103 | completedPercent := int(100 * float64(current) / float64(b.Total)) | |
| 104 | completedWidth := completedPercent * b.Width / 100 | |
| 113 | func (b *Bar) draw(current int) []byte { | |
| 114 | completedWidth := current * b.Width / b.total | |
| 105 | 115 | |
| 106 | 116 | // add fill and empty bits |
| 107 | 117 | var buf bytes.Buffer |
| 118 | // buf.WriteString(fmt.Sprintf("completedWidth = %+v ", completedWidth)) | |
| 119 | // buf.WriteString(fmt.Sprintf("current = %+v ", current)) | |
| 108 | 120 | for i := 0; i < completedWidth; i++ { |
| 109 | 121 | buf.WriteByte(b.Fill) |
| 110 | 122 | } |
| 134 | 146 | // pb = append(args, pb...) |
| 135 | 147 | // } |
| 136 | 148 | return pb |
| 137 | } | |
| 138 | ||
| 139 | // String returns the string representation of the bar | |
| 140 | func (b *Bar) String() string { | |
| 141 | bufch := make(chan []byte) | |
| 142 | b.redrawRequestCh <- redrawRequest{bufch} | |
| 143 | return string(<-bufch) | |
| 144 | 149 | } |
| 145 | 150 | |
| 146 | 151 | // CompletedPercent return the percent completed |