counters producer
Vladimir Bauer
6 years ago
| 46 | 46 | for _, widthConf := range wcc { |
| 47 | 47 | wc = widthConf |
| 48 | 48 | } |
| 49 | if pairFmt == "" { | |
| 50 | pairFmt = "%d / %d" | |
| 51 | } | |
| 52 | 49 | d := &countersDecorator{ |
| 53 | WC: wc.Init(), | |
| 54 | unit: unit, | |
| 55 | pairFmt: pairFmt, | |
| 50 | WC: wc.Init(), | |
| 51 | producer: chooseSizeProducer(unit, pairFmt), | |
| 56 | 52 | } |
| 57 | 53 | return d |
| 58 | 54 | } |
| 59 | 55 | |
| 60 | 56 | type countersDecorator struct { |
| 61 | 57 | WC |
| 62 | unit int | |
| 63 | pairFmt string | |
| 58 | producer func(*Statistics) string | |
| 64 | 59 | } |
| 65 | 60 | |
| 66 | 61 | func (d *countersDecorator) Decor(st *Statistics) string { |
| 67 | var res string | |
| 68 | switch d.unit { | |
| 62 | return d.producer(st) | |
| 63 | } | |
| 64 | ||
| 65 | func chooseSizeProducer(unit int, format string) func(*Statistics) string { | |
| 66 | if format == "" { | |
| 67 | format = "%d / %d" | |
| 68 | } | |
| 69 | switch unit { | |
| 69 | 70 | case UnitKiB: |
| 70 | res = fmt.Sprintf(d.pairFmt, SizeB1024(st.Current), SizeB1024(st.Total)) | |
| 71 | return func(st *Statistics) string { | |
| 72 | return fmt.Sprintf(format, SizeB1024(st.Current), SizeB1024(st.Total)) | |
| 73 | } | |
| 71 | 74 | case UnitKB: |
| 72 | res = fmt.Sprintf(d.pairFmt, SizeB1000(st.Current), SizeB1000(st.Total)) | |
| 75 | return func(st *Statistics) string { | |
| 76 | return fmt.Sprintf(format, SizeB1000(st.Current), SizeB1000(st.Total)) | |
| 77 | } | |
| 73 | 78 | default: |
| 74 | res = fmt.Sprintf(d.pairFmt, st.Current, st.Total) | |
| 79 | return func(st *Statistics) string { | |
| 80 | return fmt.Sprintf(format, st.Current, st.Total) | |
| 81 | } | |
| 75 | 82 | } |
| 76 | ||
| 77 | return d.FormatMsg(res) | |
| 78 | 83 | } |