merge wrapper refactoring
Vladimir Bauer
6 years ago
| 28 | 28 | if (wc.C & DSyncWidth) == 0 { |
| 29 | 29 | return decorator |
| 30 | 30 | } |
| 31 | md.placeHolders[i] = &placeHolderDecorator{ | |
| 32 | WC: wc.Init(), | |
| 33 | wch: make(chan int), | |
| 34 | } | |
| 31 | md.placeHolders[i] = &placeHolderDecorator{wc.Init()} | |
| 35 | 32 | } |
| 36 | 33 | return md |
| 37 | 34 | } |
| 69 | 66 | func (d *mergeDecorator) Decor(st *Statistics) string { |
| 70 | 67 | msg := d.Decorator.Decor(st) |
| 71 | 68 | msgLen := utf8.RuneCountInString(msg) |
| 72 | ||
| 73 | pw := msgLen / (len(d.placeHolders) + 1) | |
| 74 | ||
| 75 | for _, ph := range d.placeHolders { | |
| 76 | ph := ph | |
| 77 | width := pw | |
| 78 | go func() { | |
| 79 | if (ph.WC.C & DextraSpace) != 0 { | |
| 80 | width-- | |
| 81 | if width < 0 { | |
| 82 | width = 0 | |
| 83 | } | |
| 84 | } | |
| 85 | ph.wch <- utf8.RuneCountInString(ph.FormatMsg(strings.Repeat(" ", width))) | |
| 86 | }() | |
| 69 | if (d.wc.C & DextraSpace) != 0 { | |
| 70 | msgLen++ | |
| 87 | 71 | } |
| 88 | 72 | |
| 89 | var space int | |
| 90 | for _, ph := range d.placeHolders { | |
| 91 | space += <-ph.wch | |
| 73 | var total int | |
| 74 | max := utf8.RuneCountInString(d.placeHolders[0].FormatMsg("")) | |
| 75 | total += max | |
| 76 | pw := (msgLen - max) / len(d.placeHolders) | |
| 77 | rem := (msgLen - max) % len(d.placeHolders) | |
| 78 | ||
| 79 | var diff int | |
| 80 | for i := 1; i < len(d.placeHolders); i++ { | |
| 81 | ph := d.placeHolders[i] | |
| 82 | width := pw - diff | |
| 83 | if (ph.WC.C & DextraSpace) != 0 { | |
| 84 | width-- | |
| 85 | if width < 0 { | |
| 86 | width = 0 | |
| 87 | } | |
| 88 | } | |
| 89 | max = utf8.RuneCountInString(ph.FormatMsg(strings.Repeat(" ", width))) | |
| 90 | total += max | |
| 91 | diff = max - pw | |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | d.wc.wsync <- msgLen - space | |
| 95 | max := <-d.wc.wsync | |
| 96 | if (d.wc.C & DextraSpace) != 0 { | |
| 97 | max++ | |
| 98 | } | |
| 99 | return fmt.Sprintf(fmt.Sprintf(d.wc.dynFormat, max+space), msg) | |
| 94 | d.wc.wsync <- pw + rem | |
| 95 | max = <-d.wc.wsync | |
| 96 | return fmt.Sprintf(fmt.Sprintf(d.wc.dynFormat, max+total), msg) | |
| 100 | 97 | } |
| 101 | 98 | |
| 102 | 99 | type placeHolderDecorator struct { |
| 103 | 100 | WC |
| 104 | wch chan int | |
| 105 | 101 | } |
| 106 | 102 | |
| 107 | 103 | func (d *placeHolderDecorator) Decor(_ *Statistics) string { |