Codebase list golang-github-vbauerster-mpb / 4820193
refactoring: use Any Vladimir Bauer 6 years ago
9 changed file(s) with 66 addition(s) and 152 deletion(s). Raw diff Collapse all Expand all
4242 // pairFmt="% d / % d" output: "1 MB / 12 MB"
4343 //
4444 func Counters(unit int, pairFmt string, wcc ...WC) Decorator {
45 var wc WC
46 for _, widthConf := range wcc {
47 wc = widthConf
48 }
49 d := &countersDecorator{
50 WC: wc.Init(),
51 producer: chooseSizeProducer(unit, pairFmt),
52 }
53 return d
54 }
55
56 type countersDecorator struct {
57 WC
58 producer func(*Statistics) string
59 }
60
61 func (d *countersDecorator) Decor(st *Statistics) string {
62 return d.FormatMsg(d.producer(st))
45 return Any(chooseSizeProducer(unit, pairFmt), wcc...)
6346 }
6447
6548 func chooseSizeProducer(unit int, format string) func(*Statistics) string {
6851 }
6952 switch unit {
7053 case UnitKiB:
71 return func(st *Statistics) string {
72 return fmt.Sprintf(format, SizeB1024(st.Current), SizeB1024(st.Total))
54 return func(s *Statistics) string {
55 return fmt.Sprintf(format, SizeB1024(s.Current), SizeB1024(s.Total))
7356 }
7457 case UnitKB:
75 return func(st *Statistics) string {
76 return fmt.Sprintf(format, SizeB1000(st.Current), SizeB1000(st.Total))
58 return func(s *Statistics) string {
59 return fmt.Sprintf(format, SizeB1000(s.Current), SizeB1000(s.Total))
7760 }
7861 default:
79 return func(st *Statistics) string {
80 return fmt.Sprintf(format, st.Current, st.Total)
62 return func(s *Statistics) string {
63 return fmt.Sprintf(format, s.Current, s.Total)
8164 }
8265 }
8366 }
88 // `style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS]
99 //
1010 // `wcc` optional WC config
11 //
1112 func Elapsed(style TimeStyle, wcc ...WC) Decorator {
1213 return NewElapsed(style, time.Now(), wcc...)
1314 }
1920 // `startTime` start time
2021 //
2122 // `wcc` optional WC config
23 //
2224 func NewElapsed(style TimeStyle, startTime time.Time, wcc ...WC) Decorator {
23 var wc WC
24 for _, widthConf := range wcc {
25 wc = widthConf
25 var msg string
26 producer := chooseTimeProducer(style)
27 f := func(s *Statistics) string {
28 if !s.Completed {
29 msg = producer(time.Since(startTime))
30 }
31 return msg
2632 }
27 d := &elapsedDecorator{
28 WC: wc.Init(),
29 startTime: startTime,
30 producer: chooseTimeProducer(style),
31 }
32 return d
33 return Any(f, wcc...)
3334 }
34
35 type elapsedDecorator struct {
36 WC
37 startTime time.Time
38 producer func(time.Duration) string
39 msg string
40 }
41
42 func (d *elapsedDecorator) Decor(st *Statistics) string {
43 if !st.Completed {
44 d.msg = d.producer(time.Since(d.startTime))
45 }
46 return d.FormatMsg(d.msg)
47 }
4444 // `normalizer` available implementations are [FixedIntervalTimeNormalizer|MaxTolerateTimeNormalizer]
4545 //
4646 // `wcc` optional WC config
47 //
4748 func MovingAverageETA(style TimeStyle, average MovingAverage, normalizer TimeNormalizer, wcc ...WC) Decorator {
48 var wc WC
49 for _, widthConf := range wcc {
50 wc = widthConf
51 }
5249 d := &movingAverageETA{
53 WC: wc.Init(),
50 WC: initWC(wcc...),
5451 average: average,
5552 normalizer: normalizer,
5653 producer: chooseTimeProducer(style),
6562 producer func(time.Duration) string
6663 }
6764
68 func (d *movingAverageETA) Decor(st *Statistics) string {
65 func (d *movingAverageETA) Decor(s *Statistics) string {
6966 v := math.Round(d.average.Value())
70 remaining := time.Duration((st.Total - st.Current) * int64(v))
67 remaining := time.Duration((s.Total - s.Current) * int64(v))
7168 if d.normalizer != nil {
7269 remaining = d.normalizer.Normalize(remaining)
7370 }
9188 // `style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS]
9289 //
9390 // `wcc` optional WC config
91 //
9492 func AverageETA(style TimeStyle, wcc ...WC) Decorator {
9593 return NewAverageETA(style, time.Now(), nil, wcc...)
9694 }
104102 // `normalizer` available implementations are [FixedIntervalTimeNormalizer|MaxTolerateTimeNormalizer]
105103 //
106104 // `wcc` optional WC config
105 //
107106 func NewAverageETA(style TimeStyle, startTime time.Time, normalizer TimeNormalizer, wcc ...WC) Decorator {
108 var wc WC
109 for _, widthConf := range wcc {
110 wc = widthConf
111 }
112107 d := &averageETA{
113 WC: wc.Init(),
108 WC: initWC(wcc...),
114109 startTime: startTime,
115110 normalizer: normalizer,
116111 producer: chooseTimeProducer(style),
125120 producer func(time.Duration) string
126121 }
127122
128 func (d *averageETA) Decor(st *Statistics) string {
123 func (d *averageETA) Decor(s *Statistics) string {
129124 var remaining time.Duration
130 if st.Current != 0 {
131 durPerItem := float64(time.Since(d.startTime)) / float64(st.Current)
125 if s.Current != 0 {
126 durPerItem := float64(time.Since(d.startTime)) / float64(s.Current)
132127 durPerItem = math.Round(durPerItem)
133 remaining = time.Duration((st.Total - st.Current) * int64(durPerItem))
128 remaining = time.Duration((s.Total - s.Current) * int64(durPerItem))
134129 if d.normalizer != nil {
135130 remaining = d.normalizer.Normalize(remaining)
136131 }
6363 return d.Decorator
6464 }
6565
66 func (d *mergeDecorator) Decor(st *Statistics) string {
67 msg := d.Decorator.Decor(st)
66 func (d *mergeDecorator) Decor(s *Statistics) string {
67 msg := d.Decorator.Decor(s)
6868 msgLen := utf8.RuneCountInString(msg)
6969 if (d.wc.C & DextraSpace) != 0 {
7070 msgLen++
100100 WC
101101 }
102102
103 func (d *placeHolderDecorator) Decor(_ *Statistics) string {
103 func (d *placeHolderDecorator) Decor(*Statistics) string {
104104 return ""
105105 }
00 package decor
11
2 // Name returns name decorator.
2 // Name decorator displays text that is set once and can't be changed
3 // during decorator's lifetime.
34 //
4 // `name` string to display
5 // `str` string to display
56 //
67 // `wcc` optional WC config
7 func Name(name string, wcc ...WC) Decorator {
8 var wc WC
9 for _, widthConf := range wcc {
10 wc = widthConf
11 }
12 d := &nameDecorator{
13 WC: wc.Init(),
14 msg: name,
15 }
16 return d
8 //
9 func Name(str string, wcc ...WC) Decorator {
10 return Any(func(*Statistics) string { return str }, wcc...)
1711 }
18
19 type nameDecorator struct {
20 WC
21 msg string
22 }
23
24 func (d *nameDecorator) Decor(st *Statistics) string {
25 return d.FormatMsg(d.msg)
26 }
2222 msg string
2323 }
2424
25 func (d *onCompleteWrapper) Decor(st *Statistics) string {
26 if st.Completed {
25 func (d *onCompleteWrapper) Decor(s *Statistics) string {
26 if s.Completed {
2727 wc := d.GetConf()
2828 return wc.FormatMsg(d.msg)
2929 }
30 return d.Decorator.Decor(st)
30 return d.Decorator.Decor(s)
3131 }
3232
3333 func (d *onCompleteWrapper) Base() Decorator {
3636 return NewPercentage("% d", wcc...)
3737 }
3838
39 // NewPercentage percentage decorator with custom fmt string.
39 // NewPercentage percentage decorator with custom format string.
4040 //
41 // fmt examples:
41 // format examples:
4242 //
43 // fmt="%.1f" output: "1.0%"
44 // fmt="% .1f" output: "1.0 %"
45 // fmt="%d" output: "1%"
46 // fmt="% d" output: "1 %"
43 // format="%.1f" output: "1.0%"
44 // format="% .1f" output: "1.0 %"
45 // format="%d" output: "1%"
46 // format="% d" output: "1 %"
4747 //
48 func NewPercentage(fmt string, wcc ...WC) Decorator {
49 var wc WC
50 for _, widthConf := range wcc {
51 wc = widthConf
48 func NewPercentage(format string, wcc ...WC) Decorator {
49 if format == "" {
50 format = "% d"
5251 }
53 if fmt == "" {
54 fmt = "% d"
52 f := func(s *Statistics) string {
53 p := internal.Percentage(s.Total, s.Current, 100)
54 return fmt.Sprintf(format, percentageType(p))
5555 }
56 d := &percentageDecorator{
57 WC: wc.Init(),
58 fmt: fmt,
59 }
60 return d
56 return Any(f, wcc...)
6157 }
62
63 type percentageDecorator struct {
64 WC
65 fmt string
66 }
67
68 func (d *percentageDecorator) Decor(st *Statistics) string {
69 p := internal.Percentage(st.Total, st.Current, 100)
70 return d.FormatMsg(fmt.Sprintf(d.fmt, percentageType(p)))
71 }
5151 // unit=UnitKB, format="% .1f" output: "1.0 MB/s"
5252 //
5353 func MovingAverageSpeed(unit int, format string, average MovingAverage, wcc ...WC) Decorator {
54 var wc WC
55 for _, widthConf := range wcc {
56 wc = widthConf
57 }
5854 if format == "" {
5955 format = "%.0f"
6056 }
6157 d := &movingAverageSpeed{
62 WC: wc.Init(),
58 WC: initWC(wcc...),
6359 average: average,
6460 producer: chooseSpeedProducer(unit, format),
6561 }
7369 msg string
7470 }
7571
76 func (d *movingAverageSpeed) Decor(st *Statistics) string {
77 if !st.Completed {
72 func (d *movingAverageSpeed) Decor(s *Statistics) string {
73 if !s.Completed {
7874 var speed float64
7975 if v := d.average.Value(); v > 0 {
8076 speed = 1 / v
121117 // unit=UnitKB, format="% .1f" output: "1.0 MB/s"
122118 //
123119 func NewAverageSpeed(unit int, format string, startTime time.Time, wcc ...WC) Decorator {
124 var wc WC
125 for _, widthConf := range wcc {
126 wc = widthConf
127 }
128120 if format == "" {
129121 format = "%.0f"
130122 }
131123 d := &averageSpeed{
132 WC: wc.Init(),
124 WC: initWC(wcc...),
133125 startTime: startTime,
134126 producer: chooseSpeedProducer(unit, format),
135127 }
143135 msg string
144136 }
145137
146 func (d *averageSpeed) Decor(st *Statistics) string {
147 if !st.Completed {
148 speed := float64(st.Current) / float64(time.Since(d.startTime))
138 func (d *averageSpeed) Decor(s *Statistics) string {
139 if !s.Completed {
140 speed := float64(s.Current) / float64(time.Since(d.startTime))
149141 d.msg = d.producer(speed * 1e9)
150142 }
151143
77 //
88 // `wcc` optional WC config
99 func Spinner(frames []string, wcc ...WC) Decorator {
10 var wc WC
11 for _, widthConf := range wcc {
12 wc = widthConf
13 }
1410 if len(frames) == 0 {
1511 frames = defaultSpinnerStyle
1612 }
17 d := &spinnerDecorator{
18 WC: wc.Init(),
19 frames: frames,
13 var count uint
14 f := func(s *Statistics) string {
15 frame := frames[count%uint(len(frames))]
16 count++
17 return frame
2018 }
21 return d
19 return Any(f, wcc...)
2220 }
23
24 type spinnerDecorator struct {
25 WC
26 frames []string
27 count uint
28 }
29
30 func (d *spinnerDecorator) Decor(st *Statistics) string {
31 frame := d.frames[d.count%uint(len(d.frames))]
32 d.count++
33 return d.FormatMsg(frame)
34 }