use Any func
Vladimir Bauer
6 years ago
| 58 | 58 |
}
|
| 59 | 59 |
|
| 60 | 60 |
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))
|
| 64 | 64 |
}
|
| 65 | |
return d
|
|
65 |
return decor.Any(f, wc)
|
| 66 | 66 |
}
|
| 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 | |
}
|
| 34 | 34 |
}
|
| 35 | 35 |
|
| 36 | 36 |
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 |
})
|
| 43 | 43 |
}
|
| 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 | |
}
|
| 23 | 23 |
decor.Name("my bar:"),
|
| 24 | 24 |
),
|
| 25 | 25 |
mpb.AppendDecorators(
|
| 26 | |
newCustomPercentage(decor.Percentage(), nextCh),
|
|
26 |
newCustomPercentage(nextCh),
|
| 27 | 27 |
),
|
| 28 | 28 |
)
|
| 29 | 29 |
ew := &errorWrapper{}
|
|
| 115 | 115 |
return cf, nextCh
|
| 116 | 116 |
}
|
| 117 | 117 |
|
| 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)
|
| 121 | 129 |
}
|
| 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 | |
}
|