| 6 | 6 |
// BarOption is a function option which changes the default behavior of a bar.
|
| 7 | 7 |
type BarOption func(*bState)
|
| 8 | 8 |
|
|
9 |
type merger interface {
|
|
10 |
CompoundDecorators() []decor.Decorator
|
|
11 |
}
|
|
12 |
|
|
13 |
func (s *bState) appendAmountReceiver(d decor.Decorator) {
|
|
14 |
if ar, ok := d.(decor.AmountReceiver); ok {
|
|
15 |
s.amountReceivers = append(s.amountReceivers, ar)
|
|
16 |
}
|
|
17 |
}
|
|
18 |
|
|
19 |
func (s *bState) appendShutdownListener(d decor.Decorator) {
|
|
20 |
if sl, ok := d.(decor.ShutdownListener); ok {
|
|
21 |
s.shutdownListeners = append(s.shutdownListeners, sl)
|
|
22 |
}
|
|
23 |
}
|
|
24 |
|
|
25 |
func (s *bState) addDecorators(dest *[]decor.Decorator, decorators ...decor.Decorator) {
|
|
26 |
for _, decorator := range decorators {
|
|
27 |
s.appendAmountReceiver(decorator)
|
|
28 |
s.appendShutdownListener(decorator)
|
|
29 |
if m, ok := decorator.(merger); ok {
|
|
30 |
dd := m.CompoundDecorators()
|
|
31 |
s.appendAmountReceiver(dd[0])
|
|
32 |
s.appendShutdownListener(dd[0])
|
|
33 |
*dest = append(*dest, dd[1:]...)
|
|
34 |
}
|
|
35 |
*dest = append(*dest, decorator)
|
|
36 |
}
|
|
37 |
}
|
|
38 |
|
| 9 | 39 |
// AppendDecorators let you inject decorators to the bar's right side.
|
| 10 | |
func AppendDecorators(appenders ...decor.Decorator) BarOption {
|
| 11 | |
return func(s *bState) {
|
| 12 | |
for _, decorator := range appenders {
|
| 13 | |
s.appendAmountReceiver(decorator)
|
| 14 | |
s.appendShutdownListener(decorator)
|
| 15 | |
if md, ok := decorator.(*decor.MergeDecorator); ok {
|
| 16 | |
s.appendAmountReceiver(md.Decorator)
|
| 17 | |
s.appendShutdownListener(md.Decorator)
|
| 18 | |
s.aDecorators = append(s.aDecorators, md.PlaceHolders()...)
|
| 19 | |
}
|
| 20 | |
s.aDecorators = append(s.aDecorators, decorator)
|
| 21 | |
}
|
|
40 |
func AppendDecorators(decorators ...decor.Decorator) BarOption {
|
|
41 |
return func(s *bState) {
|
|
42 |
s.addDecorators(&s.aDecorators, decorators...)
|
| 22 | 43 |
}
|
| 23 | 44 |
}
|
| 24 | 45 |
|
| 25 | 46 |
// PrependDecorators let you inject decorators to the bar's left side.
|
| 26 | |
func PrependDecorators(prependers ...decor.Decorator) BarOption {
|
| 27 | |
return func(s *bState) {
|
| 28 | |
for _, decorator := range prependers {
|
| 29 | |
s.appendAmountReceiver(decorator)
|
| 30 | |
s.appendShutdownListener(decorator)
|
| 31 | |
if md, ok := decorator.(*decor.MergeDecorator); ok {
|
| 32 | |
s.appendAmountReceiver(md.Decorator)
|
| 33 | |
s.appendShutdownListener(md.Decorator)
|
| 34 | |
s.pDecorators = append(s.pDecorators, md.PlaceHolders()...)
|
| 35 | |
}
|
| 36 | |
s.pDecorators = append(s.pDecorators, decorator)
|
| 37 | |
}
|
|
47 |
func PrependDecorators(decorators ...decor.Decorator) BarOption {
|
|
48 |
return func(s *bState) {
|
|
49 |
s.addDecorators(&s.pDecorators, decorators...)
|
| 38 | 50 |
}
|
| 39 | 51 |
}
|
| 40 | 52 |
|