Codebase list golang-github-vbauerster-mpb / 7fe06ad
refactoring: inspect decorators Vladimir Bauer 3 years ago
1 changed file(s) with 15 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
99 // BarOption is a func option to alter default behavior of a bar.
1010 type BarOption func(*bState)
1111
12 func skipNil(decorators []decor.Decorator) (filtered []decor.Decorator) {
13 for _, d := range decorators {
14 if d != nil {
15 filtered = append(filtered, d)
12 func inspect(decorators []decor.Decorator) (dest []decor.Decorator) {
13 type mergeWrapper interface {
14 MergeUnwrap() []decor.Decorator
15 }
16 for _, decorator := range decorators {
17 if decorator == nil {
18 continue
1619 }
20 if mw, ok := decorator.(mergeWrapper); ok {
21 dest = append(dest, mw.MergeUnwrap()...)
22 }
23 dest = append(dest, decorator)
1724 }
1825 return
1926 }
2027
21 func (s *bState) addDecorators(dest *[]decor.Decorator, decorators ...decor.Decorator) {
22 type mergeWrapper interface {
23 MergeUnwrap() []decor.Decorator
24 }
25 for _, decorator := range decorators {
26 if mw, ok := decorator.(mergeWrapper); ok {
27 *dest = append(*dest, mw.MergeUnwrap()...)
28 }
29 *dest = append(*dest, decorator)
30 }
31 }
32
3328 // AppendDecorators let you inject decorators to the bar's right side.
3429 func AppendDecorators(decorators ...decor.Decorator) BarOption {
30 decorators = inspect(decorators)
3531 return func(s *bState) {
36 s.addDecorators(&s.aDecorators, skipNil(decorators)...)
32 s.aDecorators = decorators
3733 }
3834 }
3935
4036 // PrependDecorators let you inject decorators to the bar's left side.
4137 func PrependDecorators(decorators ...decor.Decorator) BarOption {
38 decorators = inspect(decorators)
4239 return func(s *bState) {
43 s.addDecorators(&s.pDecorators, skipNil(decorators)...)
40 s.pDecorators = decorators
4441 }
4542 }
4643