Codebase list golang-github-vbauerster-mpb / ca52dab
bar filler: better width check for complex runes Vladimir Bauer 5 years ago
2 changed file(s) with 117 addition(s) and 61 deletion(s). Raw diff Collapse all Expand all
00 package mpb
11
22 import (
3 "bytes"
43 "io"
54
65 "github.com/acarl005/stripansi"
3635 count uint
3736 frames []*component
3837 }
39 flush func(dst io.Writer, src [][]byte, pad *component, padWidth int)
38 flush func(dst io.Writer, filling, padding [][]byte)
4039 }
4140
4241 type component struct {
153152 w.Write(s.components[iLbound].bytes)
154153 defer w.Write(s.components[iRbound].bytes)
155154
155 refWidth, filled := 0, 0
156156 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)
160158
161159 if curWidth > 0 && curWidth != width {
162160 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
164163 curWidth -= tipFrame.width
165164 s.tip.count++
166 index++
167165 }
168166
169167 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
179178 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 }
182182 want: "[==================>---------------------------------------]",
183183 },
184184 },
185 99: {
186 {
187 style: BarStyle().Lbound("[").Filler("の").Tip("だ").Padding("つ").Rbound("]"),
188 name: "[のだつ] t,c{99,1}",
189 total: 99,
190 current: 1,
191 want: " [だつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつ…] ",
192 },
193 {
194 style: BarStyle().Lbound("[").Filler("の").Tip("だ").Padding("つ").Rbound("]"),
195 name: "[のだつ] t,c{99,2}",
196 total: 99,
197 current: 2,
198 want: " [だつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつ…] ",
199 },
200 {
201 style: BarStyle().Lbound("[").Filler("の").Tip("だ").Padding("つ").Rbound("]"),
202 name: "[のだつ] t,c{99,3}",
203 total: 99,
204 current: 3,
205 want: " [だつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつ…] ",
206 },
207 {
208 style: BarStyle().Lbound("[").Filler("の").Tip("だ").Padding("つ").Rbound("]"),
209 name: "[のだつ] t,c{99,4}",
210 total: 99,
211 current: 4,
212 want: " [のだつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつ…] ",
213 },
214 {
215 style: BarStyle().Lbound("[").Filler("の").Tip("だ").Padding("つ").Rbound("]"),
216 name: "[のだつ] t,c{99,5}",
217 total: 99,
218 current: 5,
219 want: " [のだつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつ…] ",
220 },
221 {
222 style: BarStyle().Lbound("[").Filler("の").Tip("だ").Padding("つ").Rbound("]"),
223 name: "[のだつ] t,c{99,6}",
224 total: 99,
225 current: 6,
226 want: " [ののだつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつ…] ",
227 },
228 {
229 style: BarStyle().Lbound("[").Filler("の").Tip("だ").Padding("つ").Rbound("]").Reverse(),
230 name: "[のだつ] t,c{99,6}rev",
231 total: 99,
232 current: 6,
233 want: " […つつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつだのの] ",
234 },
235 },
185236 100: {
186237 {
187238 name: "t,c{100,0}",
319370 refill: 33,
320371 trim: true,
321372 want: "[-----------------------------------------------------------<======++++++++++++++++++++++++++++++++]",
322 },
323 {
324 style: BarStyle().Lbound("[").Filler("=").Tip("の").Padding("-").Rbound("]"),
325 name: "[=の-] t,c{100,1}",
326 total: 100,
327 current: 1,
328 want: " [の---------------------------------------------------------------------------------------------…] ",
329373 },
330374 },
331375 197: {