p.SetOut nil check
Vladimir Bauer
9 years ago
| 2 | 2 |
import (
|
| 3 | 3 |
"fmt"
|
| 4 | 4 |
"math/rand"
|
| 5 | |
"runtime"
|
| 6 | 5 |
"time"
|
| 7 | 6 |
|
| 8 | 7 |
"github.com/vbauerster/uiprogress"
|
| 9 | 8 |
)
|
| 10 | 9 |
|
| 11 | 10 |
const (
|
| 12 | |
maxBlockSize = 14
|
|
11 |
maxBlockSize = 12
|
| 13 | 12 |
)
|
| 14 | 13 |
|
| 15 | 14 |
func main() {
|
| 16 | |
runtime.GOMAXPROCS(runtime.NumCPU())
|
| 17 | |
|
| 18 | 15 |
decor := func(s *uiprogress.Statistics) string {
|
| 19 | 16 |
str := fmt.Sprintf("%d/%d", s.Completed, s.Total)
|
| 20 | 17 |
return fmt.Sprintf("%-7s", str)
|
| 21 | 18 |
}
|
| 22 | 19 |
|
| 23 | |
p := uiprogress.New()
|
|
20 |
p := uiprogress.New().RefreshRate(80 * time.Millisecond)
|
| 24 | 21 |
|
| 25 | 22 |
bar1 := p.AddBar(50).AppendETA().PrependFunc(decor)
|
| 26 | 23 |
go func() {
|
| 54 | 54 |
return p
|
| 55 | 55 |
}
|
| 56 | 56 |
|
|
57 |
// SetOut sets underlying writer of progress
|
|
58 |
// default is os.Stdout
|
|
59 |
func (p *progress) SetOut(w io.Writer) *progress {
|
|
60 |
if w == nil {
|
|
61 |
return p
|
|
62 |
}
|
|
63 |
p.out = w
|
|
64 |
return p
|
|
65 |
}
|
|
66 |
|
| 57 | 67 |
// AddBar creates a new progress bar and adds to the container
|
| 58 | 68 |
func (p *progress) AddBar(total int) *Bar {
|
| 59 | 69 |
p.wg.Add(1)
|
|
| 74 | 84 |
p.interval <- d
|
| 75 | 85 |
return p
|
| 76 | 86 |
}
|
| 77 | |
|
| 78 | |
// SetOut sets underlying writer of progress
|
| 79 | |
// default is os.Stdout
|
| 80 | |
func (p *progress) SetOut(w io.Writer) *progress {
|
| 81 | |
p.out = w
|
| 82 | |
return p
|
| 83 | |
}
|
| 84 | |
|
| 85 | |
// Bypass returns a writer which allows non-buffered data to be written to the underlying output
|
| 86 | |
// func (p *progress) Bypass() io.Writer {
|
| 87 | |
// return p.lw.Bypass()
|
| 88 | |
// }
|
| 89 | 87 |
|
| 90 | 88 |
// WaitAndStop stops listening
|
| 91 | 89 |
func (p *progress) WaitAndStop() {
|