Codebase list golang-github-vbauerster-mpb / ddf99f1
Progress options Vladimir Bauer 9 years ago
6 changed file(s) with 107 addition(s) and 38 deletion(s). Raw diff Collapse all Expand all
0 package mpb
1
2 import (
3 "io"
4 "time"
5 "unicode/utf8"
6
7 "github.com/vbauerster/mpb/cwriter"
8 )
9
10 type ProgressOption func(*pConf)
11
12 // WithWidth overrides default width 80
13 func WithWidth(w int) ProgressOption {
14 return func(c *pConf) {
15 if w > 2 {
16 c.width = w
17 }
18 }
19 }
20
21 // WithFormat overrides default bar format "[=>-]"
22 func WithFormat(format string) ProgressOption {
23 return func(c *pConf) {
24 if utf8.RuneCountInString(format) == numFmtRunes {
25 c.format = format
26 }
27 }
28 }
29
30 // WithRefreshRate overrides default 100ms refresh rate
31 func WithRefreshRate(d time.Duration) ProgressOption {
32 return func(c *pConf) {
33 c.ticker.Stop()
34 c.ticker = time.NewTicker(d)
35 c.rr = d
36 }
37 }
38
39 // WithBeforeRenderFunc provided BeforeRender func,
40 // will be called before each render cycle.
41 func WithBeforeRenderFunc(f BeforeRender) ProgressOption {
42 return func(c *pConf) {
43 c.beforeRender = f
44 }
45 }
46
47 // WithCancel provide your cancel channel,
48 // which you plan to close at some point.
49 func WithCancel(ch <-chan struct{}) ProgressOption {
50 return func(c *pConf) {
51 c.cancel = ch
52 }
53 }
54
55 // WithShutdownNotifier provided chanel will be closed, inside p.Stop() call
56 func WithShutdownNotifier(ch chan struct{}) ProgressOption {
57 return func(c *pConf) {
58 c.shutdownNotifier = ch
59 }
60 }
61
62 // Output overrides default output os.Stdout
63 func Output(w io.Writer) ProgressOption {
64 return func(c *pConf) {
65 if w != nil {
66 c.cw = cwriter.New(w)
67 }
68 }
69 }
0 package mpb
1
2 import "context"
3
4 func WithContext(ctx context.Context) ProgressOption {
5 return func(c *pConf) {
6 if ctx != nil {
7 c.cancel = ctx.Done()
8 }
9 }
10 }
5959 conf pConf
6060 }
6161
62 // New creates new Progress instance, which will orchestrate bars rendering
63 // process. It acceepts context.Context, for cancellation.
64 // If you don't plan to cancel, it is safe to feed with nil
65 func New() *Progress {
66 p := &Progress{
67 wg: new(sync.WaitGroup),
68 done: make(chan struct{}),
69 ops: make(chan func(*pConf)),
70 stopReqCh: make(chan struct{}),
71 }
72 go p.server(pConf{
62 // New creates new Progress instance, which orchestrates bars rendering process.
63 // Accepts mpb.ProgressOption funcs for customization.
64 func New(options ...ProgressOption) *Progress {
65 // defaults
66 conf := pConf{
7367 bars: make([]*Bar, 0, 3),
7468 width: pwidth,
7569 format: pformat,
7670 cw: cwriter.New(os.Stdout),
7771 rr: prr,
7872 ticker: time.NewTicker(prr),
79 })
73 }
74
75 for _, opt := range options {
76 opt(&conf)
77 }
78
79 p := &Progress{
80 wg: new(sync.WaitGroup),
81 done: make(chan struct{}),
82 ops: make(chan func(*pConf)),
83 stopReqCh: make(chan struct{}),
84 }
85 go p.server(conf)
8086 return p
8187 }
8288
83 // WithCancel cancellation via channel.
84 // You have to call p.Stop() anyway, after cancel.
85 // Pancis, if nil channel is passed.
89 // WithCancel Deprecated, use mpb.WithCancel
8690 func (p *Progress) WithCancel(ch <-chan struct{}) *Progress {
8791 if ch == nil {
8892 panic("nil cancel channel")
9296 })
9397 }
9498
95 // SetWidth overrides default (80) width of bar(s).
99 // SetWidth Deprecated, use mpb.WithWidth
96100 func (p *Progress) SetWidth(width int) *Progress {
97101 if width < 2 {
98102 return p
102106 })
103107 }
104108
105 // SetOut sets underlying writer of progress. Default one is os.Stdout.
109 // SetOut Deprecated, use mpb.Output
106110 func (p *Progress) SetOut(w io.Writer) *Progress {
107111 if w == nil {
108112 return p
113117 })
114118 }
115119
116 // RefreshRate overrides default (100ms) refresh rate value
120 // RefreshRate Deprecated, use mpb.WithRefreshRate
117121 func (p *Progress) RefreshRate(d time.Duration) *Progress {
118122 return updateConf(p, func(c *pConf) {
119123 c.ticker.Stop()
120124 c.ticker = time.NewTicker(d)
121125 c.rr = d
122 })
123 }
124
125 // BeforeRenderFunc accepts a func, which gets called before render process.
126 func (p *Progress) BeforeRenderFunc(f BeforeRender) *Progress {
127 return updateConf(p, func(c *pConf) {
128 c.beforeRender = f
129126 })
130127 }
131128
188185 }
189186 }
190187
191 // ShutdownNotify means to be notified when main rendering goroutine quits, usualy after p.Stop() call.
192 func (p *Progress) ShutdownNotify(ch chan struct{}) *Progress {
193 return updateConf(p, func(c *pConf) {
194 c.shutdownNotifier = ch
195 })
196 }
197
198 // Format sets custom format for underlying bar(s), default one is "[=>-]".
188 // Format Deprecated, use mpb.WithFormat
199189 func (p *Progress) Format(format string) *Progress {
200190 if utf8.RuneCountInString(format) != numFmtRunes {
201191 return p
33
44 import "context"
55
6 // WithContext cancellation via context.
7 // You have to call p.Stop() anyway, after cancel.
8 // Pancis, if nil context is passed
6 // WithContext Deprecated, use mpb.WithContext
97 func (p *Progress) WithContext(ctx context.Context) *Progress {
108 if ctx == nil {
119 panic("nil context")
1515 func TestWithContext(t *testing.T) {
1616 ctx, cancel := context.WithCancel(context.Background())
1717 shutdown := make(chan struct{})
18 p := mpb.New().WithContext(ctx).ShutdownNotify(shutdown)
18 p := mpb.New(mpb.WithContext(ctx), mpb.WithShutdownNotifier(shutdown))
1919
2020 var wg sync.WaitGroup
2121 total := 100
111111 func TestWithCancel(t *testing.T) {
112112 cancel := make(chan struct{})
113113 shutdown := make(chan struct{})
114 p := mpb.New().WithCancel(cancel).ShutdownNotify(shutdown)
114 p := mpb.New(mpb.WithCancel(cancel), mpb.WithShutdownNotifier(shutdown))
115115
116116 var wg sync.WaitGroup
117117 total := 100