correct merge with more than one placeholders
Vladimir Bauer
6 years ago
| 1 | 1 | |
| 2 | 2 | import ( |
| 3 | 3 | "fmt" |
| 4 | "strings" | |
| 4 | 5 | "unicode/utf8" |
| 5 | 6 | ) |
| 6 | 7 | |
| 69 | 70 | msg := d.Decorator.Decor(st) |
| 70 | 71 | msgLen := utf8.RuneCountInString(msg) |
| 71 | 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 | }() | |
| 87 | } | |
| 88 | ||
| 72 | 89 | var space int |
| 73 | 90 | for _, ph := range d.placeHolders { |
| 74 | 91 | space += <-ph.wch |
| 75 | 92 | } |
| 76 | 93 | |
| 77 | 94 | d.wc.wsync <- msgLen - space |
| 78 | ||
| 79 | 95 | max := <-d.wc.wsync |
| 80 | 96 | if (d.wc.C & DextraSpace) != 0 { |
| 81 | 97 | max++ |
| 88 | 104 | wch chan int |
| 89 | 105 | } |
| 90 | 106 | |
| 91 | func (d *placeHolderDecorator) Decor(st *Statistics) string { | |
| 92 | go func() { | |
| 93 | d.wch <- utf8.RuneCountInString(d.FormatMsg("")) | |
| 94 | }() | |
| 107 | func (d *placeHolderDecorator) Decor(_ *Statistics) string { | |
| 95 | 108 | return "" |
| 96 | 109 | } |