Codebase list golang-github-vbauerster-mpb / 47a7273
SetRefill by interface probe Vladimir Bauer 7 years ago
6 changed file(s) with 51 addition(s) and 54 deletion(s). Raw diff Collapse all Expand all
5757 pDecorators []decor.Decorator
5858 amountReceivers []decor.AmountReceiver
5959 shutdownListeners []decor.ShutdownListener
60 refill *refill
6160 bufP, bufB, bufA *bytes.Buffer
6261 bufNL *bytes.Buffer
6362 panicMsg string
6665 // following options are assigned to the *Bar
6766 priority int
6867 runningBar *Bar
69 }
70 refill struct {
71 r rune
72 limit int64
7368 }
7469 frameReader struct {
7570 io.Reader
199194 }
200195 }
201196
202 // SetRefill sets fill rune to r, up until n.
203 func (b *Bar) SetRefill(n int, r rune) {
204 if n <= 0 {
205 return
206 }
197 // SetRefill sets refill, if supported by underlying Filler.
198 func (b *Bar) SetRefill(upto int) {
207199 b.operateState <- func(s *bState) {
208 if bf, ok := s.filler.(*barFiller); ok {
209 bf.refill = &refill{r, int64(n)}
200 if f, ok := s.filler.(interface{ SetRefill(int) }); ok {
201 f.SetRefill(upto)
210202 }
211203 }
212204 }
351343 s.bufB.WriteByte(' ')
352344 }
353345
346 calcWidth := s.width
354347 if prependCount+s.width+appendCount > termWidth {
355 s.filler.Fill(s.bufB, termWidth-prependCount-appendCount, stat)
356 } else {
357 s.filler.Fill(s.bufB, s.width, stat)
358 }
348 calcWidth = termWidth - prependCount - appendCount
349 }
350 s.filler.Fill(s.bufB, calcWidth, stat)
359351
360352 if !s.trimSpace {
361353 s.bufB.WriteByte(' ')
1313 rTip
1414 rEmpty
1515 rRight
16 rRefill
1617 )
1718
18 var defaultBarStyle = []rune("[=>-]")
19 var defaultBarStyle = []rune("[=>-]+")
1920
2021 type barFiller struct {
2122 format []rune
22 refill *refill
23 rup int
2324 }
2425
2526 func (s *barFiller) Fill(w io.Writer, width int, stat *decor.Statistics) {
4142 progressWidth--
4243 }
4344
44 if s.refill != nil {
45 refillCount := internal.Percentage(stat.Total, s.refill.limit, int64(width))
45 if s.rup > 0 {
46 refillCount := internal.Percentage(stat.Total, int64(s.rup), int64(width))
4647 rest := progressWidth - refillCount
47 str += runeRepeat(s.refill.r, int(refillCount)) + runeRepeat(s.format[rFill], int(rest))
48 str += runeRepeat(s.format[rRefill], int(refillCount)) + runeRepeat(s.format[rFill], int(rest))
4849 } else {
4950 str += runeRepeat(s.format[rFill], int(progressWidth))
5051 }
5960 io.WriteString(w, str)
6061 }
6162
63 func (s *barFiller) SetRefill(upto int) {
64 s.rup = upto
65 }
66
6267 func runeRepeat(r rune, count int) string {
6368 return strings.Repeat(string(r), count)
6469 }
117117 return t, ok
118118 }
119119 cb := func(t interface{}) {
120 bf := t.(*barFiller)
121120 if !utf8.ValidString(style) {
122 panic("invalid style string")
121 return
123122 }
124 defaultFormat := bf.format
125 bf.format = []rune(style)
126 if len(bf.format) < 5 {
127 bf.format = defaultFormat
128 }
123 copy(t.(*barFiller).format, []rune(style))
129124 }
130125 return MakeFillerTypeSpecificBarOption(chk, cb)
131126 }
88 "time"
99 "unicode/utf8"
1010
11 "github.com/vbauerster/mpb/v4"
11 . "github.com/vbauerster/mpb/v4"
1212 "github.com/vbauerster/mpb/v4/decor"
1313 )
1414
1515 func TestBarCompleted(t *testing.T) {
16 p := mpb.New(mpb.WithOutput(ioutil.Discard))
16 p := New(WithOutput(ioutil.Discard))
1717 total := 80
1818 bar := p.AddBar(int64(total))
1919
3131 }
3232
3333 func TestBarID(t *testing.T) {
34 p := mpb.New(mpb.WithOutput(ioutil.Discard))
34 p := New(WithOutput(ioutil.Discard))
3535 total := 80
3636 wantID := 11
37 bar := p.AddBar(int64(total), mpb.BarID(wantID))
37 bar := p.AddBar(int64(total), BarID(wantID))
3838
3939 go func() {
4040 for i := 0; i < total; i++ {
5656 var buf bytes.Buffer
5757
5858 width := 100
59 p := mpb.New(mpb.WithOutput(&buf), mpb.WithWidth(width))
59 p := New(WithOutput(&buf), WithWidth(width))
6060
6161 total := 100
6262 till := 30
63 refillRune := '+'
63 refillRune := DefaultBarStyle[len(DefaultBarStyle)-1]
6464
65 bar := p.AddBar(int64(total), mpb.TrimSpace())
65 bar := p.AddBar(int64(total), TrimSpace())
6666
67 bar.SetRefill(till, refillRune)
67 bar.SetRefill(till)
6868 bar.IncrBy(till)
6969
7070 for i := 0; i < total-till; i++ {
8989 func TestBarStyle(t *testing.T) {
9090 var buf bytes.Buffer
9191 customFormat := "╢▌▌░╟"
92 p := mpb.New(mpb.WithOutput(&buf))
92 p := New(WithOutput(&buf))
9393 total := 80
94 bar := p.AddBar(int64(total), mpb.BarStyle(customFormat), mpb.TrimSpace())
94 bar := p.AddBar(int64(total), BarStyle(customFormat), TrimSpace())
9595
9696 for i := 0; i < total; i++ {
9797 bar.Increment()
115115
116116 func TestBarPanics(t *testing.T) {
117117 var buf bytes.Buffer
118 p := mpb.New(mpb.WithDebugOutput(&buf), mpb.WithOutput(ioutil.Discard))
118 p := New(WithDebugOutput(&buf), WithOutput(ioutil.Discard))
119119
120120 wantPanic := "Upps!!!"
121121 total := 100
122122
123 bar := p.AddBar(int64(total), mpb.PrependDecorators(panicDecorator(wantPanic)))
123 bar := p.AddBar(int64(total), PrependDecorators(panicDecorator(wantPanic)))
124124
125125 go func() {
126126 for i := 0; i < 100; i++ {
1010 name string
1111 total, current int64
1212 barWidth int
13 barRefill *refill
1413 trimSpace bool
14 rup int
1515 want string
1616 }{
1717 100: {
5353 want: " [=====================================>----------------------------------------------------------] ",
5454 },
5555 {
56 name: "t,c,bw{100,40,100}:refill{'+', 32}",
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",
5765 total: 100,
5866 current: 40,
5967 barWidth: 100,
60 barRefill: &refill{'+', 32},
61 want: " [+++++++++++++++++++++++++++++++======>----------------------------------------------------------] ",
62 },
63 {
64 name: "t,c,bw{100,40,100}:refill{'+', 32}:trimSpace",
65 total: 100,
66 current: 40,
67 barWidth: 100,
68 barRefill: &refill{'+', 32},
68 rup: 32,
6969 trimSpace: true,
7070 want: "[+++++++++++++++++++++++++++++++=======>-----------------------------------------------------------]",
7171 },
214214 s.total = tc.total
215215 s.current = tc.current
216216 s.trimSpace = tc.trimSpace
217 if tc.barRefill != nil {
218 s.filler.(*barFiller).refill = tc.barRefill
217 if tc.rup > 0 {
218 if f, ok := s.filler.(interface{ SetRefill(int) }); ok {
219 f.SetRefill(tc.rup)
220 }
219221 }
220222 tmpBuf.Reset()
221223 tmpBuf.ReadFrom(s.draw(termWidth))
00 package mpb
11
2 var SyncWidth = syncWidth
2 var (
3 SyncWidth = syncWidth
4 DefaultBarStyle = defaultBarStyle
5 )