diff --git a/bar.go b/bar.go index 251b3d3..297cfa6 100644 --- a/bar.go +++ b/bar.go @@ -36,7 +36,7 @@ operateState chan func(*bState) int64Ch chan int64 boolCh chan bool - frameReaderCh chan io.Reader + frameReaderCh chan *frameReader syncTableCh chan [][]chan int bufNL *bytes.Buffer @@ -110,7 +110,7 @@ operateState: make(chan func(*bState)), int64Ch: make(chan int64), boolCh: make(chan bool), - frameReaderCh: make(chan io.Reader, 1), + frameReaderCh: make(chan *frameReader, 1), syncTableCh: make(chan [][]chan int), done: make(chan struct{}), shutdown: make(chan struct{}), diff --git a/cwriter/writer.go b/cwriter/writer.go index 8caeae1..8e7d01d 100644 --- a/cwriter/writer.go +++ b/cwriter/writer.go @@ -27,18 +27,22 @@ type Writer struct { out io.Writer buf bytes.Buffer + lineSep []byte lineCount int } // New returns a new Writer with defaults func New(w io.Writer) *Writer { - return &Writer{out: w} + return &Writer{ + out: w, + lineSep: []byte("\n"), + } } // Flush flushes the underlying buffer func (w *Writer) Flush() error { err := w.clearLines() - w.lineCount = bytes.Count(w.buf.Bytes(), []byte("\n")) + w.lineCount = bytes.Count(w.buf.Bytes(), w.lineSep) // WriteTo takes care of w.buf.Reset if _, e := w.buf.WriteTo(w.out); err == nil { err = e diff --git a/progress.go b/progress.go index 5fc91fd..4f6d5a3 100644 --- a/progress.go +++ b/progress.go @@ -201,12 +201,12 @@ func (s *pState) flush() (err error) { for s.bHeap.Len() > 0 { bar := heap.Pop(s.bHeap).(*Bar) - reader := <-bar.frameReaderCh - if _, e := s.cw.ReadFrom(reader); e != nil { + frameReader := <-bar.frameReaderCh + if _, e := s.cw.ReadFrom(frameReader); e != nil { err = e } defer func() { - if frame, ok := reader.(*frameReader); ok && frame.toShutdown { + if frameReader.toShutdown { // shutdown at next flush, in other words decrement underlying WaitGroup // only after the bar with completed state has been flushed. // this ensures no bar ends up with less than 100% rendered. @@ -216,7 +216,7 @@ s.heapUpdated = true delete(s.waitBars, bar) } - if frame.removeOnComplete { + if frameReader.removeOnComplete { s.heapUpdated = true return }