Codebase list golang-github-vbauerster-mpb / 8614962
merge wrapper refactoring Vladimir Bauer 6 years ago
1 changed file(s) with 25 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
2828 if (wc.C & DSyncWidth) == 0 {
2929 return decorator
3030 }
31 md.placeHolders[i] = &placeHolderDecorator{
32 WC: wc.Init(),
33 wch: make(chan int),
34 }
31 md.placeHolders[i] = &placeHolderDecorator{wc.Init()}
3532 }
3633 return md
3734 }
6966 func (d *mergeDecorator) Decor(st *Statistics) string {
7067 msg := d.Decorator.Decor(st)
7168 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++
8771 }
8872
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
9292 }
9393
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)
10097 }
10198
10299 type placeHolderDecorator struct {
103100 WC
104 wch chan int
105101 }
106102
107103 func (d *placeHolderDecorator) Decor(_ *Statistics) string {