Skip to content

Commit

Permalink
merge wrapper refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
vbauerster committed Dec 14, 2019
1 parent 16a6e3f commit 8614962
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions decor/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ func Merge(decorator Decorator, placeholders ...WC) Decorator {
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
}
Expand Down Expand Up @@ -70,39 +67,38 @@ func (d *mergeDecorator) Base() Decorator {
func (d *mergeDecorator) Decor(st *Statistics) string {
msg := d.Decorator.Decor(st)
msgLen := utf8.RuneCountInString(msg)
if (d.wc.C & DextraSpace) != 0 {
msgLen++
}

pw := msgLen / (len(d.placeHolders) + 1)
var total int
max := utf8.RuneCountInString(d.placeHolders[0].FormatMsg(""))
total += max
pw := (msgLen - max) / len(d.placeHolders)
rem := (msgLen - max) % len(d.placeHolders)

for _, ph := range d.placeHolders {
ph := ph
width := pw
go func() {
if (ph.WC.C & DextraSpace) != 0 {
width--
if width < 0 {
width = 0
}
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
}
ph.wch <- utf8.RuneCountInString(ph.FormatMsg(strings.Repeat(" ", width)))
}()
}

var space int
for _, ph := range d.placeHolders {
space += <-ph.wch
}
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 {
Expand Down

0 comments on commit 8614962

Please sign in to comment.