Skip to content

Commit

Permalink
Fix for issue #8. Stopping Listen by closing a channel
Browse files Browse the repository at this point in the history
  • Loading branch information
HeinOldewage committed Dec 10, 2015
1 parent ab3c029 commit 53afa9e
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,23 @@ func (p *Progress) AddBar(total int) *Bar {
// Listen listens for updates and renders the progress bars
func (p *Progress) Listen() {
p.lw.Out = p.Out
go func() {
for {
time.Sleep(p.RefreshInterval)
for _, bar := range p.Bars {
fmt.Fprintln(p.lw, bar.String())
for {
select {
case <-p.stopChan:
{
return
}
default:
{
time.Sleep(p.RefreshInterval)
for _, bar := range p.Bars {
fmt.Fprintln(p.lw, bar.String())
}
p.lw.Flush()
}
p.lw.Flush()
}
}()
<-p.stopChan
time.Sleep(p.RefreshInterval)
p.lw.Flush()

}
}

// Start starts the rendering the progress of progress bars. It listens for updates using `bar.Set(n)` and new bars when added using `AddBar`
Expand All @@ -102,5 +107,5 @@ func (p *Progress) Start() {

// Stop stops listening
func (p *Progress) Stop() {
p.stopChan <- struct{}{}
close(p.stopChan)
}

0 comments on commit 53afa9e

Please sign in to comment.