diff --git a/_examples/remove/main.go b/_examples/remove/main.go index b191b4c..0f9a8f7 100644 --- a/_examples/remove/main.go +++ b/_examples/remove/main.go @@ -24,7 +24,6 @@ mpb.BarOptional(mpb.BarRemoveOnComplete(), i == 0), mpb.PrependDecorators( decor.Name(name), - decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WCSyncSpace), ), mpb.AppendDecorators( decor.Any(func(s decor.Statistics) string { @@ -33,7 +32,8 @@ decor.Any(func(s decor.Statistics) string { return fmt.Sprintf("aborted: %v", s.Aborted) }, decor.WCSyncSpaceR), - decor.OnComplete(decor.Percentage(decor.WCSyncSpace), "done"), + decor.OnComplete(decor.NewPercentage("%d", decor.WCSyncSpace), "done"), + decor.OnAbort(decor.NewPercentage("%d", decor.WCSyncSpace), "ohno"), ), ) go func() { @@ -41,16 +41,11 @@ rng := rand.New(rand.NewSource(time.Now().UnixNano())) max := 100 * time.Millisecond for i := 0; !bar.Completed(); i++ { - // start variable is solely for EWMA calculation - // EWMA's unit of measure is an iteration's duration - start := time.Now() if bar.ID() == 2 && i >= 42 { bar.Abort(false) } time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10) bar.Increment() - // we need to call DecoratorEwmaUpdate to fulfill ewma decorator's contract - bar.DecoratorEwmaUpdate(time.Since(start)) } }() } diff --git a/decor/on_abort.go b/decor/on_abort.go new file mode 100644 index 0000000..b50f0c9 --- /dev/null +++ b/decor/on_abort.go @@ -0,0 +1,38 @@ +package decor + +// OnAbort returns decorator, which wraps provided decorator with sole +// purpose to display provided message on abort event. It has no effect +// if bar.Abort(drop bool) is called with true argument. +// +// `decorator` Decorator to wrap +// +// `message` message to display on complete event +// +func OnAbort(decorator Decorator, message string) Decorator { + d := &onAbortWrapper{ + Decorator: decorator, + msg: message, + } + if md, ok := decorator.(*mergeDecorator); ok { + d.Decorator, md.Decorator = md.Decorator, d + return md + } + return d +} + +type onAbortWrapper struct { + Decorator + msg string +} + +func (d *onAbortWrapper) Decor(s Statistics) string { + if s.Aborted { + wc := d.GetConf() + return wc.FormatMsg(d.msg) + } + return d.Decorator.Decor(s) +} + +func (d *onAbortWrapper) Base() Decorator { + return d.Decorator +}