diff --git a/progress.go b/progress.go index 6bc7482..9cf8dc3 100644 --- a/progress.go +++ b/progress.go @@ -20,8 +20,8 @@ result []chan int } - // config changeable by user - userConf struct { + // progress config, all fieals are adjustable by user + pConf struct { bars []*Bar width int @@ -53,11 +53,11 @@ wg *sync.WaitGroup done chan struct{} - ops chan func(*userConf) + ops chan func(*pConf) stopReqCh chan struct{} // following is used after (*Progress.done) is closed - conf userConf + conf pConf } // New creates new Progress instance, which will orchestrate bars rendering @@ -67,10 +67,10 @@ p := &Progress{ wg: new(sync.WaitGroup), done: make(chan struct{}), - ops: make(chan func(*userConf)), + ops: make(chan func(*pConf)), stopReqCh: make(chan struct{}), } - go p.server(userConf{ + go p.server(pConf{ bars: make([]*Bar, 0, 3), width: pwidth, format: pformat, @@ -87,7 +87,7 @@ if ch == nil { panic("nil cancel channel") } - return updateConf(p, func(c *userConf) { + return updateConf(p, func(c *pConf) { c.cancel = ch }) } @@ -97,7 +97,7 @@ if width < 2 { return p } - return updateConf(p, func(c *userConf) { + return updateConf(p, func(c *pConf) { c.width = width }) } @@ -107,7 +107,7 @@ if w == nil { return p } - return updateConf(p, func(c *userConf) { + return updateConf(p, func(c *pConf) { c.cw.Flush() c.cw = cwriter.New(w) }) @@ -115,7 +115,7 @@ // RefreshRate overrides default (100ms) refresh rate value func (p *Progress) RefreshRate(d time.Duration) *Progress { - return updateConf(p, func(c *userConf) { + return updateConf(p, func(c *pConf) { c.ticker.Stop() c.ticker = time.NewTicker(d) c.rr = d @@ -124,7 +124,7 @@ // BeforeRenderFunc accepts a func, which gets called before render process. func (p *Progress) BeforeRenderFunc(f BeforeRender) *Progress { - return updateConf(p, func(c *userConf) { + return updateConf(p, func(c *pConf) { c.beforeRender = f }) } @@ -137,7 +137,7 @@ // AddBarWithID creates a new progress bar and adds to the container. func (p *Progress) AddBarWithID(id int, total int64) *Bar { result := make(chan *Bar, 1) - op := func(c *userConf) { + op := func(c *pConf) { bar := newBar(id, total, c.width, c.format, p.wg, c.cancel) c.bars = append(c.bars, bar) p.wg.Add(1) @@ -154,7 +154,7 @@ // RemoveBar removes bar at any time. func (p *Progress) RemoveBar(b *Bar) bool { result := make(chan bool, 1) - op := func(c *userConf) { + op := func(c *pConf) { var ok bool for i, bar := range c.bars { if bar == b { @@ -174,10 +174,10 @@ } } -// BarCount returns bars count in the container. +// BarCount returns bars count func (p *Progress) BarCount() int { result := make(chan int, 1) - op := func(c *userConf) { + op := func(c *pConf) { result <- len(c.bars) } select { @@ -190,7 +190,7 @@ // ShutdownNotify means to be notified when main rendering goroutine quits, usualy after p.Stop() call. func (p *Progress) ShutdownNotify(ch chan struct{}) *Progress { - return updateConf(p, func(c *userConf) { + return updateConf(p, func(c *pConf) { c.shutdownNotifier = ch }) } @@ -200,7 +200,7 @@ if utf8.RuneCountInString(format) != numFmtRunes { return p } - return updateConf(p, func(c *userConf) { + return updateConf(p, func(c *pConf) { c.format = format }) } @@ -215,7 +215,7 @@ return default: // complete Total unknown bars - p.ops <- func(c *userConf) { + p.ops <- func(c *pConf) { for _, b := range c.bars { s := b.getState() if !s.completed && !s.aborted { @@ -232,36 +232,8 @@ } } -// func (p *Progress) getConf() userConf { -// select { -// case conf := <-p.userConfCh: -// return conf -// case <-p.done: -// return p.conf -// } -// } - -// func (p *Progress) updateConf(op func(*userConf)) { -// // c := p.getConf() -// // cb(&c) -// select { -// case p.ops <- op: -// case <-p.done: -// return -// } -// } - -func updateConf(p *Progress, op func(*userConf)) *Progress { - select { - case p.ops <- op: - return p - case <-p.done: - return nil - } -} - // server monitors underlying channels and renders any progress bars -func (p *Progress) server(conf userConf) { +func (p *Progress) server(conf pConf) { defer func() { conf.ticker.Stop() @@ -386,6 +358,15 @@ return ch } +func updateConf(p *Progress, op func(*pConf)) *Progress { + select { + case p.ops <- op: + return p + case <-p.done: + return nil + } +} + func max(slice []int) int { max := slice[0] diff --git a/progress_go1.7.go b/progress_go1.7.go index 4781fcc..8e3556e 100644 --- a/progress_go1.7.go +++ b/progress_go1.7.go @@ -10,7 +10,7 @@ if ctx == nil { panic("nil context") } - return updateConf(p, func(c *userConf) { + return updateConf(p, func(c *pConf) { c.cancel = ctx.Done() }) }