| 0 | 0 |
package mpb
|
| 1 | 1 |
|
| 2 | 2 |
import (
|
| 3 | |
"bytes"
|
| 4 | 3 |
"io"
|
| 5 | 4 |
|
| 6 | 5 |
"github.com/acarl005/stripansi"
|
|
| 36 | 35 |
count uint
|
| 37 | 36 |
frames []*component
|
| 38 | 37 |
}
|
| 39 | |
flush func(dst io.Writer, src [][]byte, pad *component, padWidth int)
|
|
38 |
flush func(dst io.Writer, filling, padding [][]byte)
|
| 40 | 39 |
}
|
| 41 | 40 |
|
| 42 | 41 |
type component struct {
|
|
| 153 | 152 |
w.Write(s.components[iLbound].bytes)
|
| 154 | 153 |
defer w.Write(s.components[iRbound].bytes)
|
| 155 | 154 |
|
|
155 |
refWidth, filled := 0, 0
|
| 156 | 156 |
curWidth := int(internal.PercentageRound(stat.Total, stat.Current, width))
|
| 157 | |
padWidth := width - curWidth
|
| 158 | |
index, refill := 0, 0
|
| 159 | |
bb := make([][]byte, curWidth)
|
|
157 |
filling := make([][]byte, 0, curWidth)
|
| 160 | 158 |
|
| 161 | 159 |
if curWidth > 0 && curWidth != width {
|
| 162 | 160 |
tipFrame := s.tip.frames[s.tip.count%uint(len(s.tip.frames))]
|
| 163 | |
bb[index] = tipFrame.bytes
|
|
161 |
filling = append(filling, tipFrame.bytes)
|
|
162 |
filled += tipFrame.width
|
| 164 | 163 |
curWidth -= tipFrame.width
|
| 165 | 164 |
s.tip.count++
|
| 166 | |
index++
|
| 167 | 165 |
}
|
| 168 | 166 |
|
| 169 | 167 |
if stat.Refill > 0 {
|
| 170 | |
refill = int(internal.PercentageRound(stat.Total, int64(stat.Refill), width))
|
| 171 | |
if refill > curWidth {
|
| 172 | |
refill = curWidth
|
| 173 | |
}
|
| 174 | |
curWidth -= refill
|
| 175 | |
}
|
| 176 | |
|
| 177 | |
for curWidth > 0 {
|
| 178 | |
bb[index] = s.components[iFiller].bytes
|
|
168 |
refWidth = int(internal.PercentageRound(stat.Total, int64(stat.Refill), width))
|
|
169 |
if refWidth > curWidth {
|
|
170 |
refWidth = curWidth
|
|
171 |
}
|
|
172 |
curWidth -= refWidth
|
|
173 |
}
|
|
174 |
|
|
175 |
for curWidth > 0 && curWidth >= s.components[iFiller].width {
|
|
176 |
filling = append(filling, s.components[iFiller].bytes)
|
|
177 |
filled += s.components[iFiller].width
|
| 179 | 178 |
curWidth -= s.components[iFiller].width
|
| 180 | |
index++
|
| 181 | |
}
|
| 182 | |
|
| 183 | |
for refill > 0 {
|
| 184 | |
bb[index] = s.components[iRefiller].bytes
|
| 185 | |
refill -= s.components[iRefiller].width
|
| 186 | |
index++
|
| 187 | |
}
|
| 188 | |
|
| 189 | |
if curWidth+refill < 0 || s.components[iPadding].width > 1 {
|
| 190 | |
buf := new(bytes.Buffer)
|
| 191 | |
s.flush(buf, bb[:index], s.components[iPadding], padWidth)
|
| 192 | |
io.WriteString(w, runewidth.Truncate(buf.String(), width, "…"))
|
| 193 | |
return
|
| 194 | |
}
|
| 195 | |
|
| 196 | |
s.flush(w, bb, s.components[iPadding], padWidth)
|
| 197 | |
}
|
| 198 | |
|
| 199 | |
func regFlush(dst io.Writer, src [][]byte, pad *component, padWidth int) {
|
| 200 | |
for i := len(src) - 1; i >= 0; i-- {
|
| 201 | |
dst.Write(src[i])
|
| 202 | |
}
|
| 203 | |
for padWidth > 0 {
|
| 204 | |
dst.Write(pad.bytes)
|
| 205 | |
padWidth -= pad.width
|
| 206 | |
}
|
| 207 | |
}
|
| 208 | |
|
| 209 | |
func revFlush(dst io.Writer, src [][]byte, pad *component, padWidth int) {
|
| 210 | |
for padWidth > 0 {
|
| 211 | |
dst.Write(pad.bytes)
|
| 212 | |
padWidth -= pad.width
|
| 213 | |
}
|
| 214 | |
for i := 0; i < len(src); i++ {
|
| 215 | |
dst.Write(src[i])
|
| 216 | |
}
|
| 217 | |
}
|
|
179 |
if s.components[iFiller].width == 0 {
|
|
180 |
break
|
|
181 |
}
|
|
182 |
}
|
|
183 |
|
|
184 |
for refWidth > 0 && refWidth >= s.components[iRefiller].width {
|
|
185 |
filling = append(filling, s.components[iRefiller].bytes)
|
|
186 |
filled += s.components[iRefiller].width
|
|
187 |
refWidth -= s.components[iRefiller].width
|
|
188 |
if s.components[iRefiller].width == 0 {
|
|
189 |
break
|
|
190 |
}
|
|
191 |
}
|
|
192 |
|
|
193 |
padWidth := width - filled
|
|
194 |
padding := make([][]byte, 0, padWidth)
|
|
195 |
for padWidth > 0 && padWidth >= s.components[iPadding].width {
|
|
196 |
padding = append(padding, s.components[iPadding].bytes)
|
|
197 |
filled += s.components[iPadding].width
|
|
198 |
padWidth -= s.components[iPadding].width
|
|
199 |
if s.components[iPadding].width == 0 {
|
|
200 |
break
|
|
201 |
}
|
|
202 |
}
|
|
203 |
|
|
204 |
truncWidth := width - filled
|
|
205 |
for truncWidth > 0 {
|
|
206 |
padding = append(padding, []byte("…"))
|
|
207 |
truncWidth--
|
|
208 |
}
|
|
209 |
|
|
210 |
s.flush(w, filling, padding)
|
|
211 |
}
|
|
212 |
|
|
213 |
func regFlush(dst io.Writer, filling, padding [][]byte) {
|
|
214 |
for i := len(filling) - 1; i >= 0; i-- {
|
|
215 |
dst.Write(filling[i])
|
|
216 |
}
|
|
217 |
for i := 0; i < len(padding); i++ {
|
|
218 |
dst.Write(padding[i])
|
|
219 |
}
|
|
220 |
}
|
|
221 |
|
|
222 |
func revFlush(dst io.Writer, filling, padding [][]byte) {
|
|
223 |
for i := len(padding) - 1; i >= 0; i-- {
|
|
224 |
dst.Write(padding[i])
|
|
225 |
}
|
|
226 |
for i := 0; i < len(filling); i++ {
|
|
227 |
dst.Write(filling[i])
|
|
228 |
}
|
|
229 |
}
|