SetOut
Vladimir Bauer
9 years ago
| 9 | 9 | ) |
| 10 | 10 | |
| 11 | 11 | // Out is the default writer to render progress bars to |
| 12 | var Out = os.Stdout | |
| 12 | // var Out = os.Stdout | |
| 13 | 13 | |
| 14 | 14 | // RefreshInterval in the default time duration to wait for refreshing the output |
| 15 | 15 | var RefreshInterval = time.Millisecond * 10 |
| 17 | 17 | // Progress represents the container that renders progress bars |
| 18 | 18 | type Progress struct { |
| 19 | 19 | // Out is the writer to render progress bars to |
| 20 | Out io.Writer | |
| 20 | out io.Writer | |
| 21 | 21 | |
| 22 | 22 | // Width is the width of the progress bars |
| 23 | 23 | // Width int |
| 38 | 38 | // New returns a new progress bar with defaults |
| 39 | 39 | func New() *Progress { |
| 40 | 40 | p := &Progress{ |
| 41 | Out: Out, | |
| 41 | out: os.Stdout, | |
| 42 | 42 | lw: uilive.New(), |
| 43 | 43 | bars: make(chan *Bar), |
| 44 | 44 | ticker: time.NewTicker(RefreshInterval), |
| 45 | 45 | } |
| 46 | 46 | go p.server() |
| 47 | return p | |
| 48 | } | |
| 49 | ||
| 50 | // SetOut is the writer to render progress bars to | |
| 51 | func (p *Progress) SetOut(w io.Writer) *Progress { | |
| 52 | p.out = w | |
| 47 | 53 | return p |
| 48 | 54 | } |
| 49 | 55 | |
| 58 | 64 | // Listen listens for updates and renders the progress bars |
| 59 | 65 | func (p *Progress) server() { |
| 60 | 66 | bars := make([]*Bar, 0) |
| 61 | p.lw.Out = p.Out | |
| 67 | p.lw.Out = p.out | |
| 62 | 68 | loop: |
| 63 | 69 | for { |
| 64 | 70 | select { |