diff --git a/bar.go b/bar.go index 7db860e..0c95b2c 100644 --- a/bar.go +++ b/bar.go @@ -29,7 +29,7 @@ recoveredPanic interface{} } -type extenderFunc func(rows []io.Reader, width int, stat decor.Statistics) []io.Reader +type extenderFunc func(rows []io.Reader, stat decor.Statistics) []io.Reader // bState is actual bar's state. type bState struct { @@ -384,7 +384,7 @@ b.recoveredPanic = p } if fn := s.extender; fn != nil { - rows = fn(rows, s.reqWidth, stat) + rows = fn(rows, stat) } frame := &renderFrame{ rows: rows, @@ -406,7 +406,7 @@ rows = append(rows, s.draw(stat)) } if fn := s.extender; fn != nil { - rows = fn(rows, s.reqWidth, stat) + rows = fn(rows, stat) } frame := &renderFrame{ rows: rows, @@ -488,7 +488,7 @@ return io.MultiReader(bufP, bufB, trunc, nlr) } - s.filler.Fill(bufB, s.reqWidth, stat) + s.filler.Fill(bufB, stat) return io.MultiReader(bufP, bufB, bufA, nlr) } @@ -589,6 +589,7 @@ func newStatistics(tw int, s *bState) decor.Statistics { return decor.Statistics{ AvailableWidth: tw, + RequestedWidth: s.reqWidth, ID: s.id, Total: s.total, Current: s.current, @@ -607,7 +608,7 @@ func makePanicExtender(p interface{}) extenderFunc { pstr := fmt.Sprint(p) - return func(rows []io.Reader, _ int, stat decor.Statistics) []io.Reader { + return func(rows []io.Reader, stat decor.Statistics) []io.Reader { r := io.MultiReader( strings.NewReader(runewidth.Truncate(pstr, stat.AvailableWidth, "…")), bytes.NewReader([]byte("\n")), diff --git a/bar_filler.go b/bar_filler.go index 81177fc..ffdaa80 100644 --- a/bar_filler.go +++ b/bar_filler.go @@ -9,11 +9,9 @@ // BarFiller interface. // Bar (without decorators) renders itself by calling BarFiller's Fill method. // -// reqWidth is requested width set by `func WithWidth(int) ContainerOption`. // If not set, it defaults to terminal width. -// type BarFiller interface { - Fill(w io.Writer, reqWidth int, stat decor.Statistics) + Fill(w io.Writer, stat decor.Statistics) } // BarFillerBuilder interface. @@ -22,17 +20,16 @@ // BarStyle() // SpinnerStyle() // NopStyle() -// type BarFillerBuilder interface { Build() BarFiller } // BarFillerFunc is function type adapter to convert compatible function // into BarFiller interface. -type BarFillerFunc func(w io.Writer, reqWidth int, stat decor.Statistics) +type BarFillerFunc func(w io.Writer, stat decor.Statistics) -func (f BarFillerFunc) Fill(w io.Writer, reqWidth int, stat decor.Statistics) { - f(w, reqWidth, stat) +func (f BarFillerFunc) Fill(w io.Writer, stat decor.Statistics) { + f(w, stat) } // BarFillerBuilderFunc is function type adapter to convert compatible diff --git a/bar_filler_bar.go b/bar_filler_bar.go index d8bf90a..cf6cdb5 100644 --- a/bar_filler_bar.go +++ b/bar_filler_bar.go @@ -148,8 +148,8 @@ return bf } -func (s *bFiller) Fill(w io.Writer, width int, stat decor.Statistics) { - width = internal.CheckRequestedWidth(width, stat.AvailableWidth) +func (s *bFiller) Fill(w io.Writer, stat decor.Statistics) { + width := internal.CheckRequestedWidth(stat.RequestedWidth, stat.AvailableWidth) brackets := s.components[iLbound].width + s.components[iRbound].width // don't count brackets as progress width -= brackets diff --git a/bar_filler_nop.go b/bar_filler_nop.go index 1a7086f..926a6a9 100644 --- a/bar_filler_nop.go +++ b/bar_filler_nop.go @@ -9,6 +9,6 @@ // NopStyle provides BarFillerBuilder which builds NOP BarFiller. func NopStyle() BarFillerBuilder { return BarFillerBuilderFunc(func() BarFiller { - return BarFillerFunc(func(io.Writer, int, decor.Statistics) {}) + return BarFillerFunc(func(io.Writer, decor.Statistics) {}) }) } diff --git a/bar_filler_spinner.go b/bar_filler_spinner.go index d38525e..84203ac 100644 --- a/bar_filler_spinner.go +++ b/bar_filler_spinner.go @@ -63,8 +63,8 @@ return sf } -func (s *sFiller) Fill(w io.Writer, width int, stat decor.Statistics) { - width = internal.CheckRequestedWidth(width, stat.AvailableWidth) +func (s *sFiller) Fill(w io.Writer, stat decor.Statistics) { + width := internal.CheckRequestedWidth(stat.RequestedWidth, stat.AvailableWidth) frame := s.frames[s.count%uint(len(s.frames))] frameWidth := runewidth.StringWidth(stripansi.Strip(frame)) diff --git a/bar_option.go b/bar_option.go index 3506ed2..372ec8f 100644 --- a/bar_option.go +++ b/bar_option.go @@ -91,14 +91,14 @@ // BarFillerOnComplete replaces bar's filler with message, on complete event. func BarFillerOnComplete(message string) BarOption { return BarFillerMiddleware(func(base BarFiller) BarFiller { - return BarFillerFunc(func(w io.Writer, reqWidth int, st decor.Statistics) { + return BarFillerFunc(func(w io.Writer, st decor.Statistics) { if st.Completed { _, err := io.WriteString(w, message) if err != nil { panic(err) } } else { - base.Fill(w, reqWidth, st) + base.Fill(w, st) } }) }) @@ -144,9 +144,9 @@ func makeExtenderFunc(filler BarFiller, rev bool) extenderFunc { buf := new(bytes.Buffer) - base := func(rows []io.Reader, width int, stat decor.Statistics) []io.Reader { + base := func(rows []io.Reader, stat decor.Statistics) []io.Reader { buf.Reset() - filler.Fill(buf, width, stat) + filler.Fill(buf, stat) for { b, err := buf.ReadBytes('\n') if err != nil { @@ -160,8 +160,8 @@ if !rev { return base } else { - return func(rows []io.Reader, width int, stat decor.Statistics) []io.Reader { - rows = base(rows, width, stat) + return func(rows []io.Reader, stat decor.Statistics) []io.Reader { + rows = base(rows, stat) for left, right := 0, len(rows)-1; left < right; left, right = left+1, right-1 { rows[left], rows[right] = rows[right], rows[left] } diff --git a/decor/decorator.go b/decor/decorator.go index aad7709..57d9569 100644 --- a/decor/decorator.go +++ b/decor/decorator.go @@ -48,6 +48,7 @@ // may need. type Statistics struct { AvailableWidth int + RequestedWidth int ID int Total int64 Current int64