Codebase list golang-github-vbauerster-mpb / a72eaae
refactoring OnAbort no need for pointer receiver for wrapper methods Vladimir Bauer 2 years ago
1 changed file(s) with 10 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
00 package decor
11
22 var (
3 _ Decorator = (*onAbortWrapper)(nil)
4 _ Wrapper = (*onAbortWrapper)(nil)
3 _ Decorator = onAbortWrapper{}
4 _ Wrapper = onAbortWrapper{}
55 )
66
7 // OnAbort returns decorator, which wraps provided decorator with sole
8 // purpose to display provided message on abort event. It has no effect
9 // if bar.Abort(drop bool) is called with true argument.
7 // OnAbort wrap decorator.
8 // Displays provided message on abort event.
9 // Has no effect if bar.Abort(true) is called.
1010 //
1111 // `decorator` Decorator to wrap
12 //
13 // `message` message to display on abort event
12 // `message` message to display
1413 func OnAbort(decorator Decorator, message string) Decorator {
1514 if decorator == nil {
1615 return nil
1716 }
18 d := &onAbortWrapper{
19 Decorator: decorator,
20 msg: message,
21 }
22 if md, ok := decorator.(*mergeDecorator); ok {
23 d.Decorator, md.Decorator = md.Decorator, d
24 return md
25 }
26 return d
17 return onAbortWrapper{decorator, message}
2718 }
2819
2920 type onAbortWrapper struct {
3122 msg string
3223 }
3324
34 func (d *onAbortWrapper) Decor(s Statistics) string {
25 func (d onAbortWrapper) Decor(s Statistics) (string, int) {
3526 if s.Aborted {
36 return d.GetConf().FormatMsg(d.msg)
27 return d.Format(d.msg)
3728 }
3829 return d.Decorator.Decor(s)
3930 }
4031
41 func (d *onAbortWrapper) Unwrap() Decorator {
32 func (d onAbortWrapper) Unwrap() Decorator {
4233 return d.Decorator
4334 }