Codebase list golang-github-vbauerster-mpb / 951d04d
don't use bytes.buffer Vladimir Bauer 9 years ago
1 changed file(s) with 7 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
00 package uiprogress
1
2 import "bytes"
31
42 var (
53 // Fill is the default character representing completed progress
113111 func (b *Bar) draw(current int) []byte {
114112 completedWidth := current * b.Width / b.total
115113
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)
120115 for i := 0; i < completedWidth; i++ {
121 buf.WriteByte(b.Fill)
116 buf[i] = b.Fill
122117 }
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
125120 }
126
127121 // set head bit
128 pb := buf.Bytes()
129122 if completedWidth > 0 && completedWidth < b.Width {
130 pb[completedWidth-1] = b.Head
123 buf[completedWidth-1] = b.Head
131124 }
132125
133126 // 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
135128
136129 // render append functions to the right of the bar
137130 // for _, f := range b.appendFuncs {
145138 // args = append(args, ' ')
146139 // pb = append(args, pb...)
147140 // }
148 return pb
141 return buf
149142 }
150143
151144 // CompletedPercent return the percent completed