Codebase list golang-github-vbauerster-mpb / b8fed30
use Any func Vladimir Bauer 6 years ago
3 changed file(s) with 22 addition(s) and 54 deletion(s). Raw diff Collapse all Expand all
5858 }
5959
6060 func newVariadicSpinner(wc decor.WC) decor.Decorator {
61 d := &variadicSpinner{
62 WC: wc.Init(),
63 base: decor.Spinner(nil),
61 spinner := decor.Spinner(nil)
62 f := func(s *decor.Statistics) string {
63 return strings.Repeat(spinner.Decor(s), int(s.Current/3))
6464 }
65 return d
65 return decor.Any(f, wc)
6666 }
67
68 type variadicSpinner struct {
69 decor.WC
70 base decor.Decorator
71 }
72
73 func (d *variadicSpinner) Decor(st *decor.Statistics) string {
74 msg := d.base.Decor(st)
75 msg = strings.Repeat(msg, int(st.Current/3))
76 return d.FormatMsg(msg)
77 }
3434 }
3535
3636 func panicDecorator(name, panicMsg string) decor.Decorator {
37 d := &decorator{
38 msg: name,
39 panicMsg: panicMsg,
40 }
41 d.Init()
42 return d
37 return decor.Any(func(s *decor.Statistics) string {
38 if s.ID == 1 && s.Current >= 42 {
39 panic(panicMsg)
40 }
41 return name
42 })
4343 }
44
45 type decorator struct {
46 decor.WC
47 msg string
48 panicMsg string
49 }
50
51 func (d *decorator) Decor(st *decor.Statistics) string {
52 if st.ID == 1 && st.Current >= 42 {
53 panic(d.panicMsg)
54 }
55 return d.FormatMsg(d.msg)
56 }
2323 decor.Name("my bar:"),
2424 ),
2525 mpb.AppendDecorators(
26 newCustomPercentage(decor.Percentage(), nextCh),
26 newCustomPercentage(nextCh),
2727 ),
2828 )
2929 ew := &errorWrapper{}
115115 return cf, nextCh
116116 }
117117
118 type myPercentageDecorator struct {
119 decor.Decorator
120 ch <-chan struct{}
118 func newCustomPercentage(ch <-chan struct{}) decor.Decorator {
119 base := decor.Percentage()
120 f := func(s *decor.Statistics) string {
121 select {
122 case <-ch:
123 return ""
124 default:
125 return base.Decor(s)
126 }
127 }
128 return decor.Any(f)
121129 }
122
123 func (d *myPercentageDecorator) Decor(st *decor.Statistics) string {
124 select {
125 case <-d.ch:
126 return ""
127 default:
128 return d.Decorator.Decor(st)
129 }
130 }
131
132 func newCustomPercentage(base decor.Decorator, ch <-chan struct{}) decor.Decorator {
133 return &myPercentageDecorator{
134 Decorator: base,
135 ch: ch,
136 }
137 }