on complete wrapper
Vladimir Bauer
6 years ago
| 46 | 46 | for _, widthConf := range wcc { |
| 47 | 47 | wc = widthConf |
| 48 | 48 | } |
| 49 | wc.Init() | |
| 50 | 49 | if pairFmt == "" { |
| 51 | 50 | pairFmt = "%d / %d" |
| 52 | 51 | } |
| 53 | 52 | d := &countersDecorator{ |
| 54 | WC: wc, | |
| 53 | WC: wc.Init(), | |
| 55 | 54 | unit: unit, |
| 56 | 55 | pairFmt: pairFmt, |
| 57 | 56 | } |
| 60 | 59 | |
| 61 | 60 | type countersDecorator struct { |
| 62 | 61 | WC |
| 63 | unit int | |
| 64 | pairFmt string | |
| 65 | completeMsg *string | |
| 62 | unit int | |
| 63 | pairFmt string | |
| 66 | 64 | } |
| 67 | 65 | |
| 68 | 66 | func (d *countersDecorator) Decor(st *Statistics) string { |
| 69 | if st.Completed && d.completeMsg != nil { | |
| 70 | return d.FormatMsg(*d.completeMsg) | |
| 71 | } | |
| 72 | ||
| 73 | 67 | var res string |
| 74 | 68 | switch d.unit { |
| 75 | 69 | case UnitKiB: |
| 82 | 76 | |
| 83 | 77 | return d.FormatMsg(res) |
| 84 | 78 | } |
| 85 | ||
| 86 | func (d *countersDecorator) OnCompleteMessage(msg string) { | |
| 87 | d.completeMsg = &msg | |
| 88 | } | |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | // Decorator interface. |
| 54 | // A decorator must implement this interface, in order to be used with | |
| 55 | // mpb library. | |
| 54 | // Implementors should embed WC type, that way only single method | |
| 55 | // Decor(*Statistics) needs to be implemented, the rest will be handled | |
| 56 | // by WC type. | |
| 56 | 57 | type Decorator interface { |
| 57 | ConfigSetter | |
| 58 | Configurator | |
| 58 | 59 | Synchronizer |
| 59 | 60 | Decor(*Statistics) string |
| 60 | 61 | } |
| 66 | 67 | Sync() (chan int, bool) |
| 67 | 68 | } |
| 68 | 69 | |
| 69 | // ConfigSetter interface. | |
| 70 | type ConfigSetter interface { | |
| 71 | SetConfig(config WC) (old WC) | |
| 72 | } | |
| 73 | ||
| 74 | // OnCompleteMessenger interface. | |
| 75 | // Decorators implementing this interface suppose to return provided | |
| 76 | // string on complete event. | |
| 77 | type OnCompleteMessenger interface { | |
| 78 | OnCompleteMessage(string) | |
| 70 | // Configurator interface. | |
| 71 | type Configurator interface { | |
| 72 | GetConf() WC | |
| 73 | SetConf(*WC) | |
| 79 | 74 | } |
| 80 | 75 | |
| 81 | 76 | // AmountReceiver interface. |
| 82 | // EWMA based decorators must implement this one. | |
| 77 | // EWMA based decorators need to implement this one. | |
| 83 | 78 | type AmountReceiver interface { |
| 84 | 79 | NextAmount(int64, ...time.Duration) |
| 85 | 80 | } |
| 135 | 130 | } |
| 136 | 131 | |
| 137 | 132 | // Init initializes width related config. |
| 138 | func (wc *WC) Init() { | |
| 133 | func (wc *WC) Init() WC { | |
| 139 | 134 | wc.dynFormat = "%%" |
| 140 | 135 | if (wc.C & DidentRight) != 0 { |
| 141 | 136 | wc.dynFormat += "-" |
| 143 | 138 | wc.dynFormat += "%ds" |
| 144 | 139 | wc.staticFormat = fmt.Sprintf(wc.dynFormat, wc.W) |
| 145 | 140 | if (wc.C & DSyncWidth) != 0 { |
| 141 | // it's deliberate choice to override wsync on each Init() call, | |
| 142 | // this way globals like WCSyncSpace can be reused | |
| 146 | 143 | wc.wsync = make(chan int) |
| 147 | 144 | } |
| 145 | return *wc | |
| 148 | 146 | } |
| 149 | 147 | |
| 150 | 148 | // Sync is implementation of Synchronizer interface. |
| 152 | 150 | return wc.wsync, (wc.C & DSyncWidth) != 0 |
| 153 | 151 | } |
| 154 | 152 | |
| 155 | // SetConfig sets new conf and returns old one. | |
| 156 | func (wc *WC) SetConfig(conf WC) (old WC) { | |
| 157 | conf.Init() | |
| 158 | old = *wc | |
| 159 | *wc = conf | |
| 160 | return old | |
| 153 | // GetConf is implementation of Configurator interface. | |
| 154 | func (wc *WC) GetConf() WC { | |
| 155 | return *wc | |
| 161 | 156 | } |
| 162 | 157 | |
| 163 | // OnComplete returns decorator, which wraps provided decorator, with | |
| 164 | // sole purpose to display provided message on complete event. | |
| 165 | // | |
| 166 | // `decorator` Decorator to wrap | |
| 167 | // | |
| 168 | // `message` message to display on complete event | |
| 169 | func OnComplete(decorator Decorator, message string) Decorator { | |
| 170 | if d, ok := decorator.(OnCompleteMessenger); ok { | |
| 171 | d.OnCompleteMessage(message) | |
| 172 | } | |
| 173 | return decorator | |
| 158 | // SetConf is implementation of Configurator interface. | |
| 159 | func (wc *WC) SetConf(conf *WC) { | |
| 160 | *wc = conf.Init() | |
| 174 | 161 | } |
| 24 | 24 | for _, widthConf := range wcc { |
| 25 | 25 | wc = widthConf |
| 26 | 26 | } |
| 27 | wc.Init() | |
| 28 | 27 | d := &elapsedDecorator{ |
| 29 | WC: wc, | |
| 28 | WC: wc.Init(), | |
| 30 | 29 | startTime: startTime, |
| 31 | 30 | producer: chooseTimeProducer(style), |
| 32 | 31 | } |
| 35 | 34 | |
| 36 | 35 | type elapsedDecorator struct { |
| 37 | 36 | WC |
| 38 | startTime time.Time | |
| 39 | producer func(time.Duration) string | |
| 40 | msg string | |
| 41 | completeMsg *string | |
| 37 | startTime time.Time | |
| 38 | producer func(time.Duration) string | |
| 39 | msg string | |
| 42 | 40 | } |
| 43 | 41 | |
| 44 | 42 | func (d *elapsedDecorator) Decor(st *Statistics) string { |
| 45 | if st.Completed { | |
| 46 | if d.completeMsg != nil { | |
| 47 | return d.FormatMsg(*d.completeMsg) | |
| 48 | } | |
| 49 | return d.FormatMsg(d.msg) | |
| 43 | if !st.Completed { | |
| 44 | d.msg = d.producer(time.Since(d.startTime)) | |
| 50 | 45 | } |
| 51 | ||
| 52 | d.msg = d.producer(time.Since(d.startTime)) | |
| 53 | 46 | return d.FormatMsg(d.msg) |
| 54 | 47 | } |
| 55 | ||
| 56 | func (d *elapsedDecorator) OnCompleteMessage(msg string) { | |
| 57 | d.completeMsg = &msg | |
| 58 | } | |
| 49 | 49 | for _, widthConf := range wcc { |
| 50 | 50 | wc = widthConf |
| 51 | 51 | } |
| 52 | wc.Init() | |
| 53 | 52 | d := &movingAverageETA{ |
| 54 | WC: wc, | |
| 53 | WC: wc.Init(), | |
| 55 | 54 | average: average, |
| 56 | 55 | normalizer: normalizer, |
| 57 | 56 | producer: chooseTimeProducer(style), |
| 61 | 60 | |
| 62 | 61 | type movingAverageETA struct { |
| 63 | 62 | WC |
| 64 | average ewma.MovingAverage | |
| 65 | normalizer TimeNormalizer | |
| 66 | producer func(time.Duration) string | |
| 67 | completeMsg *string | |
| 63 | average ewma.MovingAverage | |
| 64 | normalizer TimeNormalizer | |
| 65 | producer func(time.Duration) string | |
| 68 | 66 | } |
| 69 | 67 | |
| 70 | 68 | func (d *movingAverageETA) Decor(st *Statistics) string { |
| 71 | if st.Completed && d.completeMsg != nil { | |
| 72 | return d.FormatMsg(*d.completeMsg) | |
| 73 | } | |
| 74 | ||
| 75 | 69 | v := math.Round(d.average.Value()) |
| 76 | 70 | remaining := time.Duration((st.Total - st.Current) * int64(v)) |
| 77 | 71 | if d.normalizer != nil { |
| 90 | 84 | return |
| 91 | 85 | } |
| 92 | 86 | d.average.Add(durPerItem) |
| 93 | } | |
| 94 | ||
| 95 | func (d *movingAverageETA) OnCompleteMessage(msg string) { | |
| 96 | d.completeMsg = &msg | |
| 97 | 87 | } |
| 98 | 88 | |
| 99 | 89 | // AverageETA decorator. It's wrapper of NewAverageETA. |
| 119 | 109 | for _, widthConf := range wcc { |
| 120 | 110 | wc = widthConf |
| 121 | 111 | } |
| 122 | wc.Init() | |
| 123 | 112 | d := &averageETA{ |
| 124 | WC: wc, | |
| 113 | WC: wc.Init(), | |
| 125 | 114 | startTime: startTime, |
| 126 | 115 | normalizer: normalizer, |
| 127 | 116 | producer: chooseTimeProducer(style), |
| 131 | 120 | |
| 132 | 121 | type averageETA struct { |
| 133 | 122 | WC |
| 134 | startTime time.Time | |
| 135 | normalizer TimeNormalizer | |
| 136 | producer func(time.Duration) string | |
| 137 | completeMsg *string | |
| 123 | startTime time.Time | |
| 124 | normalizer TimeNormalizer | |
| 125 | producer func(time.Duration) string | |
| 138 | 126 | } |
| 139 | 127 | |
| 140 | 128 | func (d *averageETA) Decor(st *Statistics) string { |
| 141 | if st.Completed && d.completeMsg != nil { | |
| 142 | return d.FormatMsg(*d.completeMsg) | |
| 143 | } | |
| 144 | ||
| 145 | 129 | var remaining time.Duration |
| 146 | 130 | if st.Current != 0 { |
| 147 | 131 | durPerItem := float64(time.Since(d.startTime)) / float64(st.Current) |
| 152 | 136 | } |
| 153 | 137 | } |
| 154 | 138 | return d.FormatMsg(d.producer(remaining)) |
| 155 | } | |
| 156 | ||
| 157 | func (d *averageETA) OnCompleteMessage(msg string) { | |
| 158 | d.completeMsg = &msg | |
| 159 | 139 | } |
| 160 | 140 | |
| 161 | 141 | func (d *averageETA) AverageAdjust(startTime time.Time) { |
| 19 | 19 | } |
| 20 | 20 | md := &mergeDecorator{ |
| 21 | 21 | Decorator: decorator, |
| 22 | wc: decorator.GetConf(), | |
| 22 | 23 | placeHolders: make([]*placeHolderDecorator, len(placeholders)), |
| 23 | 24 | } |
| 24 | md.wc = decorator.SetConfig(md.wc) | |
| 25 | decorator.SetConf(&WC{}) | |
| 25 | 26 | for i, wc := range placeholders { |
| 26 | wc.Init() | |
| 27 | 27 | md.placeHolders[i] = &placeHolderDecorator{ |
| 28 | WC: wc, | |
| 28 | WC: wc.Init(), | |
| 29 | 29 | wsync: make(chan int), |
| 30 | 30 | } |
| 31 | 31 | } |
| 9 | 9 | for _, widthConf := range wcc { |
| 10 | 10 | wc = widthConf |
| 11 | 11 | } |
| 12 | wc.Init() | |
| 13 | 12 | d := &nameDecorator{ |
| 14 | WC: wc, | |
| 13 | WC: wc.Init(), | |
| 15 | 14 | msg: name, |
| 16 | 15 | } |
| 17 | 16 | return d |
| 19 | 18 | |
| 20 | 19 | type nameDecorator struct { |
| 21 | 20 | WC |
| 22 | msg string | |
| 23 | complete *string | |
| 21 | msg string | |
| 24 | 22 | } |
| 25 | 23 | |
| 26 | 24 | func (d *nameDecorator) Decor(st *Statistics) string { |
| 27 | if st.Completed && d.complete != nil { | |
| 28 | return d.FormatMsg(*d.complete) | |
| 29 | } | |
| 30 | 25 | return d.FormatMsg(d.msg) |
| 31 | 26 | } |
| 32 | ||
| 33 | func (d *nameDecorator) OnCompleteMessage(msg string) { | |
| 34 | d.complete = &msg | |
| 35 | } | |
| 0 | package decor | |
| 1 | ||
| 2 | // OnComplete returns decorator, which wraps provided decorator, with | |
| 3 | // sole purpose to display provided message on complete event. | |
| 4 | // | |
| 5 | // `decorator` Decorator to wrap | |
| 6 | // | |
| 7 | // `message` message to display on complete event | |
| 8 | func OnComplete(decorator Decorator, message string) Decorator { | |
| 9 | d := &onCompleteWrapper{ | |
| 10 | Decorator: decorator, | |
| 11 | wc: decorator.GetConf(), | |
| 12 | msg: message, | |
| 13 | } | |
| 14 | return d | |
| 15 | } | |
| 16 | ||
| 17 | type onCompleteWrapper struct { | |
| 18 | Decorator | |
| 19 | wc WC | |
| 20 | msg string | |
| 21 | } | |
| 22 | ||
| 23 | func (d *onCompleteWrapper) Decor(st *Statistics) string { | |
| 24 | if st.Completed { | |
| 25 | return d.wc.FormatMsg(d.msg) | |
| 26 | } | |
| 27 | return d.Decorator.Decor(st) | |
| 28 | } |
| 50 | 50 | for _, widthConf := range wcc { |
| 51 | 51 | wc = widthConf |
| 52 | 52 | } |
| 53 | wc.Init() | |
| 54 | 53 | if fmt == "" { |
| 55 | 54 | fmt = "% d" |
| 56 | 55 | } |
| 57 | 56 | d := &percentageDecorator{ |
| 58 | WC: wc, | |
| 57 | WC: wc.Init(), | |
| 59 | 58 | fmt: fmt, |
| 60 | 59 | } |
| 61 | 60 | return d |
| 63 | 62 | |
| 64 | 63 | type percentageDecorator struct { |
| 65 | 64 | WC |
| 66 | fmt string | |
| 67 | completeMsg *string | |
| 65 | fmt string | |
| 68 | 66 | } |
| 69 | 67 | |
| 70 | 68 | func (d *percentageDecorator) Decor(st *Statistics) string { |
| 71 | if st.Completed && d.completeMsg != nil { | |
| 72 | return d.FormatMsg(*d.completeMsg) | |
| 73 | } | |
| 74 | 69 | p := internal.Percentage(st.Total, st.Current, 100) |
| 75 | 70 | return d.FormatMsg(fmt.Sprintf(d.fmt, percentageType(p))) |
| 76 | 71 | } |
| 77 | ||
| 78 | func (d *percentageDecorator) OnCompleteMessage(msg string) { | |
| 79 | d.completeMsg = &msg | |
| 80 | } | |
| 58 | 58 | if format == "" { |
| 59 | 59 | format = "%.0f" |
| 60 | 60 | } |
| 61 | wc.Init() | |
| 62 | 61 | d := &movingAverageSpeed{ |
| 63 | WC: wc, | |
| 62 | WC: wc.Init(), | |
| 64 | 63 | average: average, |
| 65 | 64 | producer: chooseSpeedProducer(unit, format), |
| 66 | 65 | } |
| 69 | 68 | |
| 70 | 69 | type movingAverageSpeed struct { |
| 71 | 70 | WC |
| 72 | producer func(float64) string | |
| 73 | average ewma.MovingAverage | |
| 74 | msg string | |
| 75 | completeMsg *string | |
| 71 | producer func(float64) string | |
| 72 | average ewma.MovingAverage | |
| 73 | msg string | |
| 76 | 74 | } |
| 77 | 75 | |
| 78 | 76 | func (d *movingAverageSpeed) Decor(st *Statistics) string { |
| 79 | if st.Completed { | |
| 80 | if d.completeMsg != nil { | |
| 81 | return d.FormatMsg(*d.completeMsg) | |
| 77 | if !st.Completed { | |
| 78 | var speed float64 | |
| 79 | if v := math.Round(d.average.Value()); v != 0 { | |
| 80 | speed = 1 / time.Duration(v).Seconds() | |
| 82 | 81 | } |
| 83 | return d.FormatMsg(d.msg) | |
| 82 | d.msg = d.producer(speed) | |
| 84 | 83 | } |
| 85 | ||
| 86 | var speed float64 | |
| 87 | if v := math.Round(d.average.Value()); v != 0 { | |
| 88 | speed = 1 / time.Duration(v).Seconds() | |
| 89 | } | |
| 90 | ||
| 91 | d.msg = d.producer(speed) | |
| 92 | 84 | return d.FormatMsg(d.msg) |
| 93 | 85 | } |
| 94 | 86 | |
| 102 | 94 | return |
| 103 | 95 | } |
| 104 | 96 | d.average.Add(durPerByte) |
| 105 | } | |
| 106 | ||
| 107 | func (d *movingAverageSpeed) OnCompleteMessage(msg string) { | |
| 108 | d.completeMsg = &msg | |
| 109 | 97 | } |
| 110 | 98 | |
| 111 | 99 | // AverageSpeed decorator with dynamic unit measure adjustment. It's |
| 140 | 128 | if format == "" { |
| 141 | 129 | format = "%.0f" |
| 142 | 130 | } |
| 143 | wc.Init() | |
| 144 | 131 | d := &averageSpeed{ |
| 145 | WC: wc, | |
| 132 | WC: wc.Init(), | |
| 146 | 133 | startTime: startTime, |
| 147 | 134 | producer: chooseSpeedProducer(unit, format), |
| 148 | 135 | } |
| 151 | 138 | |
| 152 | 139 | type averageSpeed struct { |
| 153 | 140 | WC |
| 154 | startTime time.Time | |
| 155 | producer func(float64) string | |
| 156 | msg string | |
| 157 | completeMsg *string | |
| 141 | startTime time.Time | |
| 142 | producer func(float64) string | |
| 143 | msg string | |
| 158 | 144 | } |
| 159 | 145 | |
| 160 | 146 | func (d *averageSpeed) Decor(st *Statistics) string { |
| 161 | if st.Completed { | |
| 162 | if d.completeMsg != nil { | |
| 163 | return d.FormatMsg(*d.completeMsg) | |
| 164 | } | |
| 165 | return d.FormatMsg(d.msg) | |
| 147 | if !st.Completed { | |
| 148 | speed := float64(st.Current) / time.Since(d.startTime).Seconds() | |
| 149 | d.msg = d.producer(speed) | |
| 166 | 150 | } |
| 167 | 151 | |
| 168 | speed := float64(st.Current) / time.Since(d.startTime).Seconds() | |
| 169 | d.msg = d.producer(speed) | |
| 170 | ||
| 171 | 152 | return d.FormatMsg(d.msg) |
| 172 | } | |
| 173 | ||
| 174 | func (d *averageSpeed) OnCompleteMessage(msg string) { | |
| 175 | d.completeMsg = &msg | |
| 176 | 153 | } |
| 177 | 154 | |
| 178 | 155 | func (d *averageSpeed) AverageAdjust(startTime time.Time) { |
| 11 | 11 | for _, widthConf := range wcc { |
| 12 | 12 | wc = widthConf |
| 13 | 13 | } |
| 14 | wc.Init() | |
| 15 | 14 | if len(frames) == 0 { |
| 16 | 15 | frames = defaultSpinnerStyle |
| 17 | 16 | } |
| 18 | 17 | d := &spinnerDecorator{ |
| 19 | WC: wc, | |
| 18 | WC: wc.Init(), | |
| 20 | 19 | frames: frames, |
| 21 | 20 | } |
| 22 | 21 | return d |
| 24 | 23 | |
| 25 | 24 | type spinnerDecorator struct { |
| 26 | 25 | WC |
| 27 | frames []string | |
| 28 | count uint | |
| 29 | complete *string | |
| 26 | frames []string | |
| 27 | count uint | |
| 30 | 28 | } |
| 31 | 29 | |
| 32 | 30 | func (d *spinnerDecorator) Decor(st *Statistics) string { |
| 33 | if st.Completed && d.complete != nil { | |
| 34 | return d.FormatMsg(*d.complete) | |
| 35 | } | |
| 36 | 31 | frame := d.frames[d.count%uint(len(d.frames))] |
| 37 | 32 | d.count++ |
| 38 | 33 | return d.FormatMsg(frame) |
| 39 | 34 | } |
| 40 | ||
| 41 | func (d *spinnerDecorator) OnCompleteMessage(msg string) { | |
| 42 | d.complete = &msg | |
| 43 | } | |