gofmt
Vladimir Bauer
3 years ago
| 5 | 5 | // `fn` DecorFunc callback |
| 6 | 6 | // |
| 7 | 7 | // `wcc` optional WC config |
| 8 | // | |
| 9 | 8 | func Any(fn DecorFunc, wcc ...WC) Decorator { |
| 10 | 9 | return &any{initWC(wcc...), fn} |
| 11 | 10 | } |
| 41 | 41 | // pairFmt="% .1f / % .1f" output: "1.0 MB / 12.0 MB" |
| 42 | 42 | // pairFmt="%d / %d" output: "1MB / 12MB" |
| 43 | 43 | // pairFmt="% d / % d" output: "1 MB / 12 MB" |
| 44 | // | |
| 45 | 44 | func Counters(unit int, pairFmt string, wcc ...WC) Decorator { |
| 46 | 45 | producer := func(unit int, pairFmt string) DecorFunc { |
| 47 | 46 | if pairFmt == "" { |
| 98 | 97 | // format="% .1f" output: "12.0 MiB" |
| 99 | 98 | // format="%d" output: "12MiB" |
| 100 | 99 | // format="% d" output: "12 MiB" |
| 101 | // | |
| 102 | 100 | func Total(unit int, format string, wcc ...WC) Decorator { |
| 103 | 101 | producer := func(unit int, format string) DecorFunc { |
| 104 | 102 | if format == "" { |
| 156 | 154 | // format="% .1f" output: "12.0 MiB" |
| 157 | 155 | // format="%d" output: "12MiB" |
| 158 | 156 | // format="% d" output: "12 MiB" |
| 159 | // | |
| 160 | 157 | func Current(unit int, format string, wcc ...WC) Decorator { |
| 161 | 158 | producer := func(unit int, format string) DecorFunc { |
| 162 | 159 | if format == "" { |
| 214 | 211 | // format="% .1f" output: "12.0 MiB" |
| 215 | 212 | // format="%d" output: "12MiB" |
| 216 | 213 | // format="% d" output: "12 MiB" |
| 217 | // | |
| 218 | 214 | func InvertedCurrent(unit int, format string, wcc ...WC) Decorator { |
| 219 | 215 | producer := func(unit int, format string) DecorFunc { |
| 220 | 216 | if format == "" { |
| 5 | 5 | // |
| 6 | 6 | // Don't: |
| 7 | 7 | // |
| 8 | // p := mpb.New() | |
| 9 | // name := decor.Name("bar") | |
| 10 | // p.AddBar(100, mpb.AppendDecorators(name)) | |
| 11 | // p.AddBar(100, mpb.AppendDecorators(name)) | |
| 8 | // p := mpb.New() | |
| 9 | // name := decor.Name("bar") | |
| 10 | // p.AddBar(100, mpb.AppendDecorators(name)) | |
| 11 | // p.AddBar(100, mpb.AppendDecorators(name)) | |
| 12 | 12 | // |
| 13 | 13 | // Do: |
| 14 | 14 | // |
| 8 | 8 | // `style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS] |
| 9 | 9 | // |
| 10 | 10 | // `wcc` optional WC config |
| 11 | // | |
| 12 | 11 | func Elapsed(style TimeStyle, wcc ...WC) Decorator { |
| 13 | 12 | return NewElapsed(style, time.Now(), wcc...) |
| 14 | 13 | } |
| 20 | 19 | // `startTime` start time |
| 21 | 20 | // |
| 22 | 21 | // `wcc` optional WC config |
| 23 | // | |
| 24 | 22 | func NewElapsed(style TimeStyle, startTime time.Time, wcc ...WC) Decorator { |
| 25 | 23 | var msg string |
| 26 | 24 | producer := chooseTimeProducer(style) |
| 44 | 44 | // `normalizer` available implementations are [FixedIntervalTimeNormalizer|MaxTolerateTimeNormalizer] |
| 45 | 45 | // |
| 46 | 46 | // `wcc` optional WC config |
| 47 | // | |
| 48 | 47 | func MovingAverageETA(style TimeStyle, average ewma.MovingAverage, normalizer TimeNormalizer, wcc ...WC) Decorator { |
| 49 | 48 | d := &movingAverageETA{ |
| 50 | 49 | WC: initWC(wcc...), |
| 84 | 83 | // `style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS] |
| 85 | 84 | // |
| 86 | 85 | // `wcc` optional WC config |
| 87 | // | |
| 88 | 86 | func AverageETA(style TimeStyle, wcc ...WC) Decorator { |
| 89 | 87 | return NewAverageETA(style, time.Now(), nil, wcc...) |
| 90 | 88 | } |
| 98 | 96 | // `normalizer` available implementations are [FixedIntervalTimeNormalizer|MaxTolerateTimeNormalizer] |
| 99 | 97 | // |
| 100 | 98 | // `wcc` optional WC config |
| 101 | // | |
| 102 | 99 | func NewAverageETA(style TimeStyle, startTime time.Time, normalizer TimeNormalizer, wcc ...WC) Decorator { |
| 103 | 100 | d := &averageETA{ |
| 104 | 101 | WC: initWC(wcc...), |
| 9 | 9 | // Merge wraps its decorator argument with intention to sync width |
| 10 | 10 | // with several decorators of another bar. Visual example: |
| 11 | 11 | // |
| 12 | // +----+--------+---------+--------+ | |
| 13 | // | B1 | MERGE(D, P1, Pn) | | |
| 14 | // +----+--------+---------+--------+ | |
| 15 | // | B2 | D0 | D1 | Dn | | |
| 16 | // +----+--------+---------+--------+ | |
| 17 | // | |
| 12 | // +----+--------+---------+--------+ | |
| 13 | // | B1 | MERGE(D, P1, Pn) | | |
| 14 | // +----+--------+---------+--------+ | |
| 15 | // | B2 | D0 | D1 | Dn | | |
| 16 | // +----+--------+---------+--------+ | |
| 18 | 17 | func Merge(decorator Decorator, placeholders ...WC) Decorator { |
| 19 | 18 | if decorator == nil { |
| 20 | 19 | return nil |
| 5 | 5 | // `str` string to display |
| 6 | 6 | // |
| 7 | 7 | // `wcc` optional WC config |
| 8 | // | |
| 9 | 8 | func Name(str string, wcc ...WC) Decorator { |
| 10 | 9 | return Any(func(Statistics) string { return str }, wcc...) |
| 11 | 10 | } |
| 6 | 6 | // `decorator` Decorator to wrap |
| 7 | 7 | // |
| 8 | 8 | // `message` message to display on abort event |
| 9 | // | |
| 10 | 9 | func OnAbort(decorator Decorator, message string) Decorator { |
| 11 | 10 | if decorator == nil { |
| 12 | 11 | return nil |
| 5 | 5 | // `decorator` Decorator to wrap |
| 6 | 6 | // |
| 7 | 7 | // `message` message to display on complete event |
| 8 | // | |
| 9 | 8 | func OnComplete(decorator Decorator, message string) Decorator { |
| 10 | 9 | if decorator == nil { |
| 11 | 10 | return nil |
| 4 | 4 | // `decorator` Decorator |
| 5 | 5 | // |
| 6 | 6 | // `cond` bool |
| 7 | // | |
| 8 | 7 | func OnCondition(decorator Decorator, cond bool) Decorator { |
| 9 | 8 | return Conditional(cond, decorator, nil) |
| 10 | 9 | } |
| 14 | 13 | // `decorator` Decorator |
| 15 | 14 | // |
| 16 | 15 | // `predicate` func() bool |
| 17 | // | |
| 18 | 16 | func OnPredicate(decorator Decorator, predicate func() bool) Decorator { |
| 19 | 17 | return Predicative(predicate, decorator, nil) |
| 20 | 18 | } |
| 27 | 25 | // `a` Decorator |
| 28 | 26 | // |
| 29 | 27 | // `b` Decorator |
| 30 | // | |
| 31 | 28 | func Conditional(cond bool, a, b Decorator) Decorator { |
| 32 | 29 | if cond { |
| 33 | 30 | return a |
| 44 | 41 | // `a` Decorator |
| 45 | 42 | // |
| 46 | 43 | // `b` Decorator |
| 47 | // | |
| 48 | 44 | func Predicative(predicate func() bool, a, b Decorator) Decorator { |
| 49 | 45 | if predicate() { |
| 50 | 46 | return a |
| 42 | 42 | // format="% .1f" output: "1.0 %" |
| 43 | 43 | // format="%d" output: "1%" |
| 44 | 44 | // format="% d" output: "1 %" |
| 45 | // | |
| 46 | 45 | func NewPercentage(format string, wcc ...WC) Decorator { |
| 47 | 46 | if format == "" { |
| 48 | 47 | format = "% d" |
| 11 | 11 | // used with SizeB1000 or SizeB1024 types, for example: |
| 12 | 12 | // |
| 13 | 13 | // fmt.Printf("%.1f", FmtAsSpeed(SizeB1024(2048))) |
| 14 | // | |
| 15 | 14 | func FmtAsSpeed(input fmt.Formatter) fmt.Formatter { |
| 16 | 15 | return &speedFormatter{input} |
| 17 | 16 | } |
| 56 | 55 | // unit=UnitKiB, format="% .1f" output: "1.0 MiB/s" |
| 57 | 56 | // unit=UnitKB, format="%.1f" output: "1.0MB/s" |
| 58 | 57 | // unit=UnitKB, format="% .1f" output: "1.0 MB/s" |
| 59 | // | |
| 60 | 58 | func MovingAverageSpeed(unit int, format string, average ewma.MovingAverage, wcc ...WC) Decorator { |
| 61 | 59 | if format == "" { |
| 62 | 60 | format = "%.0f" |
| 118 | 116 | // unit=UnitKiB, format="% .1f" output: "1.0 MiB/s" |
| 119 | 117 | // unit=UnitKB, format="%.1f" output: "1.0MB/s" |
| 120 | 118 | // unit=UnitKB, format="% .1f" output: "1.0 MB/s" |
| 121 | // | |
| 122 | 119 | func NewAverageSpeed(unit int, format string, startTime time.Time, wcc ...WC) Decorator { |
| 123 | 120 | if format == "" { |
| 124 | 121 | format = "%.0f" |