refactoring counters: unit is either [0|SizeB1024(0)|SizeB1000(0)]
Vladimir Bauer
3 years ago
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | // CountersKibiByte is a wrapper around Counters with predefined unit |
| 19 | // UnitKiB (bytes/1024). | |
| 19 | // as SizeB1024(0). | |
| 20 | 20 | func CountersKibiByte(pairFmt string, wcc ...WC) Decorator { |
| 21 | return Counters(UnitKiB, pairFmt, wcc...) | |
| 21 | return Counters(SizeB1024(0), pairFmt, wcc...) | |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | // CountersKiloByte is a wrapper around Counters with predefined unit |
| 25 | // UnitKB (bytes/1000). | |
| 25 | // as SizeB1000(0). | |
| 26 | 26 | func CountersKiloByte(pairFmt string, wcc ...WC) Decorator { |
| 27 | return Counters(UnitKB, pairFmt, wcc...) | |
| 27 | return Counters(SizeB1000(0), pairFmt, wcc...) | |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | // Counters decorator with dynamic unit measure adjustment. |
| 31 | 31 | // |
| 32 | // `unit` one of [0|UnitKiB|UnitKB] zero for no unit | |
| 33 | // | |
| 34 | // `pairFmt` printf compatible verbs for current and total pair | |
| 35 | // | |
| 36 | // `wcc` optional WC config | |
| 37 | // | |
| 38 | // pairFmt example if unit=UnitKB: | |
| 39 | // | |
| 32 | // `unit` one of [0|SizeB1024(0)|SizeB1000(0)] | |
| 33 | // | |
| 34 | // `pairFmt` printf compatible verbs for current and total | |
| 35 | // | |
| 36 | // `wcc` optional WC config | |
| 37 | // | |
| 38 | // pairFmt example if unit=SizeB1000(0): | |
| 39 | // | |
| 40 | // pairFmt="%d / %d" output: "1MB / 12MB" | |
| 41 | // pairFmt="% d / % d" output: "1 MB / 12 MB" | |
| 40 | 42 | // pairFmt="%.1f / %.1f" output: "1.0MB / 12.0MB" |
| 41 | 43 | // pairFmt="% .1f / % .1f" output: "1.0 MB / 12.0 MB" |
| 42 | // pairFmt="%d / %d" output: "1MB / 12MB" | |
| 43 | // pairFmt="% d / % d" output: "1 MB / 12 MB" | |
| 44 | func Counters(unit int, pairFmt string, wcc ...WC) Decorator { | |
| 45 | producer := func(unit int, pairFmt string) DecorFunc { | |
| 46 | if pairFmt == "" { | |
| 47 | pairFmt = "%d / %d" | |
| 48 | } else if strings.Count(pairFmt, "%") != 2 { | |
| 49 | panic("expected pairFmt with exactly 2 verbs") | |
| 50 | } | |
| 51 | switch unit { | |
| 52 | case UnitKiB: | |
| 44 | // pairFmt="%f / %f" output: "1.000000MB / 12.000000MB" | |
| 45 | // pairFmt="% f / % f" output: "1.000000 MB / 12.000000 MB" | |
| 46 | func Counters(unit interface{}, pairFmt string, wcc ...WC) Decorator { | |
| 47 | if pairFmt == "" { | |
| 48 | pairFmt = "%d / %d" | |
| 49 | } else if strings.Count(pairFmt, "%") != 2 { | |
| 50 | panic("expected pairFmt with exactly 2 verbs") | |
| 51 | } | |
| 52 | producer := func() DecorFunc { | |
| 53 | switch unit.(type) { | |
| 54 | case SizeB1024: | |
| 53 | 55 | return func(s Statistics) string { |
| 54 | 56 | return fmt.Sprintf(pairFmt, SizeB1024(s.Current), SizeB1024(s.Total)) |
| 55 | 57 | } |
| 56 | case UnitKB: | |
| 58 | case SizeB1000: | |
| 57 | 59 | return func(s Statistics) string { |
| 58 | 60 | return fmt.Sprintf(pairFmt, SizeB1000(s.Current), SizeB1000(s.Total)) |
| 59 | 61 | } |
| 63 | 65 | } |
| 64 | 66 | } |
| 65 | 67 | } |
| 66 | return Any(producer(unit, pairFmt), wcc...) | |
| 68 | return Any(producer(), wcc...) | |
| 67 | 69 | } |
| 68 | 70 | |
| 69 | 71 | // TotalNoUnit is a wrapper around Total with no unit param. |
| 72 | 74 | } |
| 73 | 75 | |
| 74 | 76 | // TotalKibiByte is a wrapper around Total with predefined unit |
| 75 | // UnitKiB (bytes/1024). | |
| 77 | // as SizeB1024(0). | |
| 76 | 78 | func TotalKibiByte(format string, wcc ...WC) Decorator { |
| 77 | return Total(UnitKiB, format, wcc...) | |
| 79 | return Total(SizeB1024(0), format, wcc...) | |
| 78 | 80 | } |
| 79 | 81 | |
| 80 | 82 | // TotalKiloByte is a wrapper around Total with predefined unit |
| 81 | // UnitKB (bytes/1000). | |
| 83 | // as SizeB1000(0). | |
| 82 | 84 | func TotalKiloByte(format string, wcc ...WC) Decorator { |
| 83 | return Total(UnitKB, format, wcc...) | |
| 85 | return Total(SizeB1000(0), format, wcc...) | |
| 84 | 86 | } |
| 85 | 87 | |
| 86 | 88 | // Total decorator with dynamic unit measure adjustment. |
| 87 | 89 | // |
| 88 | // `unit` one of [0|UnitKiB|UnitKB] zero for no unit | |
| 90 | // `unit` one of [0|SizeB1024(0)|SizeB1000(0)] | |
| 89 | 91 | // |
| 90 | 92 | // `format` printf compatible verb for Total |
| 91 | 93 | // |
| 92 | 94 | // `wcc` optional WC config |
| 93 | 95 | // |
| 94 | // format example if unit=UnitKiB: | |
| 95 | // | |
| 96 | // format example if unit=SizeB1024(0): | |
| 97 | // | |
| 98 | // format="%d" output: "12MiB" | |
| 99 | // format="% d" output: "12 MiB" | |
| 96 | 100 | // format="%.1f" output: "12.0MiB" |
| 97 | 101 | // format="% .1f" output: "12.0 MiB" |
| 98 | // format="%d" output: "12MiB" | |
| 99 | // format="% d" output: "12 MiB" | |
| 100 | func Total(unit int, format string, wcc ...WC) Decorator { | |
| 101 | producer := func(unit int, format string) DecorFunc { | |
| 102 | if format == "" { | |
| 103 | format = "%d" | |
| 104 | } else if strings.Count(format, "%") != 1 { | |
| 105 | panic("expected format with exactly 1 verb") | |
| 106 | } | |
| 107 | ||
| 108 | switch unit { | |
| 109 | case UnitKiB: | |
| 102 | // format="%f" output: "12.000000MiB" | |
| 103 | // format="% f" output: "12.000000 MiB" | |
| 104 | func Total(unit interface{}, format string, wcc ...WC) Decorator { | |
| 105 | if format == "" { | |
| 106 | format = "%d" | |
| 107 | } else if strings.Count(format, "%") != 1 { | |
| 108 | panic("expected format with exactly 1 verb") | |
| 109 | } | |
| 110 | producer := func() DecorFunc { | |
| 111 | switch unit.(type) { | |
| 112 | case SizeB1024: | |
| 110 | 113 | return func(s Statistics) string { |
| 111 | 114 | return fmt.Sprintf(format, SizeB1024(s.Total)) |
| 112 | 115 | } |
| 113 | case UnitKB: | |
| 116 | case SizeB1000: | |
| 114 | 117 | return func(s Statistics) string { |
| 115 | 118 | return fmt.Sprintf(format, SizeB1000(s.Total)) |
| 116 | 119 | } |
| 120 | 123 | } |
| 121 | 124 | } |
| 122 | 125 | } |
| 123 | return Any(producer(unit, format), wcc...) | |
| 126 | return Any(producer(), wcc...) | |
| 124 | 127 | } |
| 125 | 128 | |
| 126 | 129 | // CurrentNoUnit is a wrapper around Current with no unit param. |
| 129 | 132 | } |
| 130 | 133 | |
| 131 | 134 | // CurrentKibiByte is a wrapper around Current with predefined unit |
| 132 | // UnitKiB (bytes/1024). | |
| 135 | // as SizeB1024(0). | |
| 133 | 136 | func CurrentKibiByte(format string, wcc ...WC) Decorator { |
| 134 | return Current(UnitKiB, format, wcc...) | |
| 137 | return Current(SizeB1024(0), format, wcc...) | |
| 135 | 138 | } |
| 136 | 139 | |
| 137 | 140 | // CurrentKiloByte is a wrapper around Current with predefined unit |
| 138 | // UnitKB (bytes/1000). | |
| 141 | // as SizeB1000(0). | |
| 139 | 142 | func CurrentKiloByte(format string, wcc ...WC) Decorator { |
| 140 | return Current(UnitKB, format, wcc...) | |
| 143 | return Current(SizeB1000(0), format, wcc...) | |
| 141 | 144 | } |
| 142 | 145 | |
| 143 | 146 | // Current decorator with dynamic unit measure adjustment. |
| 144 | 147 | // |
| 145 | // `unit` one of [0|UnitKiB|UnitKB] zero for no unit | |
| 148 | // `unit` one of [0|SizeB1024(0)|SizeB1000(0)] | |
| 146 | 149 | // |
| 147 | 150 | // `format` printf compatible verb for Current |
| 148 | 151 | // |
| 149 | 152 | // `wcc` optional WC config |
| 150 | 153 | // |
| 151 | // format example if unit=UnitKiB: | |
| 152 | // | |
| 154 | // format example if unit=SizeB1024(0): | |
| 155 | // | |
| 156 | // format="%d" output: "12MiB" | |
| 157 | // format="% d" output: "12 MiB" | |
| 153 | 158 | // format="%.1f" output: "12.0MiB" |
| 154 | 159 | // format="% .1f" output: "12.0 MiB" |
| 155 | // format="%d" output: "12MiB" | |
| 156 | // format="% d" output: "12 MiB" | |
| 157 | func Current(unit int, format string, wcc ...WC) Decorator { | |
| 158 | producer := func(unit int, format string) DecorFunc { | |
| 159 | if format == "" { | |
| 160 | format = "%d" | |
| 161 | } else if strings.Count(format, "%") != 1 { | |
| 162 | panic("expected format with exactly 1 verb") | |
| 163 | } | |
| 164 | ||
| 165 | switch unit { | |
| 166 | case UnitKiB: | |
| 160 | // format="%f" output: "12.000000MiB" | |
| 161 | // format="% f" output: "12.000000 MiB" | |
| 162 | func Current(unit interface{}, format string, wcc ...WC) Decorator { | |
| 163 | if format == "" { | |
| 164 | format = "%d" | |
| 165 | } else if strings.Count(format, "%") != 1 { | |
| 166 | panic("expected format with exactly 1 verb") | |
| 167 | } | |
| 168 | producer := func() DecorFunc { | |
| 169 | switch unit.(type) { | |
| 170 | case SizeB1024: | |
| 167 | 171 | return func(s Statistics) string { |
| 168 | 172 | return fmt.Sprintf(format, SizeB1024(s.Current)) |
| 169 | 173 | } |
| 170 | case UnitKB: | |
| 174 | case SizeB1000: | |
| 171 | 175 | return func(s Statistics) string { |
| 172 | 176 | return fmt.Sprintf(format, SizeB1000(s.Current)) |
| 173 | 177 | } |
| 177 | 181 | } |
| 178 | 182 | } |
| 179 | 183 | } |
| 180 | return Any(producer(unit, format), wcc...) | |
| 184 | return Any(producer(), wcc...) | |
| 181 | 185 | } |
| 182 | 186 | |
| 183 | 187 | // InvertedCurrentNoUnit is a wrapper around InvertedCurrent with no unit param. |
| 186 | 190 | } |
| 187 | 191 | |
| 188 | 192 | // InvertedCurrentKibiByte is a wrapper around InvertedCurrent with predefined unit |
| 189 | // UnitKiB (bytes/1024). | |
| 193 | // as SizeB1024(0). | |
| 190 | 194 | func InvertedCurrentKibiByte(format string, wcc ...WC) Decorator { |
| 191 | return InvertedCurrent(UnitKiB, format, wcc...) | |
| 195 | return InvertedCurrent(SizeB1024(0), format, wcc...) | |
| 192 | 196 | } |
| 193 | 197 | |
| 194 | 198 | // InvertedCurrentKiloByte is a wrapper around InvertedCurrent with predefined unit |
| 195 | // UnitKB (bytes/1000). | |
| 199 | // as SizeB1000(0). | |
| 196 | 200 | func InvertedCurrentKiloByte(format string, wcc ...WC) Decorator { |
| 197 | return InvertedCurrent(UnitKB, format, wcc...) | |
| 201 | return InvertedCurrent(SizeB1000(0), format, wcc...) | |
| 198 | 202 | } |
| 199 | 203 | |
| 200 | 204 | // InvertedCurrent decorator with dynamic unit measure adjustment. |
| 201 | 205 | // |
| 202 | // `unit` one of [0|UnitKiB|UnitKB] zero for no unit | |
| 206 | // `unit` one of [0|SizeB1024(0)|SizeB1000(0)] | |
| 203 | 207 | // |
| 204 | 208 | // `format` printf compatible verb for InvertedCurrent |
| 205 | 209 | // |
| 206 | 210 | // `wcc` optional WC config |
| 207 | 211 | // |
| 208 | // format example if unit=UnitKiB: | |
| 209 | // | |
| 212 | // format example if unit=SizeB1024(0): | |
| 213 | // | |
| 214 | // format="%d" output: "12MiB" | |
| 215 | // format="% d" output: "12 MiB" | |
| 210 | 216 | // format="%.1f" output: "12.0MiB" |
| 211 | 217 | // format="% .1f" output: "12.0 MiB" |
| 212 | // format="%d" output: "12MiB" | |
| 213 | // format="% d" output: "12 MiB" | |
| 214 | func InvertedCurrent(unit int, format string, wcc ...WC) Decorator { | |
| 215 | producer := func(unit int, format string) DecorFunc { | |
| 216 | if format == "" { | |
| 217 | format = "%d" | |
| 218 | } else if strings.Count(format, "%") != 1 { | |
| 219 | panic("expected format with exactly 1 verb") | |
| 220 | } | |
| 221 | ||
| 222 | switch unit { | |
| 223 | case UnitKiB: | |
| 218 | // format="%f" output: "12.000000MiB" | |
| 219 | // format="% f" output: "12.000000 MiB" | |
| 220 | func InvertedCurrent(unit interface{}, format string, wcc ...WC) Decorator { | |
| 221 | if format == "" { | |
| 222 | format = "%d" | |
| 223 | } else if strings.Count(format, "%") != 1 { | |
| 224 | panic("expected format with exactly 1 verb") | |
| 225 | } | |
| 226 | producer := func() DecorFunc { | |
| 227 | switch unit.(type) { | |
| 228 | case SizeB1024: | |
| 224 | 229 | return func(s Statistics) string { |
| 225 | 230 | return fmt.Sprintf(format, SizeB1024(s.Total-s.Current)) |
| 226 | 231 | } |
| 227 | case UnitKB: | |
| 232 | case SizeB1000: | |
| 228 | 233 | return func(s Statistics) string { |
| 229 | 234 | return fmt.Sprintf(format, SizeB1000(s.Total-s.Current)) |
| 230 | 235 | } |
| 234 | 239 | } |
| 235 | 240 | } |
| 236 | 241 | } |
| 237 | return Any(producer(unit, format), wcc...) | |
| 238 | } | |
| 242 | return Any(producer(), wcc...) | |
| 243 | } | |