Codebase list golang-github-vbauerster-mpb / 30e87c5
refactoring OnComplete no need for pointer receiver for wrapper methods Vladimir Bauer 2 years ago
1 changed file(s) with 9 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
00 package decor
11
22 var (
3 _ Decorator = (*onCompleteWrapper)(nil)
4 _ Wrapper = (*onCompleteWrapper)(nil)
3 _ Decorator = onCompleteWrapper{}
4 _ Wrapper = onCompleteWrapper{}
55 )
66
7 // OnComplete returns decorator, which wraps provided decorator with
8 // sole purpose to display provided message on complete event.
7 // OnComplete wrap decorator.
8 // Displays provided message on complete event.
99 //
1010 // `decorator` Decorator to wrap
11 //
12 // `message` message to display on complete event
11 // `message` message to display
1312 func OnComplete(decorator Decorator, message string) Decorator {
1413 if decorator == nil {
1514 return nil
1615 }
17 d := &onCompleteWrapper{
18 Decorator: decorator,
19 msg: message,
20 }
21 if md, ok := decorator.(*mergeDecorator); ok {
22 d.Decorator, md.Decorator = md.Decorator, d
23 return md
24 }
25 return d
16 return onCompleteWrapper{decorator, message}
2617 }
2718
2819 type onCompleteWrapper struct {
3021 msg string
3122 }
3223
33 func (d *onCompleteWrapper) Decor(s Statistics) string {
24 func (d onCompleteWrapper) Decor(s Statistics) (string, int) {
3425 if s.Completed {
35 return d.GetConf().FormatMsg(d.msg)
26 return d.Format(d.msg)
3627 }
3728 return d.Decorator.Decor(s)
3829 }
3930
40 func (d *onCompleteWrapper) Unwrap() Decorator {
31 func (d onCompleteWrapper) Unwrap() Decorator {
4132 return d.Decorator
4233 }