1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
package mpb
import (
"io"
"unicode/utf8"
"github.com/vbauerster/mpb/v4/decor"
"github.com/vbauerster/mpb/v4/internal"
)
const (
rLeft = iota
rFill
rTip
rEmpty
rRight
rRevTip
rRefill
)
// DefaultBarStyle is applied when bar constructed with *Progress.AddBar method.
//
// '1th rune' stands for left boundary rune
//
// '2th rune' stands for fill rune
//
// '3th rune' stands for tip rune
//
// '4th rune' stands for empty rune
//
// '5th rune' stands for right boundary rune
//
// '6th rune' stands for reverse tip rune
//
// '7th rune' stands for refill rune
//
const DefaultBarStyle string = "[=>-]<+"
type barFiller struct {
format [][]byte
tip []byte
refill int64
reverse bool
flush func(w io.Writer, bb [][]byte)
}
// NewBarFiller constucts mpb.Filler, to be used with *Progress.Add method.
func NewBarFiller(style string, reverse bool) Filler {
if style == "" {
style = DefaultBarStyle
}
bf := &barFiller{
format: make([][]byte, utf8.RuneCountInString(style)),
}
bf.SetStyle(style)
bf.SetReverse(reverse)
return bf
}
func (s *barFiller) SetStyle(style string) {
if !utf8.ValidString(style) {
return
}
src := make([][]byte, 0, utf8.RuneCountInString(style))
for _, r := range style {
src = append(src, []byte(string(r)))
}
copy(s.format, src)
if s.reverse {
s.tip = s.format[rRevTip]
} else {
s.tip = s.format[rTip]
}
}
func (s *barFiller) SetReverse(reverse bool) {
if reverse {
s.tip = s.format[rRevTip]
s.flush = func(w io.Writer, bb [][]byte) {
for i := len(bb) - 1; i >= 0; i-- {
w.Write(bb[i])
}
}
} else {
s.tip = s.format[rTip]
s.flush = func(w io.Writer, bb [][]byte) {
for i := 0; i < len(bb); i++ {
w.Write(bb[i])
}
}
}
s.reverse = reverse
}
func (s *barFiller) SetRefill(amount int64) {
s.refill = amount
}
func (s *barFiller) Fill(w io.Writer, width int, stat *decor.Statistics) {
// don't count rLeft and rRight as progress
width -= 2
if width < 2 {
return
}
w.Write(s.format[rLeft])
defer w.Write(s.format[rRight])
bb := make([][]byte, width)
cwidth := int(internal.PercentageRound(stat.Total, stat.Current, width))
for i := 0; i < cwidth; i++ {
bb[i] = s.format[rFill]
}
if s.refill > 0 {
var rwidth int
if s.refill > stat.Current {
rwidth = cwidth
} else {
rwidth = int(internal.PercentageRound(stat.Total, int64(s.refill), width))
}
for i := 0; i < rwidth; i++ {
bb[i] = s.format[rRefill]
}
}
if cwidth > 0 && cwidth < width {
bb[cwidth-1] = s.tip
}
for i := cwidth; i < width; i++ {
bb[i] = s.format[rEmpty]
}
s.flush(w, bb)
}
|