Codebase list golang-github-vbauerster-mpb / 8dff752
use strings.Repeat and io.WriteString Vladimir Bauer 7 years ago
1 changed file(s) with 12 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
00 package mpb
11
22 import (
3 "bytes"
43 "io"
4 "strings"
55
66 "github.com/vbauerster/mpb/decor"
77 "github.com/vbauerster/mpb/internal"
2424
2525 func (s *barFiller) fill(w io.Writer, width int, stat *decor.Statistics) {
2626
27 w.Write([]byte(string(s.format[rLeft])))
27 str := string(s.format[rLeft])
2828
2929 // don't count rLeft and rRight [brackets]
3030 width -= 2
3131
3232 if width <= 2 {
33 w.Write([]byte(string(s.format[rRight])))
33 io.WriteString(w, str+string(s.format[rRight]))
3434 return
3535 }
3636
4242 }
4343
4444 if s.refill != nil {
45 // append refill rune
46 times := internal.Percentage(stat.Total, s.refill.limit, int64(width))
47 w.Write(s.repeat(s.refill.r, int(times)))
48 rest := progressWidth - times
49 w.Write(s.repeat(s.format[rFill], int(rest)))
45 refillCount := internal.Percentage(stat.Total, s.refill.limit, int64(width))
46 rest := progressWidth - refillCount
47 str += runeRepeat(s.refill.r, int(refillCount)) + runeRepeat(s.format[rFill], int(rest))
5048 } else {
51 w.Write(s.repeat(s.format[rFill], int(progressWidth)))
49 str += runeRepeat(s.format[rFill], int(progressWidth))
5250 }
5351
5452 if needTip {
55 w.Write([]byte(string(s.format[rTip])))
53 str += string(s.format[rTip])
5654 progressWidth++
5755 }
5856
5957 rest := int64(width) - progressWidth
60 w.Write(s.repeat(s.format[rEmpty], int(rest)))
61 w.Write([]byte(string(s.format[rRight])))
58 str += runeRepeat(s.format[rEmpty], int(rest)) + string(s.format[rRight])
59 io.WriteString(w, str)
6260 }
6361
64 func (s *barFiller) repeat(r rune, count int) []byte {
65 return bytes.Repeat([]byte(string(r)), count)
62 func runeRepeat(r rune, count int) string {
63 return strings.Repeat(string(r), count)
6664 }