use strings.Repeat and io.WriteString
Vladimir Bauer
7 years ago
| 0 | 0 | package mpb |
| 1 | 1 | |
| 2 | 2 | import ( |
| 3 | "bytes" | |
| 4 | 3 | "io" |
| 4 | "strings" | |
| 5 | 5 | |
| 6 | 6 | "github.com/vbauerster/mpb/decor" |
| 7 | 7 | "github.com/vbauerster/mpb/internal" |
| 24 | 24 | |
| 25 | 25 | func (s *barFiller) fill(w io.Writer, width int, stat *decor.Statistics) { |
| 26 | 26 | |
| 27 | w.Write([]byte(string(s.format[rLeft]))) | |
| 27 | str := string(s.format[rLeft]) | |
| 28 | 28 | |
| 29 | 29 | // don't count rLeft and rRight [brackets] |
| 30 | 30 | width -= 2 |
| 31 | 31 | |
| 32 | 32 | if width <= 2 { |
| 33 | w.Write([]byte(string(s.format[rRight]))) | |
| 33 | io.WriteString(w, str+string(s.format[rRight])) | |
| 34 | 34 | return |
| 35 | 35 | } |
| 36 | 36 | |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | 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)) | |
| 50 | 48 | } else { |
| 51 | w.Write(s.repeat(s.format[rFill], int(progressWidth))) | |
| 49 | str += runeRepeat(s.format[rFill], int(progressWidth)) | |
| 52 | 50 | } |
| 53 | 51 | |
| 54 | 52 | if needTip { |
| 55 | w.Write([]byte(string(s.format[rTip]))) | |
| 53 | str += string(s.format[rTip]) | |
| 56 | 54 | progressWidth++ |
| 57 | 55 | } |
| 58 | 56 | |
| 59 | 57 | 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) | |
| 62 | 60 | } |
| 63 | 61 | |
| 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) | |
| 66 | 64 | } |