New ProgressOption, OutputInterceptors. See issue #8
Vladimir Bauer
8 years ago
| 1 | 1 |
|
| 2 | 2 |
import (
|
| 3 | 3 |
"io"
|
|
4 |
"io/ioutil"
|
| 4 | 5 |
"time"
|
| 5 | 6 |
"unicode/utf8"
|
| 6 | 7 |
|
|
| 64 | 65 |
// Output overrides default output os.Stdout
|
| 65 | 66 |
func Output(w io.Writer) ProgressOption {
|
| 66 | 67 |
return func(c *pConf) {
|
| 67 | |
if w != nil {
|
| 68 | |
c.cw = cwriter.New(w)
|
|
68 |
if w == nil {
|
|
69 |
w = ioutil.Discard
|
| 69 | 70 |
}
|
|
71 |
c.cw = cwriter.New(w)
|
| 70 | 72 |
}
|
| 71 | 73 |
}
|
|
74 |
|
|
75 |
// OutputInterceptors provides a way to write to the underlying progress pool's
|
|
76 |
// writer. Could be useful if you want to output something below the bars, while
|
|
77 |
// they're rendering.
|
|
78 |
func OutputInterceptors(interseptors ...func(io.Writer)) ProgressOption {
|
|
79 |
return func(c *pConf) {
|
|
80 |
c.interceptors = interseptors
|
|
81 |
}
|
|
82 |
}
|
| 0 | 0 |
package mpb
|
| 1 | 1 |
|
| 2 | 2 |
import (
|
|
3 |
"io"
|
| 3 | 4 |
"os"
|
| 4 | 5 |
"sync"
|
| 5 | 6 |
"time"
|
|
| 26 | 27 |
cw *cwriter.Writer
|
| 27 | 28 |
ticker *time.Ticker
|
| 28 | 29 |
beforeRender BeforeRender
|
|
30 |
interceptors []func(io.Writer)
|
| 29 | 31 |
|
| 30 | 32 |
shutdownNotifier chan struct{}
|
| 31 | 33 |
cancel <-chan struct{}
|
|
| 212 | 214 |
|
| 213 | 215 |
for buf := range ch {
|
| 214 | 216 |
conf.cw.Write(buf)
|
|
217 |
}
|
|
218 |
|
|
219 |
for _, interceptor := range conf.interceptors {
|
|
220 |
interceptor(conf.cw)
|
| 215 | 221 |
}
|
| 216 | 222 |
|
| 217 | 223 |
conf.cw.Flush()
|