diff --git a/_examples/merge/main.go b/_examples/merge/main.go index cde6c42..509f1bc 100644 --- a/_examples/merge/main.go +++ b/_examples/merge/main.go @@ -59,20 +59,9 @@ } func newVariadicSpinner(wc decor.WC) decor.Decorator { - d := &variadicSpinner{ - WC: wc.Init(), - base: decor.Spinner(nil), + spinner := decor.Spinner(nil) + f := func(s *decor.Statistics) string { + return strings.Repeat(spinner.Decor(s), int(s.Current/3)) } - return d + return decor.Any(f, wc) } - -type variadicSpinner struct { - decor.WC - base decor.Decorator -} - -func (d *variadicSpinner) Decor(st *decor.Statistics) string { - msg := d.base.Decor(st) - msg = strings.Repeat(msg, int(st.Current/3)) - return d.FormatMsg(msg) -} diff --git a/_examples/panic/main.go b/_examples/panic/main.go index 2a1fed4..39c014c 100644 --- a/_examples/panic/main.go +++ b/_examples/panic/main.go @@ -35,23 +35,10 @@ } func panicDecorator(name, panicMsg string) decor.Decorator { - d := &decorator{ - msg: name, - panicMsg: panicMsg, - } - d.Init() - return d + return decor.Any(func(s *decor.Statistics) string { + if s.ID == 1 && s.Current >= 42 { + panic(panicMsg) + } + return name + }) } - -type decorator struct { - decor.WC - msg string - panicMsg string -} - -func (d *decorator) Decor(st *decor.Statistics) string { - if st.ID == 1 && st.Current >= 42 { - panic(d.panicMsg) - } - return d.FormatMsg(d.msg) -} diff --git a/_examples/suppressBar/main.go b/_examples/suppressBar/main.go index ef62dcf..f7d0c3e 100644 --- a/_examples/suppressBar/main.go +++ b/_examples/suppressBar/main.go @@ -24,7 +24,7 @@ decor.Name("my bar:"), ), mpb.AppendDecorators( - newCustomPercentage(decor.Percentage(), nextCh), + newCustomPercentage(nextCh), ), ) ew := &errorWrapper{} @@ -116,23 +116,15 @@ return cf, nextCh } -type myPercentageDecorator struct { - decor.Decorator - ch <-chan struct{} +func newCustomPercentage(ch <-chan struct{}) decor.Decorator { + base := decor.Percentage() + f := func(s *decor.Statistics) string { + select { + case <-ch: + return "" + default: + return base.Decor(s) + } + } + return decor.Any(f) } - -func (d *myPercentageDecorator) Decor(st *decor.Statistics) string { - select { - case <-d.ch: - return "" - default: - return d.Decorator.Decor(st) - } -} - -func newCustomPercentage(base decor.Decorator, ch <-chan struct{}) decor.Decorator { - return &myPercentageDecorator{ - Decorator: base, - ch: ch, - } -}