don't use bytes.buffer
Vladimir Bauer
9 years ago
| 0 | 0 | package uiprogress |
| 1 | ||
| 2 | import "bytes" | |
| 3 | 1 | |
| 4 | 2 | var ( |
| 5 | 3 | // Fill is the default character representing completed progress |
| 113 | 111 | func (b *Bar) draw(current int) []byte { |
| 114 | 112 | completedWidth := current * b.Width / b.total |
| 115 | 113 | |
| 116 | // add fill and empty bits | |
| 117 | var buf bytes.Buffer | |
| 118 | // buf.WriteString(fmt.Sprintf("completedWidth = %+v ", completedWidth)) | |
| 119 | // buf.WriteString(fmt.Sprintf("current = %+v ", current)) | |
| 114 | buf := make([]byte, b.Width) | |
| 120 | 115 | for i := 0; i < completedWidth; i++ { |
| 121 | buf.WriteByte(b.Fill) | |
| 116 | buf[i] = b.Fill | |
| 122 | 117 | } |
| 123 | for i := 0; i < b.Width-completedWidth; i++ { | |
| 124 | buf.WriteByte(b.Empty) | |
| 118 | for i := completedWidth; i < b.Width; i++ { | |
| 119 | buf[i] = b.Empty | |
| 125 | 120 | } |
| 126 | ||
| 127 | 121 | // set head bit |
| 128 | pb := buf.Bytes() | |
| 129 | 122 | if completedWidth > 0 && completedWidth < b.Width { |
| 130 | pb[completedWidth-1] = b.Head | |
| 123 | buf[completedWidth-1] = b.Head | |
| 131 | 124 | } |
| 132 | 125 | |
| 133 | 126 | // set left and right ends bits |
| 134 | pb[0], pb[len(pb)-1] = b.LeftEnd, b.RightEnd | |
| 127 | buf[0], buf[len(buf)-1] = b.LeftEnd, b.RightEnd | |
| 135 | 128 | |
| 136 | 129 | // render append functions to the right of the bar |
| 137 | 130 | // for _, f := range b.appendFuncs { |
| 145 | 138 | // args = append(args, ' ') |
| 146 | 139 | // pb = append(args, pb...) |
| 147 | 140 | // } |
| 148 | return pb | |
| 141 | return buf | |
| 149 | 142 | } |
| 150 | 143 | |
| 151 | 144 | // CompletedPercent return the percent completed |