Codebase list golang-github-vbauerster-mpb / e94dbf9
progress' output is changable any time Vladimir Bauer 9 years ago
1 changed file(s) with 16 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
3434 sort SortType
3535 stopped bool
3636
37 op chan *operation
38 rrChangeReqCh chan time.Duration
37 op chan *operation
38 rrChangeReqCh chan time.Duration
39 outChangeReqCh chan io.Writer
3940
4041 wg *sync.WaitGroup
4142 }
4950 // New returns a new progress bar with defaults
5051 func New() *Progress {
5152 p := &Progress{
52 out: os.Stdout,
53 width: 70,
54 op: make(chan *operation),
55 rrChangeReqCh: make(chan time.Duration),
56 wg: new(sync.WaitGroup),
53 width: 70,
54 op: make(chan *operation),
55 rrChangeReqCh: make(chan time.Duration),
56 outChangeReqCh: make(chan io.Writer),
57 wg: new(sync.WaitGroup),
5758 }
58 go p.server()
59 go p.server(cwriter.New(os.Stdout))
5960 return p
6061 }
6162
7374 if w == nil {
7475 return p
7576 }
76 p.out = w
77 p.outChangeReqCh <- w
7778 return p
7879 }
7980
113114 }
114115
115116 // server monitors underlying channels and renders any progress bars
116 func (p *Progress) server() {
117 func (p *Progress) server(cw *cwriter.Writer) {
117118 t := time.NewTicker(refreshRate * time.Millisecond)
118119 bars := make([]*Bar, 0, 4)
119 lw := cwriter.New(p.out)
120120 for {
121121 select {
122 case w := <-p.outChangeReqCh:
123 cw.Flush()
124 cw = cwriter.New(w)
122125 case op, ok := <-p.op:
123126 if !ok {
124127 // fmt.Fprintln(os.Stderr, "Sopping bars")
152155 }
153156 for _, b := range bars {
154157 // cannot parallel this, because order matters
155 fmt.Fprintln(lw, b)
158 fmt.Fprintln(cw, b)
156159 }
157 lw.Flush()
160 cw.Flush()
158161 for _, b := range bars {
159162 go func(b *Bar) {
160163 b.flushedCh <- struct{}{}