| 1 |
package uiprogress |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"os" |
| 7 |
"sync" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/gosuri/uilive" |
| 11 |
) |
| 12 |
|
| 13 |
// Out is the default writer to render progress bars to |
| 14 |
var Out = os.Stdout |
| 15 |
|
| 16 |
// RefreshInterval in the default time duration to wait for refreshing the output |
| 17 |
var RefreshInterval = time.Millisecond * 10 |
| 18 |
|
| 19 |
// defaultProgress is the default progress |
| 20 |
var defaultProgress = New() |
| 21 |
|
| 22 |
// Progress represents the container that renders progress bars |
| 23 |
type Progress struct { |
| 24 |
// Out is the writer to render progress bars to |
| 25 |
Out io.Writer |
| 26 |
|
| 27 |
// Width is the width of the progress bars |
| 28 |
Width int |
| 29 |
|
| 30 |
// Bars is the collection of progress bars |
| 31 |
Bars []*Bar |
| 32 |
|
| 33 |
// RefreshInterval in the time duration to wait for refreshing the output |
| 34 |
RefreshInterval time.Duration |
| 35 |
|
| 36 |
lw *uilive.Writer |
| 37 |
ticker *time.Ticker |
| 38 |
tdone chan bool |
| 39 |
mtx *sync.RWMutex |
| 40 |
} |
| 41 |
|
| 42 |
// New returns a new progress bar with defaults |
| 43 |
func New() *Progress { |
| 44 |
lw := uilive.New() |
| 45 |
lw.Out = Out |
| 46 |
|
| 47 |
return &Progress{ |
| 48 |
Width: Width, |
| 49 |
Out: Out, |
| 50 |
Bars: make([]*Bar, 0), |
| 51 |
RefreshInterval: RefreshInterval, |
| 52 |
|
| 53 |
tdone: make(chan bool), |
| 54 |
lw: uilive.New(), |
| 55 |
mtx: &sync.RWMutex{}, |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// AddBar creates a new progress bar and adds it to the default progress container |
| 60 |
func AddBar(total int) *Bar { |
| 61 |
return defaultProgress.AddBar(total) |
| 62 |
} |
| 63 |
|
| 64 |
// Start starts the rendering the progress of progress bars using the DefaultProgress. It listens for updates using `bar.Set(n)` and new bars when added using `AddBar` |
| 65 |
func Start() { |
| 66 |
defaultProgress.Start() |
| 67 |
} |
| 68 |
|
| 69 |
// Stop stops listening |
| 70 |
func Stop() { |
| 71 |
defaultProgress.Stop() |
| 72 |
} |
| 73 |
|
| 74 |
// Listen listens for updates and renders the progress bars |
| 75 |
func Listen() { |
| 76 |
defaultProgress.Listen() |
| 77 |
} |
| 78 |
|
| 79 |
func (p *Progress) SetOut(o io.Writer) { |
| 80 |
p.mtx.Lock() |
| 81 |
defer p.mtx.Unlock() |
| 82 |
|
| 83 |
p.Out = o |
| 84 |
p.lw.Out = o |
| 85 |
} |
| 86 |
|
| 87 |
func (p *Progress) SetRefreshInterval(interval time.Duration) { |
| 88 |
p.mtx.Lock() |
| 89 |
defer p.mtx.Unlock() |
| 90 |
p.RefreshInterval = interval |
| 91 |
} |
| 92 |
|
| 93 |
// AddBar creates a new progress bar and adds to the container |
| 94 |
func (p *Progress) AddBar(total int) *Bar { |
| 95 |
p.mtx.Lock() |
| 96 |
defer p.mtx.Unlock() |
| 97 |
|
| 98 |
bar := NewBar(total) |
| 99 |
bar.Width = p.Width |
| 100 |
p.Bars = append(p.Bars, bar) |
| 101 |
return bar |
| 102 |
} |
| 103 |
|
| 104 |
// Listen listens for updates and renders the progress bars |
| 105 |
func (p *Progress) Listen() { |
| 106 |
for { |
| 107 |
|
| 108 |
p.mtx.Lock() |
| 109 |
interval := p.RefreshInterval |
| 110 |
p.mtx.Unlock() |
| 111 |
|
| 112 |
select { |
| 113 |
case <-time.After(interval): |
| 114 |
p.print() |
| 115 |
case <-p.tdone: |
| 116 |
p.print() |
| 117 |
close(p.tdone) |
| 118 |
return |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
func (p *Progress) print() { |
| 124 |
p.mtx.Lock() |
| 125 |
defer p.mtx.Unlock() |
| 126 |
for _, bar := range p.Bars { |
| 127 |
fmt.Fprintln(p.lw, bar.String()) |
| 128 |
} |
| 129 |
p.lw.Flush() |
| 130 |
} |
| 131 |
|
| 132 |
// Start starts the rendering the progress of progress bars. It listens for updates using `bar.Set(n)` and new bars when added using `AddBar` |
| 133 |
func (p *Progress) Start() { |
| 134 |
go p.Listen() |
| 135 |
} |
| 136 |
|
| 137 |
// Stop stops listening |
| 138 |
func (p *Progress) Stop() { |
| 139 |
p.tdone <- true |
| 140 |
<-p.tdone |
| 141 |
} |
| 142 |
|
| 143 |
// Bypass returns a writer which allows non-buffered data to be written to the underlying output |
| 144 |
func (p *Progress) Bypass() io.Writer { |
| 145 |
return p.lw.Bypass() |
| 146 |
} |