Codebase list golang-github-vbauerster-mpb / 25b2e01
BarFiller: use all term width by default WithWidth Vladimir Bauer 6 years ago
9 changed file(s) with 34 addition(s) and 43 deletion(s). Raw diff Collapse all Expand all
4646 baseF BarFiller
4747 filler BarFiller
4848 id int
49 width int
49 reqWidth int
5050 total int64
5151 current int64
5252 lastN int64
321321 }()
322322
323323 st := newStatistics(tw, s)
324 frame, lines := s.extender(s.draw(st), s.width, st)
324 frame, lines := s.extender(s.draw(st), s.reqWidth, st)
325325 b.extendedLines = lines
326326
327327 b.toShutdown = s.toComplete && !s.completeFlushed
331331 case <-b.done:
332332 s := b.cacheState
333333 st := newStatistics(tw, s)
334 frame, lines := s.extender(s.draw(st), s.width, st)
334 frame, lines := s.extender(s.draw(st), s.reqWidth, st)
335335 b.extendedLines = lines
336336 b.frameCh <- frame
337337 }
398398
399399 s.bufA.WriteByte('\n')
400400
401 if !s.trimSpace && stat.TermWidth >= 2 {
401 if !s.trimSpace {
402402 defer s.bufB.WriteByte(' ')
403403 s.bufB.WriteByte(' ')
404404 stat.OccupiedWidth += 2
405405 }
406406
407 s.filler.Fill(s.bufB, s.width, stat)
407 s.filler.Fill(s.bufB, s.reqWidth, stat)
408408
409409 return io.MultiReader(s.bufP, s.bufB, s.bufA)
410410 }
8484 s.refill = amount
8585 }
8686
87 func (s *barFiller) Fill(w io.Writer, width int, stat decor.Statistics) {
88 // auto shrink
89 if stat.OccupiedWidth+width > stat.TermWidth {
90 width = stat.TermWidth - stat.OccupiedWidth
91 }
92 // don't count rLeft and rRight as progress
93 width -= 2
87 func (s *barFiller) Fill(w io.Writer, reqWidth int, stat decor.Statistics) {
88 width := internal.CalcWidthForBarFiller(reqWidth, stat.TermWidth-stat.OccupiedWidth)
89
90 width -= 2 // don't count rLeft and rRight as progress
9491 if width < 2 {
9592 return
9693 }
55 "unicode/utf8"
66
77 "github.com/vbauerster/mpb/v5/decor"
8 "github.com/vbauerster/mpb/v5/internal"
89 )
910
1011 // SpinnerAlignment enum.
3839 return filler
3940 }
4041
41 func (s *spinnerFiller) Fill(w io.Writer, width int, stat decor.Statistics) {
42 // auto shrink
43 if stat.OccupiedWidth+width > stat.TermWidth {
44 width = stat.TermWidth - stat.OccupiedWidth
45 }
42 func (s *spinnerFiller) Fill(w io.Writer, reqWidth int, stat decor.Statistics) {
43 width := internal.CalcWidthForBarFiller(reqWidth, stat.TermWidth-stat.OccupiedWidth)
4644
4745 frame := s.frames[s.count%uint(len(s.frames))]
4846 frameWidth := utf8.RuneCountInString(frame)
4545 // BarWidth sets bar width independent of the container.
4646 func BarWidth(width int) BarOption {
4747 return func(s *bState) {
48 s.width = width
48 s.reqWidth = width
4949 }
5050 }
5151
143143 func TestBarStyle(t *testing.T) {
144144 var buf bytes.Buffer
145145 customFormat := "╢▌▌░╟"
146 p := New(WithOutput(&buf))
147146 total := 80
147 p := New(WithOutput(&buf), WithWidth(total))
148148 bar := p.AddBar(int64(total), BarStyle(customFormat), TrimSpace())
149149
150150 for i := 0; i < total; i++ {
2020 }
2121 }
2222
23 // WithWidth sets container width. Default is 80. Bars inherit this
24 // width, as long as no BarWidth is applied.
23 // WithWidth sets container width. If not set underlying bars will
24 // occupy whole term width.
2525 func WithWidth(width int) ContainerOption {
2626 return func(s *pState) {
27 if width < 0 {
28 return
29 }
30 s.width = width
27 s.reqWidth = width
3128 }
3229 }
3330
22 import (
33 "bytes"
44 "testing"
5 "unicode/utf8"
65 )
76
87 func TestDraw(t *testing.T) {
2221 total: 60,
2322 current: 20,
2423 barWidth: 80,
25 want: "",
24 want: " ",
2625 },
2726 {
2827 name: "t,c,bw{60,20,80}trim",
3938 total: 60,
4039 current: 20,
4140 barWidth: 80,
42 want: "",
41 want: " ",
4342 },
4443 {
4544 name: "t,c,bw{60,20,80}trim",
322321 for termWidth, cases := range testSuite {
323322 for _, tc := range cases {
324323 s := newTestState(tc.reverse)
325 s.width = tc.barWidth
324 s.reqWidth = tc.barWidth
326325 s.total = tc.total
327326 s.current = tc.current
328327 s.trimSpace = tc.trimSpace
336335 by := tmpBuf.Bytes()
337336 by = by[:len(by)-1]
338337
339 if utf8.RuneCount(by) > termWidth {
340 t.Errorf("termWidth:%d %q barWidth:%d overflow termWidth\n", termWidth, tc.name, utf8.RuneCount(by))
341 }
342
343338 got := string(by)
344339 if got != tc.want {
345340 t.Errorf("termWidth:%d %q want: %q %d, got: %q %d\n", termWidth, tc.name, tc.want, len(tc.want), got, len(got))
1515 func PercentageRound(total, current int64, width int) float64 {
1616 return math.Round(Percentage(total, current, width))
1717 }
18
19 func CalcWidthForBarFiller(reqWidth, available int) int {
20 if reqWidth <= 0 || reqWidth >= available {
21 return available
22 }
23 return reqWidth
24 }
1818 const (
1919 // default RefreshRate
2020 prr = 120 * time.Millisecond
21 // default width
22 pwidth = 80
2321 )
2422
2523 // Progress represents the container that renders Progress bars
4543
4644 // following are provided/overrided by user
4745 idCount int
48 width int
46 reqWidth int
4947 popCompleted bool
5048 rr time.Duration
5149 uwg *sync.WaitGroup
6967 func NewWithContext(ctx context.Context, options ...ContainerOption) *Progress {
7068 s := &pState{
7169 bHeap: priorityQueue{},
72 width: pwidth,
7370 rr: prr,
7471 parkedBars: make(map[*Bar]*Bar),
7572 output: os.Stdout,
112109 // Panics if *Progress instance is done, i.e. called after *Progress.Wait().
113110 func (p *Progress) Add(total int64, filler BarFiller, options ...BarOption) *Bar {
114111 if filler == nil {
115 filler = NewBarFiller(DefaultBarStyle, false)
112 filler = BarFillerFunc(func(io.Writer, int, decor.Statistics) {})
116113 }
117114 p.bwg.Add(1)
118115 result := make(chan *Bar)
232229
233230 tw, err := cw.GetWidth()
234231 if err != nil {
235 tw = s.width
232 tw = s.reqWidth
236233 }
237234 for i := 0; i < s.bHeap.Len(); i++ {
238235 bar := s.bHeap[i]
346343 filler: filler,
347344 priority: s.idCount,
348345 id: s.idCount,
349 width: s.width,
346 reqWidth: s.reqWidth,
350347 debugOut: s.debugOut,
351348 extender: func(r io.Reader, _ int, _ decor.Statistics) (io.Reader, int) {
352349 return r, 0
363360 bs.priority = -1
364361 }
365362
366 bs.bufP = bytes.NewBuffer(make([]byte, 0, bs.width))
367 bs.bufB = bytes.NewBuffer(make([]byte, 0, bs.width))
368 bs.bufA = bytes.NewBuffer(make([]byte, 0, bs.width))
363 bs.bufP = bytes.NewBuffer(make([]byte, 0, 64))
364 bs.bufB = bytes.NewBuffer(make([]byte, 0, 128))
365 bs.bufA = bytes.NewBuffer(make([]byte, 0, 64))
369366
370367 return bs
371368 }