Codebase list golang-github-vbauerster-mpb / 2c527af
pass cw as arg Vladimir Bauer 7 years ago
2 changed file(s) with 26 addition(s) and 26 deletion(s). Raw diff Collapse all Expand all
44 "io"
55 "sync"
66 "time"
7
8 "github.com/vbauerster/mpb/v4/cwriter"
97 )
108
119 // ContainerOption is a function option which changes the default
7472 if w == nil {
7573 return
7674 }
77 s.cw = cwriter.New(w)
75 s.output = w
7876 }
7977 }
8078
3636 width int
3737 format string
3838 rr time.Duration
39 cw *cwriter.Writer
4039 pMatrix map[int][]chan int
4140 aMatrix map[int][]chan int
4241 forceRefreshCh chan time.Time
42 output io.Writer
4343
4444 // following are provided/overrided by user
4545 ctx context.Context
5555 func New(options ...ContainerOption) *Progress {
5656 pq := make(priorityQueue, 0)
5757 heap.Init(&pq)
58
5859 s := &pState{
5960 ctx: context.Background(),
6061 bHeap: &pq,
6162 width: pwidth,
62 cw: cwriter.New(os.Stdout),
6363 rr: prr,
6464 waitBars: make(map[*Bar]*Bar),
6565 debugOut: ioutil.Discard,
6666 forceRefreshCh: make(chan time.Time),
67 output: os.Stdout,
6768 }
6869
6970 for _, opt := range options {
8081 done: make(chan struct{}),
8182 }
8283 p.cwg.Add(1)
83 go p.serve(s)
84 go p.serve(s, cwriter.New(s.output))
8485 return p
8586 }
8687
178179 p.cwg.Wait()
179180 }
180181
181 func (p *Progress) serve(s *pState) {
182 func (p *Progress) serve(s *pState, cw *cwriter.Writer) {
182183 defer p.cwg.Done()
183184
184185 manualOrTickCh, cleanUp := s.manualOrTick()
197198 }
198199 return
199200 }
200 tw, err := s.cw.GetWidth()
201 if err != nil {
202 tw = s.width
203 }
204 s.render(p.done, tw)
205 }
206 }
207 }
208
209 func (s *pState) render(done <-chan struct{}, tw int) {
201 if err := s.render(p.done, cw); err != nil {
202 fmt.Fprintf(s.debugOut, "[mpb] %s %v\n", time.Now(), err)
203 }
204 }
205 }
206 }
207
208 func (s *pState) render(done <-chan struct{}, cw *cwriter.Writer) error {
210209 if s.heapUpdated {
211210 s.updateSyncMatrix()
212211 s.heapUpdated = false
214213 syncWidth(s.pMatrix)
215214 syncWidth(s.aMatrix)
216215
216 tw, err := cw.GetWidth()
217 if err != nil {
218 tw = s.width
219 }
217220 for i := 0; i < s.bHeap.Len(); i++ {
218221 bar := (*s.bHeap)[i]
219222 go bar.render(s.debugOut, tw)
220223 }
221224
222 if err := s.flush(done, s.bHeap.Len()); err != nil {
223 fmt.Fprintf(s.debugOut, "%s %s %v\n", "[mpb]", time.Now(), err)
224 }
225 }
226
227 func (s *pState) flush(done <-chan struct{}, lineCount int) error {
225 return s.flush(done, cw)
226 }
227
228 func (s *pState) flush(done <-chan struct{}, cw *cwriter.Writer) error {
229 var lineCount int
228230 for s.bHeap.Len() > 0 {
229231 bar := heap.Pop(s.bHeap).(*Bar)
230232 frameReader := <-bar.frameReaderCh
254256 }
255257 heap.Push(s.bHeap, bar)
256258 }()
257 s.cw.ReadFrom(frameReader)
258 lineCount += frameReader.extendedLines
259 cw.ReadFrom(frameReader)
260 lineCount += frameReader.extendedLines + 1
259261 }
260262
261263 for i := len(s.shutdownPending) - 1; i >= 0; i-- {
263265 s.shutdownPending = s.shutdownPending[:i]
264266 }
265267
266 return s.cw.Flush(lineCount)
268 return cw.Flush(lineCount)
267269 }
268270
269271 func (s *pState) manualOrTick() (<-chan time.Time, func()) {