Codebase list golang-github-vbauerster-mpb / 83f3fbc
refactoring: simplify BarFiller's signature Move reqWidth param into decor.Statistics. Latter one already had AvailableWidth, so having RequestedWidth there feels ok. Vladimir Bauer 3 years ago
7 changed file(s) with 22 addition(s) and 23 deletion(s). Raw diff Collapse all Expand all
2828 recoveredPanic interface{}
2929 }
3030
31 type extenderFunc func(rows []io.Reader, width int, stat decor.Statistics) []io.Reader
31 type extenderFunc func(rows []io.Reader, stat decor.Statistics) []io.Reader
3232
3333 // bState is actual bar's state.
3434 type bState struct {
383383 b.recoveredPanic = p
384384 }
385385 if fn := s.extender; fn != nil {
386 rows = fn(rows, s.reqWidth, stat)
386 rows = fn(rows, stat)
387387 }
388388 frame := &renderFrame{
389389 rows: rows,
405405 rows = append(rows, s.draw(stat))
406406 }
407407 if fn := s.extender; fn != nil {
408 rows = fn(rows, s.reqWidth, stat)
408 rows = fn(rows, stat)
409409 }
410410 frame := &renderFrame{
411411 rows: rows,
487487 return io.MultiReader(bufP, bufB, trunc, nlr)
488488 }
489489
490 s.filler.Fill(bufB, s.reqWidth, stat)
490 s.filler.Fill(bufB, stat)
491491
492492 return io.MultiReader(bufP, bufB, bufA, nlr)
493493 }
588588 func newStatistics(tw int, s *bState) decor.Statistics {
589589 return decor.Statistics{
590590 AvailableWidth: tw,
591 RequestedWidth: s.reqWidth,
591592 ID: s.id,
592593 Total: s.total,
593594 Current: s.current,
606607
607608 func makePanicExtender(p interface{}) extenderFunc {
608609 pstr := fmt.Sprint(p)
609 return func(rows []io.Reader, _ int, stat decor.Statistics) []io.Reader {
610 return func(rows []io.Reader, stat decor.Statistics) []io.Reader {
610611 r := io.MultiReader(
611612 strings.NewReader(runewidth.Truncate(pstr, stat.AvailableWidth, "…")),
612613 bytes.NewReader([]byte("\n")),
88 // BarFiller interface.
99 // Bar (without decorators) renders itself by calling BarFiller's Fill method.
1010 //
11 // reqWidth is requested width set by `func WithWidth(int) ContainerOption`.
1211 // If not set, it defaults to terminal width.
13 //
1412 type BarFiller interface {
15 Fill(w io.Writer, reqWidth int, stat decor.Statistics)
13 Fill(w io.Writer, stat decor.Statistics)
1614 }
1715
1816 // BarFillerBuilder interface.
2119 // BarStyle()
2220 // SpinnerStyle()
2321 // NopStyle()
24 //
2522 type BarFillerBuilder interface {
2623 Build() BarFiller
2724 }
2825
2926 // BarFillerFunc is function type adapter to convert compatible function
3027 // into BarFiller interface.
31 type BarFillerFunc func(w io.Writer, reqWidth int, stat decor.Statistics)
28 type BarFillerFunc func(w io.Writer, stat decor.Statistics)
3229
33 func (f BarFillerFunc) Fill(w io.Writer, reqWidth int, stat decor.Statistics) {
34 f(w, reqWidth, stat)
30 func (f BarFillerFunc) Fill(w io.Writer, stat decor.Statistics) {
31 f(w, stat)
3532 }
3633
3734 // BarFillerBuilderFunc is function type adapter to convert compatible
147147 return bf
148148 }
149149
150 func (s *bFiller) Fill(w io.Writer, width int, stat decor.Statistics) {
151 width = internal.CheckRequestedWidth(width, stat.AvailableWidth)
150 func (s *bFiller) Fill(w io.Writer, stat decor.Statistics) {
151 width := internal.CheckRequestedWidth(stat.RequestedWidth, stat.AvailableWidth)
152152 brackets := s.components[iLbound].width + s.components[iRbound].width
153153 // don't count brackets as progress
154154 width -= brackets
88 // NopStyle provides BarFillerBuilder which builds NOP BarFiller.
99 func NopStyle() BarFillerBuilder {
1010 return BarFillerBuilderFunc(func() BarFiller {
11 return BarFillerFunc(func(io.Writer, int, decor.Statistics) {})
11 return BarFillerFunc(func(io.Writer, decor.Statistics) {})
1212 })
1313 }
6262 return sf
6363 }
6464
65 func (s *sFiller) Fill(w io.Writer, width int, stat decor.Statistics) {
66 width = internal.CheckRequestedWidth(width, stat.AvailableWidth)
65 func (s *sFiller) Fill(w io.Writer, stat decor.Statistics) {
66 width := internal.CheckRequestedWidth(stat.RequestedWidth, stat.AvailableWidth)
6767
6868 frame := s.frames[s.count%uint(len(s.frames))]
6969 frameWidth := runewidth.StringWidth(stripansi.Strip(frame))
9090 // BarFillerOnComplete replaces bar's filler with message, on complete event.
9191 func BarFillerOnComplete(message string) BarOption {
9292 return BarFillerMiddleware(func(base BarFiller) BarFiller {
93 return BarFillerFunc(func(w io.Writer, reqWidth int, st decor.Statistics) {
93 return BarFillerFunc(func(w io.Writer, st decor.Statistics) {
9494 if st.Completed {
9595 _, err := io.WriteString(w, message)
9696 if err != nil {
9797 panic(err)
9898 }
9999 } else {
100 base.Fill(w, reqWidth, st)
100 base.Fill(w, st)
101101 }
102102 })
103103 })
143143
144144 func makeExtenderFunc(filler BarFiller, rev bool) extenderFunc {
145145 buf := new(bytes.Buffer)
146 base := func(rows []io.Reader, width int, stat decor.Statistics) []io.Reader {
146 base := func(rows []io.Reader, stat decor.Statistics) []io.Reader {
147147 buf.Reset()
148 filler.Fill(buf, width, stat)
148 filler.Fill(buf, stat)
149149 for {
150150 b, err := buf.ReadBytes('\n')
151151 if err != nil {
159159 if !rev {
160160 return base
161161 } else {
162 return func(rows []io.Reader, width int, stat decor.Statistics) []io.Reader {
163 rows = base(rows, width, stat)
162 return func(rows []io.Reader, stat decor.Statistics) []io.Reader {
163 rows = base(rows, stat)
164164 for left, right := 0, len(rows)-1; left < right; left, right = left+1, right-1 {
165165 rows[left], rows[right] = rows[right], rows[left]
166166 }
4747 // may need.
4848 type Statistics struct {
4949 AvailableWidth int
50 RequestedWidth int
5051 ID int
5152 Total int64
5253 Current int64