| 0 | 0 |
package mpb
|
| 1 | 1 |
|
| 2 | 2 |
import (
|
| 3 | |
"bytes"
|
| 4 | 3 |
"io"
|
| 5 | 4 |
"unicode/utf8"
|
| 6 | 5 |
|
|
| 14 | 13 |
rTip
|
| 15 | 14 |
rEmpty
|
| 16 | 15 |
rRight
|
|
16 |
rRevTip
|
| 17 | 17 |
rRefill
|
| 18 | 18 |
)
|
| 19 | 19 |
|
| 20 | |
var defaultBarStyle = "[=>-]+"
|
|
20 |
var defaultBarStyle = "[=>-]<+"
|
| 21 | 21 |
|
| 22 | 22 |
type barFiller struct {
|
| 23 | |
format [][]byte
|
| 24 | |
rup int
|
|
23 |
format [][]byte
|
|
24 |
refillCount int
|
|
25 |
reverse bool
|
| 25 | 26 |
}
|
| 26 | 27 |
|
| 27 | 28 |
func newDefaultBarFiller() Filler {
|
|
| 34 | 35 |
|
| 35 | 36 |
func (s *barFiller) setStyle(style string) {
|
| 36 | 37 |
if !utf8.ValidString(style) {
|
| 37 | |
style = defaultBarStyle
|
|
38 |
return
|
| 38 | 39 |
}
|
| 39 | 40 |
src := make([][]byte, 0, utf8.RuneCountInString(style))
|
| 40 | 41 |
for _, r := range style {
|
|
| 43 | 44 |
copy(s.format, src)
|
| 44 | 45 |
}
|
| 45 | 46 |
|
|
47 |
func (s *barFiller) setReverse() {
|
|
48 |
s.reverse = true
|
|
49 |
}
|
|
50 |
|
| 46 | 51 |
func (s *barFiller) Fill(w io.Writer, width int, stat *decor.Statistics) {
|
| 47 | |
|
| 48 | |
b := s.format[rLeft]
|
| 49 | 52 |
|
| 50 | 53 |
// don't count rLeft and rRight [brackets]
|
| 51 | 54 |
width -= 2
|
| 52 | |
|
| 53 | 55 |
if width < 2 {
|
| 54 | |
return
|
| 55 | |
} else if width == 2 {
|
| 56 | |
w.Write(append(b, s.format[rRight]...))
|
| 57 | 56 |
return
|
| 58 | 57 |
}
|
| 59 | 58 |
|
| 60 | |
cwidth := internal.Percentage(stat.Total, stat.Current, int64(width))
|
| 61 | |
|
| 62 | |
if s.rup > 0 {
|
| 63 | |
rwidth := internal.Percentage(stat.Total, int64(s.rup), int64(width))
|
| 64 | |
b = append(b, bytes.Repeat(s.format[rRefill], int(rwidth))...)
|
| 65 | |
rest := cwidth - rwidth
|
| 66 | |
b = append(b, bytes.Repeat(s.format[rFill], int(rest))...)
|
| 67 | |
} else {
|
| 68 | |
b = append(b, bytes.Repeat(s.format[rFill], int(cwidth))...)
|
|
59 |
w.Write(s.format[rLeft])
|
|
60 |
if width == 2 {
|
|
61 |
w.Write(s.format[rRight])
|
|
62 |
return
|
| 69 | 63 |
}
|
| 70 | 64 |
|
| 71 | |
if cwidth < int64(width) && cwidth > 0 {
|
| 72 | |
_, size := utf8.DecodeLastRune(b)
|
| 73 | |
b = append(b[:len(b)-size], s.format[rTip]...)
|
|
65 |
bb := make([][]byte, width)
|
|
66 |
|
|
67 |
cwidth := int(internal.Percentage(stat.Total, stat.Current, int64(width)))
|
|
68 |
|
|
69 |
for i := 0; i < cwidth; i++ {
|
|
70 |
bb[i] = s.format[rFill]
|
| 74 | 71 |
}
|
| 75 | 72 |
|
| 76 | |
rest := int64(width) - cwidth
|
| 77 | |
b = append(b, bytes.Repeat(s.format[rEmpty], int(rest))...)
|
| 78 | |
w.Write(append(b, s.format[rRight]...))
|
|
73 |
if s.refillCount > 0 {
|
|
74 |
var rwidth int
|
|
75 |
if s.refillCount > cwidth {
|
|
76 |
rwidth = cwidth
|
|
77 |
} else {
|
|
78 |
rwidth = int(internal.Percentage(stat.Total, int64(s.refillCount), int64(width)))
|
|
79 |
}
|
|
80 |
for i := 0; i < rwidth; i++ {
|
|
81 |
bb[i] = s.format[rRefill]
|
|
82 |
}
|
|
83 |
}
|
|
84 |
|
|
85 |
if cwidth < width && cwidth > 0 {
|
|
86 |
if s.reverse {
|
|
87 |
bb[cwidth-1] = s.format[rRevTip]
|
|
88 |
} else {
|
|
89 |
bb[cwidth-1] = s.format[rTip]
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
|
93 |
for i := cwidth; i < width; i++ {
|
|
94 |
bb[i] = s.format[rEmpty]
|
|
95 |
}
|
|
96 |
|
|
97 |
if s.reverse {
|
|
98 |
for i, j := 0, len(bb)-1; i < j; i, j = i+1, j-1 {
|
|
99 |
bb[i], bb[j] = bb[j], bb[i]
|
|
100 |
}
|
|
101 |
}
|
|
102 |
|
|
103 |
for i := 0; i < width; i++ {
|
|
104 |
w.Write(bb[i])
|
|
105 |
}
|
|
106 |
w.Write(s.format[rRight])
|
| 79 | 107 |
}
|
| 80 | 108 |
|
| 81 | |
func (s *barFiller) SetRefill(upto int) {
|
| 82 | |
s.rup = upto
|
|
109 |
func (s *barFiller) SetRefill(count int) {
|
|
110 |
s.refillCount = count
|
| 83 | 111 |
}
|