Codebase list golang-github-vbauerster-mpb / d23ea49
bar draw refactoring Vladimir Bauer 5 years ago
3 changed file(s) with 26 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
389389 }
390390
391391 func (s *bState) draw(stat decor.Statistics) io.Reader {
392 if !s.trimSpace {
392 nlr := strings.NewReader("\n")
393 tw := stat.AvailableWidth
394 for _, d := range s.pDecorators {
395 s.bufP.WriteString(d.Decor(stat))
396 }
397 stat.AvailableWidth -= runewidth.StringWidth(stripansi.Strip(s.bufP.String()))
398 if stat.AvailableWidth < 1 {
399 trunc := strings.NewReader(runewidth.Truncate(stripansi.Strip(s.bufP.String()), tw, "…"))
400 s.bufP.Reset()
401 return io.MultiReader(trunc, nlr)
402 }
403
404 if !s.trimSpace && stat.AvailableWidth > 1 {
393405 stat.AvailableWidth -= 2
394406 s.bufB.WriteByte(' ')
395407 defer s.bufB.WriteByte(' ')
396408 }
397409
398 nlr := strings.NewReader("\n")
399 tw := stat.AvailableWidth
400 for _, d := range s.pDecorators {
401 str := d.Decor(stat)
402 stat.AvailableWidth -= runewidth.StringWidth(stripansi.Strip(str))
403 s.bufP.WriteString(str)
404 }
405 if stat.AvailableWidth <= 0 {
406 trunc := strings.NewReader(runewidth.Truncate(stripansi.Strip(s.bufP.String()), tw, "…"))
407 s.bufP.Reset()
408 return io.MultiReader(trunc, s.bufB, nlr)
409 }
410
411410 tw = stat.AvailableWidth
412411 for _, d := range s.aDecorators {
413 str := d.Decor(stat)
414 stat.AvailableWidth -= runewidth.StringWidth(stripansi.Strip(str))
415 s.bufA.WriteString(str)
416 }
417 if stat.AvailableWidth <= 0 {
412 s.bufA.WriteString(d.Decor(stat))
413 }
414 stat.AvailableWidth -= runewidth.StringWidth(stripansi.Strip(s.bufA.String()))
415 if stat.AvailableWidth < 1 {
418416 trunc := strings.NewReader(runewidth.Truncate(stripansi.Strip(s.bufA.String()), tw, "…"))
419417 s.bufA.Reset()
420418 return io.MultiReader(s.bufP, s.bufB, trunc, nlr)
2323 name: "t,c{60,20}",
2424 total: 60,
2525 current: 20,
26 want: "… ",
27 },
28 {
29 name: "t,c{60,20}trim",
30 total: 60,
31 current: 20,
32 trim: true,
3326 want: "",
3427 },
28 {
29 name: "t,c{60,20}trim",
30 total: 60,
31 current: 20,
32 trim: true,
33 want: "",
34 },
3535 },
3636 1: {
3737 {
3838 name: "t,c{60,20}",
3939 total: 60,
4040 current: 20,
41 want: "… ",
41 want: "",
4242 },
4343 {
4444 name: "t,c{60,20}trim",
22 // CheckRequestedWidth checks that requested width doesn't overflow
33 // available width
44 func CheckRequestedWidth(requested, available int) int {
5 if requested <= 0 || requested >= available {
5 if requested < 1 || requested >= available {
66 return available
77 }
88 return requested