SetRefill by interface probe
Vladimir Bauer
7 years ago
| 57 | 57 | pDecorators []decor.Decorator |
| 58 | 58 | amountReceivers []decor.AmountReceiver |
| 59 | 59 | shutdownListeners []decor.ShutdownListener |
| 60 | refill *refill | |
| 61 | 60 | bufP, bufB, bufA *bytes.Buffer |
| 62 | 61 | bufNL *bytes.Buffer |
| 63 | 62 | panicMsg string |
| 66 | 65 | // following options are assigned to the *Bar |
| 67 | 66 | priority int |
| 68 | 67 | runningBar *Bar |
| 69 | } | |
| 70 | refill struct { | |
| 71 | r rune | |
| 72 | limit int64 | |
| 73 | 68 | } |
| 74 | 69 | frameReader struct { |
| 75 | 70 | io.Reader |
| 199 | 194 | } |
| 200 | 195 | } |
| 201 | 196 | |
| 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) { | |
| 207 | 199 | 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) | |
| 210 | 202 | } |
| 211 | 203 | } |
| 212 | 204 | } |
| 351 | 343 | s.bufB.WriteByte(' ') |
| 352 | 344 | } |
| 353 | 345 | |
| 346 | calcWidth := s.width | |
| 354 | 347 | 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) | |
| 359 | 351 | |
| 360 | 352 | if !s.trimSpace { |
| 361 | 353 | s.bufB.WriteByte(' ') |
| 13 | 13 | rTip |
| 14 | 14 | rEmpty |
| 15 | 15 | rRight |
| 16 | rRefill | |
| 16 | 17 | ) |
| 17 | 18 | |
| 18 | var defaultBarStyle = []rune("[=>-]") | |
| 19 | var defaultBarStyle = []rune("[=>-]+") | |
| 19 | 20 | |
| 20 | 21 | type barFiller struct { |
| 21 | 22 | format []rune |
| 22 | refill *refill | |
| 23 | rup int | |
| 23 | 24 | } |
| 24 | 25 | |
| 25 | 26 | func (s *barFiller) Fill(w io.Writer, width int, stat *decor.Statistics) { |
| 41 | 42 | progressWidth-- |
| 42 | 43 | } |
| 43 | 44 | |
| 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)) | |
| 46 | 47 | 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)) | |
| 48 | 49 | } else { |
| 49 | 50 | str += runeRepeat(s.format[rFill], int(progressWidth)) |
| 50 | 51 | } |
| 59 | 60 | io.WriteString(w, str) |
| 60 | 61 | } |
| 61 | 62 | |
| 63 | func (s *barFiller) SetRefill(upto int) { | |
| 64 | s.rup = upto | |
| 65 | } | |
| 66 | ||
| 62 | 67 | func runeRepeat(r rune, count int) string { |
| 63 | 68 | return strings.Repeat(string(r), count) |
| 64 | 69 | } |
| 117 | 117 | return t, ok |
| 118 | 118 | } |
| 119 | 119 | cb := func(t interface{}) { |
| 120 | bf := t.(*barFiller) | |
| 121 | 120 | if !utf8.ValidString(style) { |
| 122 | panic("invalid style string") | |
| 121 | return | |
| 123 | 122 | } |
| 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)) | |
| 129 | 124 | } |
| 130 | 125 | return MakeFillerTypeSpecificBarOption(chk, cb) |
| 131 | 126 | } |
| 8 | 8 | "time" |
| 9 | 9 | "unicode/utf8" |
| 10 | 10 | |
| 11 | "github.com/vbauerster/mpb/v4" | |
| 11 | . "github.com/vbauerster/mpb/v4" | |
| 12 | 12 | "github.com/vbauerster/mpb/v4/decor" |
| 13 | 13 | ) |
| 14 | 14 | |
| 15 | 15 | func TestBarCompleted(t *testing.T) { |
| 16 | p := mpb.New(mpb.WithOutput(ioutil.Discard)) | |
| 16 | p := New(WithOutput(ioutil.Discard)) | |
| 17 | 17 | total := 80 |
| 18 | 18 | bar := p.AddBar(int64(total)) |
| 19 | 19 | |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | func TestBarID(t *testing.T) { |
| 34 | p := mpb.New(mpb.WithOutput(ioutil.Discard)) | |
| 34 | p := New(WithOutput(ioutil.Discard)) | |
| 35 | 35 | total := 80 |
| 36 | 36 | wantID := 11 |
| 37 | bar := p.AddBar(int64(total), mpb.BarID(wantID)) | |
| 37 | bar := p.AddBar(int64(total), BarID(wantID)) | |
| 38 | 38 | |
| 39 | 39 | go func() { |
| 40 | 40 | for i := 0; i < total; i++ { |
| 56 | 56 | var buf bytes.Buffer |
| 57 | 57 | |
| 58 | 58 | width := 100 |
| 59 | p := mpb.New(mpb.WithOutput(&buf), mpb.WithWidth(width)) | |
| 59 | p := New(WithOutput(&buf), WithWidth(width)) | |
| 60 | 60 | |
| 61 | 61 | total := 100 |
| 62 | 62 | till := 30 |
| 63 | refillRune := '+' | |
| 63 | refillRune := DefaultBarStyle[len(DefaultBarStyle)-1] | |
| 64 | 64 | |
| 65 | bar := p.AddBar(int64(total), mpb.TrimSpace()) | |
| 65 | bar := p.AddBar(int64(total), TrimSpace()) | |
| 66 | 66 | |
| 67 | bar.SetRefill(till, refillRune) | |
| 67 | bar.SetRefill(till) | |
| 68 | 68 | bar.IncrBy(till) |
| 69 | 69 | |
| 70 | 70 | for i := 0; i < total-till; i++ { |
| 89 | 89 | func TestBarStyle(t *testing.T) { |
| 90 | 90 | var buf bytes.Buffer |
| 91 | 91 | customFormat := "╢▌▌░╟" |
| 92 | p := mpb.New(mpb.WithOutput(&buf)) | |
| 92 | p := New(WithOutput(&buf)) | |
| 93 | 93 | total := 80 |
| 94 | bar := p.AddBar(int64(total), mpb.BarStyle(customFormat), mpb.TrimSpace()) | |
| 94 | bar := p.AddBar(int64(total), BarStyle(customFormat), TrimSpace()) | |
| 95 | 95 | |
| 96 | 96 | for i := 0; i < total; i++ { |
| 97 | 97 | bar.Increment() |
| 115 | 115 | |
| 116 | 116 | func TestBarPanics(t *testing.T) { |
| 117 | 117 | var buf bytes.Buffer |
| 118 | p := mpb.New(mpb.WithDebugOutput(&buf), mpb.WithOutput(ioutil.Discard)) | |
| 118 | p := New(WithDebugOutput(&buf), WithOutput(ioutil.Discard)) | |
| 119 | 119 | |
| 120 | 120 | wantPanic := "Upps!!!" |
| 121 | 121 | total := 100 |
| 122 | 122 | |
| 123 | bar := p.AddBar(int64(total), mpb.PrependDecorators(panicDecorator(wantPanic))) | |
| 123 | bar := p.AddBar(int64(total), PrependDecorators(panicDecorator(wantPanic))) | |
| 124 | 124 | |
| 125 | 125 | go func() { |
| 126 | 126 | for i := 0; i < 100; i++ { |
| 10 | 10 | name string |
| 11 | 11 | total, current int64 |
| 12 | 12 | barWidth int |
| 13 | barRefill *refill | |
| 14 | 13 | trimSpace bool |
| 14 | rup int | |
| 15 | 15 | want string |
| 16 | 16 | }{ |
| 17 | 17 | 100: { |
| 53 | 53 | want: " [=====================================>----------------------------------------------------------] ", |
| 54 | 54 | }, |
| 55 | 55 | { |
| 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", | |
| 57 | 65 | total: 100, |
| 58 | 66 | current: 40, |
| 59 | 67 | 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, | |
| 69 | 69 | trimSpace: true, |
| 70 | 70 | want: "[+++++++++++++++++++++++++++++++=======>-----------------------------------------------------------]", |
| 71 | 71 | }, |
| 214 | 214 | s.total = tc.total |
| 215 | 215 | s.current = tc.current |
| 216 | 216 | 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 | } | |
| 219 | 221 | } |
| 220 | 222 | tmpBuf.Reset() |
| 221 | 223 | tmpBuf.ReadFrom(s.draw(termWidth)) |