Codebase list golang-github-vbauerster-mpb / ccf4cd2
barFiller internal format is [][]byte Vladimir Bauer 7 years ago
5 changed file(s) with 247 addition(s) and 178 deletion(s). Raw diff Collapse all Expand all
00 package mpb
11
22 import (
3 "bytes"
34 "io"
4 "strings"
5 "unicode/utf8"
56
67 "github.com/vbauerster/mpb/v4/decor"
78 "github.com/vbauerster/mpb/v4/internal"
1617 rRefill
1718 )
1819
19 var defaultBarStyle = []rune("[=>-]+")
20 var defaultBarStyle = "[=>-]+"
2021
2122 type barFiller struct {
22 format []rune
23 format [][]byte
2324 rup int
25 }
26
27 func newDefaultBarFiller() Filler {
28 bf := &barFiller{
29 format: make([][]byte, utf8.RuneCountInString(defaultBarStyle)),
30 }
31 bf.setStyle(defaultBarStyle)
32 return bf
33 }
34
35 func (s *barFiller) setStyle(style string) {
36 if !utf8.ValidString(style) {
37 style = defaultBarStyle
38 }
39 src := make([][]byte, 0, utf8.RuneCountInString(style))
40 for _, r := range style {
41 src = append(src, []byte(string(r)))
42 }
43 copy(s.format, src)
2444 }
2545
2646 func (s *barFiller) Fill(w io.Writer, width int, stat *decor.Statistics) {
2747
28 str := string(s.format[rLeft])
48 b := s.format[rLeft]
2949
3050 // don't count rLeft and rRight [brackets]
3151 width -= 2
3252
33 if width <= 2 {
34 io.WriteString(w, str+string(s.format[rRight]))
53 if width < 2 {
54 return
55 } else if width == 2 {
56 w.Write(append(b, s.format[rRight]...))
3557 return
3658 }
3759
38 progressWidth := internal.Percentage(stat.Total, stat.Current, int64(width))
39 needTip := progressWidth < int64(width) && progressWidth > 0
60 cwidth := internal.Percentage(stat.Total, stat.Current, int64(width))
4061
41 if needTip {
42 progressWidth--
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))...)
4369 }
4470
45 if s.rup > 0 {
46 refillCount := internal.Percentage(stat.Total, int64(s.rup), int64(width))
47 rest := progressWidth - refillCount
48 str += runeRepeat(s.format[rRefill], int(refillCount)) + runeRepeat(s.format[rFill], int(rest))
49 } else {
50 str += runeRepeat(s.format[rFill], int(progressWidth))
71 if cwidth < int64(width) && cwidth > 0 {
72 _, size := utf8.DecodeLastRune(b)
73 b = append(b[:len(b)-size], s.format[rTip]...)
5174 }
5275
53 if needTip {
54 str += string(s.format[rTip])
55 progressWidth++
56 }
57
58 rest := int64(width) - progressWidth
59 str += runeRepeat(s.format[rEmpty], int(rest)) + string(s.format[rRight])
60 io.WriteString(w, str)
76 rest := int64(width) - cwidth
77 b = append(b, bytes.Repeat(s.format[rEmpty], int(rest))...)
78 w.Write(append(b, s.format[rRight]...))
6179 }
6280
6381 func (s *barFiller) SetRefill(upto int) {
6482 s.rup = upto
6583 }
66
67 func runeRepeat(r rune, count int) string {
68 return strings.Repeat(string(r), count)
69 }
00 package mpb
11
22 import (
3 "unicode/utf8"
4
53 "github.com/vbauerster/mpb/v4/decor"
64 )
75
115113 return t, ok
116114 }
117115 cb := func(t interface{}) {
118 if !utf8.ValidString(style) {
119 return
120 }
121 copy(t.(*barFiller).format, []rune(style))
116 t.(*barFiller).setStyle(style)
122117 }
123118 return MakeFillerTypeSpecificBarOption(chk, cb)
124119 }
6060
6161 total := 100
6262 till := 30
63 refillRune := DefaultBarStyle[len(DefaultBarStyle)-1]
63 refillRune, _ := utf8.DecodeLastRuneInString(DefaultBarStyle)
6464
6565 bar := p.AddBar(int64(total), TrimSpace())
6666
22 import (
33 "bytes"
44 "testing"
5 "unicode/utf8"
56 )
67
78 func TestDraw(t *testing.T) {
1415 rup int
1516 want string
1617 }{
18 2: {
19 {
20 name: "t,c,bw{60,20,80}",
21 total: 60,
22 current: 20,
23 barWidth: 80,
24 want: " ",
25 },
26 {
27 name: "t,c,bw,trim{60,20,80,true}",
28 total: 60,
29 current: 20,
30 barWidth: 80,
31 trimSpace: true,
32 want: "",
33 },
34 },
35 3: {
36 {
37 name: "t,c,bw{60,20,80}",
38 total: 60,
39 current: 20,
40 barWidth: 80,
41 want: " ",
42 },
43 {
44 name: "t,c,bw,trim{60,20,80,true}",
45 total: 60,
46 current: 20,
47 barWidth: 80,
48 trimSpace: true,
49 want: "",
50 },
51 },
52 4: {
53 {
54 name: "t,c,bw{60,20,80}",
55 total: 60,
56 current: 20,
57 barWidth: 80,
58 want: " ",
59 },
60 {
61 name: "t,c,bw,trim{60,20,80,true}",
62 total: 60,
63 current: 20,
64 barWidth: 80,
65 trimSpace: true,
66 want: "[]",
67 },
68 },
69 5: {
70 {
71 name: "t,c,bw{60,20,80}",
72 total: 60,
73 current: 20,
74 barWidth: 80,
75 want: " ",
76 },
77 {
78 name: "t,c,bw,trim{60,20,80,true}",
79 total: 60,
80 current: 20,
81 barWidth: 80,
82 trimSpace: true,
83 want: "[>--]",
84 },
85 },
86 6: {
87 {
88 name: "t,c,bw{60,20,80}",
89 total: 60,
90 current: 20,
91 barWidth: 80,
92 want: " [] ",
93 },
94 {
95 name: "t,c,bw,trim{60,20,80,true}",
96 total: 60,
97 current: 20,
98 barWidth: 80,
99 trimSpace: true,
100 want: "[>---]",
101 },
102 },
103 7: {
104 {
105 name: "t,c,bw{60,20,80}",
106 total: 60,
107 current: 20,
108 barWidth: 80,
109 want: " [>--] ",
110 },
111 {
112 name: "t,c,bw,trim{60,20,80,true}",
113 total: 60,
114 current: 20,
115 barWidth: 80,
116 trimSpace: true,
117 want: "[=>---]",
118 },
119 },
120 8: {
121 {
122 name: "t,c,bw{60,20,80}",
123 total: 60,
124 current: 20,
125 barWidth: 80,
126 want: " [>---] ",
127 },
128 {
129 name: "t,c,bw,trim{60,20,80,true}",
130 total: 60,
131 current: 20,
132 barWidth: 80,
133 trimSpace: true,
134 want: "[=>----]",
135 },
136 },
137 80: {
138 {
139 name: "t,c,bw{60,20,80}",
140 total: 60,
141 current: 20,
142 barWidth: 80,
143 want: " [========================>---------------------------------------------------] ",
144 },
145 {
146 name: "t,c,bw,trim{60,20,80,true}",
147 total: 60,
148 current: 20,
149 barWidth: 80,
150 trimSpace: true,
151 want: "[=========================>----------------------------------------------------]",
152 },
153 },
17154 100: {
18155 {
19156 name: "t,c,bw{100,100,0}",
23160 want: " [------------------------------------------------------------------------------------------------] ",
24161 },
25162 {
26 name: "t,c,bw{100,100,0}:trimSpace",
163 name: "t,c,bw,trim{100,100,0,true}",
27164 total: 100,
28165 current: 0,
29166 barWidth: 100,
38175 want: " [>-----------------------------------------------------------------------------------------------] ",
39176 },
40177 {
41 name: "t,c,bw{100,1,100}:trimSpace",
178 name: "t,c,bw,trim{100,1,100,true}",
42179 total: 100,
43180 current: 1,
44181 barWidth: 100,
46183 want: "[>-------------------------------------------------------------------------------------------------]",
47184 },
48185 {
49 name: "t,c,bw{100,40,100}",
186 name: "t,c,bw{100,33,100}",
187 total: 100,
188 current: 33,
189 barWidth: 100,
190 want: " [===============================>----------------------------------------------------------------] ",
191 },
192 {
193 name: "t,c,bw,trim{100,33,100,true}",
194 total: 100,
195 current: 33,
196 barWidth: 100,
197 trimSpace: true,
198 want: "[===============================>------------------------------------------------------------------]",
199 },
200 {
201 name: "t,c,bw,rup{100,33,100,33}",
202 total: 100,
203 current: 33,
204 barWidth: 100,
205 rup: 33,
206 want: " [+++++++++++++++++++++++++++++++>----------------------------------------------------------------] ",
207 },
208 {
209 name: "t,c,bw,rup,trim{100,33,100,33,true}",
210 total: 100,
211 current: 33,
212 barWidth: 100,
213 rup: 33,
214 trimSpace: true,
215 want: "[+++++++++++++++++++++++++++++++>------------------------------------------------------------------]",
216 },
217 {
218 name: "t,c,bw,rup{100,40,100,32}",
50219 total: 100,
51220 current: 40,
52221 barWidth: 100,
53 want: " [=====================================>----------------------------------------------------------] ",
54 },
55 {
56 name: "t,c,bw,rup{100,40,100,32}",
57 total: 100,
58 current: 40,
59 barWidth: 100,
60 rup: 32,
61 want: " [+++++++++++++++++++++++++++++++======>----------------------------------------------------------] ",
62 },
63 {
64 name: "t,c,bw,rup{100,40,100,32}:trimSpace",
222 rup: 33,
223 want: " [++++++++++++++++++++++++++++++++=====>----------------------------------------------------------] ",
224 },
225 {
226 name: "t,c,bw,rup,trim{100,40,100,32,true}",
65227 total: 100,
66228 current: 40,
67229 barWidth: 100,
68 rup: 32,
69 trimSpace: true,
70 want: "[+++++++++++++++++++++++++++++++=======>-----------------------------------------------------------]",
230 rup: 33,
231 trimSpace: true,
232 want: "[++++++++++++++++++++++++++++++++======>-----------------------------------------------------------]",
71233 },
72234 {
73235 name: "t,c,bw{100,99,100}",
77239 want: " [==============================================================================================>-] ",
78240 },
79241 {
242 name: "t,c,bw,trim{100,99,100,true}",
243 total: 100,
244 current: 99,
245 barWidth: 100,
246 trimSpace: true,
247 want: "[================================================================================================>-]",
248 },
249 {
80250 name: "t,c,bw{100,100,100}",
81251 total: 100,
82252 current: 100,
83253 barWidth: 100,
84254 want: " [================================================================================================] ",
85255 },
86 },
87 2: {
88 {
89 name: "t,c,bw{0,0,100}",
90 barWidth: 100,
91 want: " [] ",
92 },
93 {
94 name: "t,c,bw{60,20,80}",
95 total: 60,
96 current: 20,
97 barWidth: 80,
98 want: " [] ",
99 },
100 },
101 4: {
102 {
103 name: "t,c,bw{100,20,100}",
104 total: 100,
105 current: 20,
106 barWidth: 100,
107 want: " [] ",
108 },
109 {
110 name: "t,c,bw{100,98,100}",
111 total: 100,
112 current: 98,
113 barWidth: 100,
114 want: " [] ",
115 },
116 {
117 name: "t,c,bw{100,100,100}",
118 total: 100,
119 current: 100,
120 barWidth: 100,
121 want: " [] ",
122 },
123 },
124 8: {
125 {
126 name: "t,c,bw{100,20,100}",
127 total: 100,
128 current: 20,
129 barWidth: 100,
130 want: " [>---] ",
131 },
132 {
133 name: "t,c,bw{100,98,100}",
134 total: 100,
135 current: 98,
136 barWidth: 100,
137 want: " [====] ",
138 },
139 {
140 name: "t,c,bw{100,100,100}",
141 total: 100,
142 current: 100,
143 barWidth: 100,
144 want: " [====] ",
145 },
146 },
147 20: {
148 {
149 name: "t,c,bw{100,20,100}",
150 total: 100,
151 current: 20,
152 barWidth: 100,
153 want: " [==>-------------] ",
154 },
155 {
156 name: "t,c,bw{100,60,100}",
157 total: 100,
158 current: 60,
159 barWidth: 100,
160 want: " [=========>------] ",
161 },
162 {
163 name: "t,c,bw{100,98,100}",
164 total: 100,
165 current: 98,
166 barWidth: 100,
167 want: " [================] ",
168 },
169 {
170 name: "t,c,bw{100,100,100}",
171 total: 100,
172 current: 100,
173 barWidth: 100,
174 want: " [================] ",
175 },
176 },
177 50: {
178 {
179 name: "t,c,bw{100,20,100}",
180 total: 100,
181 current: 20,
182 barWidth: 100,
183 want: " [========>-------------------------------------] ",
184 },
185 {
186 name: "t,c,bw{100,60,100}",
187 total: 100,
188 current: 60,
189 barWidth: 100,
190 want: " [===========================>------------------] ",
191 },
192 {
193 name: "t,c,bw{100,98,100}",
194 total: 100,
195 current: 98,
196 barWidth: 100,
197 want: " [============================================>-] ",
198 },
199 {
200 name: "t,c,bw{100,100,100}",
201 total: 100,
202 current: 100,
203 barWidth: 100,
204 want: " [==============================================] ",
256 {
257 name: "t,c,bw,trim{100,100,100,true}",
258 total: 100,
259 current: 100,
260 barWidth: 100,
261 trimSpace: true,
262 want: "[==================================================================================================]",
205263 },
206264 },
207265 }
223281 tmpBuf.ReadFrom(s.draw(termWidth))
224282 by := tmpBuf.Bytes()
225283 by = by[:len(by)-1]
284
285 if utf8.RuneCount(by) > termWidth {
286 t.Errorf("termWidth:%d %q barWidth:%d overflow termWidth\n", termWidth, tc.name, utf8.RuneCount(by))
287 }
288
226289 got := string(by)
227290 if got != tc.want {
228 t.Errorf("termWidth %d; %s: want: %q %d, got: %q %d\n", termWidth, tc.name, tc.want, len(tc.want), got, len(got))
291 t.Errorf("termWidth:%d %q want: %q %d, got: %q %d\n", termWidth, tc.name, tc.want, len(tc.want), got, len(got))
229292 }
230293 }
231294 }
233296
234297 func newTestState() *bState {
235298 s := &bState{
236 filler: &barFiller{format: defaultBarStyle},
299 filler: newDefaultBarFiller(),
237300 bufP: new(bytes.Buffer),
238301 bufB: new(bytes.Buffer),
239302 bufA: new(bytes.Buffer),
8282
8383 // AddBar creates a new progress bar and adds to the container.
8484 func (p *Progress) AddBar(total int64, options ...BarOption) *Bar {
85 filler := &barFiller{
86 format: defaultBarStyle,
87 }
88 return p.Add(total, filler, options...)
85 return p.Add(total, newDefaultBarFiller(), options...)
8986 }
9087
9188 // AddSpinner creates a new spinner bar and adds to the container.