diff --git a/decor/merge.go b/decor/merge.go index f4f2aac..7238692 100644 --- a/decor/merge.go +++ b/decor/merge.go @@ -29,10 +29,7 @@ if (wc.C & DSyncWidth) == 0 { return decorator } - md.placeHolders[i] = &placeHolderDecorator{ - WC: wc.Init(), - wch: make(chan int), - } + md.placeHolders[i] = &placeHolderDecorator{wc.Init()} } return md } @@ -70,39 +67,38 @@ func (d *mergeDecorator) Decor(st *Statistics) string { msg := d.Decorator.Decor(st) msgLen := utf8.RuneCountInString(msg) - - pw := msgLen / (len(d.placeHolders) + 1) - - for _, ph := range d.placeHolders { - ph := ph - width := pw - go func() { - if (ph.WC.C & DextraSpace) != 0 { - width-- - if width < 0 { - width = 0 - } - } - ph.wch <- utf8.RuneCountInString(ph.FormatMsg(strings.Repeat(" ", width))) - }() + if (d.wc.C & DextraSpace) != 0 { + msgLen++ } - var space int - for _, ph := range d.placeHolders { - space += <-ph.wch + var total int + max := utf8.RuneCountInString(d.placeHolders[0].FormatMsg("")) + total += max + pw := (msgLen - max) / len(d.placeHolders) + rem := (msgLen - max) % len(d.placeHolders) + + var diff int + for i := 1; i < len(d.placeHolders); i++ { + ph := d.placeHolders[i] + width := pw - diff + if (ph.WC.C & DextraSpace) != 0 { + width-- + if width < 0 { + width = 0 + } + } + max = utf8.RuneCountInString(ph.FormatMsg(strings.Repeat(" ", width))) + total += max + diff = max - pw } - d.wc.wsync <- msgLen - space - max := <-d.wc.wsync - if (d.wc.C & DextraSpace) != 0 { - max++ - } - return fmt.Sprintf(fmt.Sprintf(d.wc.dynFormat, max+space), msg) + d.wc.wsync <- pw + rem + max = <-d.wc.wsync + return fmt.Sprintf(fmt.Sprintf(d.wc.dynFormat, max+total), msg) } type placeHolderDecorator struct { WC - wch chan int } func (d *placeHolderDecorator) Decor(_ *Statistics) string {