Codebase list golang-github-vbauerster-mpb / 0732bab
Refactoring... Vladimir Bauer 8 years ago
3 changed file(s) with 18 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
6262 // New creates new Progress instance, which orchestrates bars rendering process.
6363 // Accepts mpb.ProgressOption funcs for customization.
6464 func New(options ...ProgressOption) *Progress {
65 // defaults
66 conf := pState{
65 s := &pState{
6766 bars: make([]*Bar, 0, 3),
6867 width: pwidth,
6968 format: pformat,
7473 }
7574
7675 for _, opt := range options {
77 opt(&conf)
76 opt(s)
7877 }
7978
8079 p := &Progress{
81 ewg: conf.ewg,
80 ewg: s.ewg,
8281 wg: new(sync.WaitGroup),
8382 done: make(chan struct{}),
8483 ops: make(chan func(*pState)),
8584 quit: make(chan struct{}),
8685 }
87 go p.server(conf)
86 go p.server(s)
8887 return p
8988 }
9089
9392 p.wg.Add(1)
9493 result := make(chan *Bar, 1)
9594 select {
96 case p.ops <- func(c *pState) {
97 options = append(options, barWidth(c.width), barFormat(c.format))
98 b := newBar(c.idCounter, total, p.wg, c.cancel, options...)
99 c.bars = append(c.bars, b)
100 c.idCounter++
95 case p.ops <- func(s *pState) {
96 options = append(options, barWidth(s.width), barFormat(s.format))
97 b := newBar(s.idCounter, total, p.wg, s.cancel, options...)
98 s.bars = append(s.bars, b)
99 s.idCounter++
101100 result <- b
102101 }:
103102 return <-result
110109 func (p *Progress) RemoveBar(b *Bar) bool {
111110 result := make(chan bool, 1)
112111 select {
113 case p.ops <- func(c *pState) {
112 case p.ops <- func(s *pState) {
114113 var ok bool
115 for i, bar := range c.bars {
114 for i, bar := range s.bars {
116115 if bar == b {
117116 bar.Complete()
118 c.bars = append(c.bars[:i], c.bars[i+1:]...)
117 s.bars = append(s.bars[:i], s.bars[i+1:]...)
119118 ok = true
120119 break
121120 }
132131 func (p *Progress) BarCount() int {
133132 result := make(chan int, 1)
134133 select {
135 case p.ops <- func(c *pState) {
136 result <- len(c.bars)
134 case p.ops <- func(s *pState) {
135 result <- len(s.bars)
137136 }:
138137 return <-result
139138 case <-p.quit:
1212 "github.com/vbauerster/mpb/cwriter"
1313 )
1414
15 func (p *Progress) server(s pState) {
15 func (p *Progress) server(s *pState) {
1616 winch := make(chan os.Signal, 1)
1717 signal.Notify(winch, syscall.SIGWINCH)
1818
3333 for {
3434 select {
3535 case op := <-p.ops:
36 op(&s)
36 op(s)
3737 case <-s.ticker.C:
3838 if len(s.bars) == 0 {
3939 runtime.Gosched()
99 "github.com/vbauerster/mpb/cwriter"
1010 )
1111
12 func (p *Progress) server(s pState) {
12 func (p *Progress) server(s *pState) {
1313 defer func() {
1414 if s.shutdownNotifier != nil {
1515 close(s.shutdownNotifier)
2222 for {
2323 select {
2424 case op := <-p.ops:
25 op(&s)
25 op(s)
2626 case <-s.ticker.C:
2727 if len(s.bars) == 0 {
2828 runtime.Gosched()