Codebase list golang-github-vbauerster-mpb / c75c594
Merge and OnComplete Vladimir Bauer 6 years ago
4 changed file(s) with 32 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
2020 var pdecorators mpb.BarOption
2121 if i == 0 {
2222 pdecorators = mpb.PrependDecorators(
23 // Merge to sync width with decorators on lines 37 and 38
2423 decor.Merge(
25 newVariadicSpinner(decor.WCSyncSpace),
24 decor.OnComplete(
25 newVariadicSpinner(decor.WCSyncSpace),
26 "done",
27 ),
2628 decor.WCSyncSpace, // Placeholder
2729 ),
2830 )
5759
5860 func newVariadicSpinner(wc decor.WC) decor.Decorator {
5961 d := &variadicSpinner{
60 WC: wc.Init(),
61 d: decor.Spinner(nil),
62 WC: wc.Init(),
63 base: decor.Spinner(nil),
6264 }
6365 return d
6466 }
6567
6668 type variadicSpinner struct {
6769 decor.WC
68 d decor.Decorator
69 complete *string
70 base decor.Decorator
7071 }
7172
7273 func (d *variadicSpinner) Decor(st *decor.Statistics) string {
73 if st.Completed && d.complete != nil {
74 return d.FormatMsg(*d.complete)
75 }
76 msg := d.d.Decor(st)
74 msg := d.base.Decor(st)
7775 msg = strings.Repeat(msg, int(st.Current/3))
7876 return d.FormatMsg(msg)
7977 }
80
81 func (d *variadicSpinner) OnCompleteMessage(msg string) {
82 d.complete = &msg
83 }
1616 func (s *bState) addDecorators(dest *[]decor.Decorator, decorators ...decor.Decorator) {
1717 for _, decorator := range decorators {
1818 if mw, ok := decorator.(mergeWrapper); ok {
19 dd := mw.MergeUnwrap()
20 s.mDecorators = append(s.mDecorators, dd[0])
21 *dest = append(*dest, dd[1:]...)
19 *dest = append(*dest, mw.MergeUnwrap()...)
2220 }
2321 *dest = append(*dest, decorator)
2422 }
2424 }
2525 decorator.SetConf(&WC{})
2626 for i, wc := range placeholders {
27 if (wc.C & DSyncWidth) == 0 {
28 return decorator
29 }
2730 md.placeHolders[i] = &placeHolderDecorator{
28 WC: wc.Init(),
29 wsync: make(chan int),
31 WC: wc.Init(),
32 wch: make(chan int),
3033 }
3134 }
3235 return md
3942 }
4043
4144 func (d *mergeDecorator) MergeUnwrap() []Decorator {
42 decorators := make([]Decorator, len(d.placeHolders)+1)
43 decorators[0] = d.Decorator
45 decorators := make([]Decorator, len(d.placeHolders))
4446 for i, ph := range d.placeHolders {
45 decorators[i+1] = ph
47 decorators[i] = ph
4648 }
4749 return decorators
4850 }
5153 return d.wc.Sync()
5254 }
5355
56 func (d *mergeDecorator) Base() Decorator {
57 return d.Decorator
58 }
59
5460 func (d *mergeDecorator) Decor(st *Statistics) string {
5561 msg := d.Decorator.Decor(st)
5662 msgLen := utf8.RuneCountInString(msg)
5763
58 var pWidth int
64 var space int
5965 for _, ph := range d.placeHolders {
60 pWidth += <-ph.wsync
66 space += <-ph.wch
6167 }
6268
63 d.wc.wsync <- msgLen - pWidth
69 d.wc.wsync <- msgLen - space
6470
6571 max := <-d.wc.wsync
6672 if (d.wc.C & DextraSpace) != 0 {
6773 max++
6874 }
69 return fmt.Sprintf(fmt.Sprintf(d.wc.dynFormat, max+pWidth), msg)
75 return fmt.Sprintf(fmt.Sprintf(d.wc.dynFormat, max+space), msg)
7076 }
7177
7278 type placeHolderDecorator struct {
7379 WC
74 wsync chan int
80 wch chan int
7581 }
7682
7783 func (d *placeHolderDecorator) Decor(st *Statistics) string {
7884 go func() {
79 d.wsync <- utf8.RuneCountInString(d.FormatMsg(""))
85 d.wch <- utf8.RuneCountInString(d.FormatMsg(""))
8086 }()
8187 return ""
8288 }
88 func OnComplete(decorator Decorator, message string) Decorator {
99 d := &onCompleteWrapper{
1010 Decorator: decorator,
11 wc: decorator.GetConf(),
1211 msg: message,
1312 }
1413 return d
1615
1716 type onCompleteWrapper struct {
1817 Decorator
19 wc WC
2018 msg string
2119 }
2220
2321 func (d *onCompleteWrapper) Decor(st *Statistics) string {
2422 if st.Completed {
25 return d.wc.FormatMsg(d.msg)
23 wc := d.GetConf()
24 return wc.FormatMsg(d.msg)
2625 }
2726 return d.Decorator.Decor(st)
2827 }
28
29 func (d *onCompleteWrapper) Base() Decorator {
30 return d.Decorator
31 }