|
1 | 1 | #pragma once
|
2 | 2 |
|
3 |
| -#include <sstream> |
| 3 | +#include <fmt/format.h> |
4 | 4 |
|
5 |
| -namespace waybar::util { |
| 5 | +class pow_format { |
| 6 | + public: |
| 7 | + pow_format(long long val, std::string&& unit, bool binary = false): |
| 8 | + val_(val), unit_(unit), binary_(binary) { }; |
6 | 9 |
|
7 |
| -std::string pow_format(unsigned long long value, const std::string &unit, bool binary = false); |
| 10 | + long long val_; |
| 11 | + std::string unit_; |
| 12 | + bool binary_; |
| 13 | +}; |
8 | 14 |
|
| 15 | + |
| 16 | +namespace fmt { |
| 17 | + template <> |
| 18 | + struct formatter<pow_format> { |
| 19 | + char spec = 0; |
| 20 | + int width = 0; |
| 21 | + |
| 22 | + template <typename ParseContext> |
| 23 | + constexpr auto parse(ParseContext& ctx) -> decltype (ctx.begin()) { |
| 24 | + auto it = ctx.begin(), end = ctx.end(); |
| 25 | + if (it != end && *it == ':') ++it; |
| 26 | + if (*it == '>' || *it == '<' || *it == '=') { |
| 27 | + spec = *it; |
| 28 | + ++it; |
| 29 | + } |
| 30 | + if (it == end || *it == '}') return it; |
| 31 | + if ('0' <= *it && *it <= '9') { |
| 32 | + // We ignore it for now, but keep it for compatibility with |
| 33 | + // existing configs where the format for pow_format'ed numbers was |
| 34 | + // 'string' and specifications such as {:>9} were valid. |
| 35 | + // The rationale for ignoring it is that the only reason to specify |
| 36 | + // an alignment and a with is to get a fixed width bar, and ">" is |
| 37 | + // sufficient in this implementation. |
| 38 | + width = parse_nonnegative_int(it, end, ctx); |
| 39 | + } |
| 40 | + return it; |
| 41 | + } |
| 42 | + |
| 43 | + template<class FormatContext> |
| 44 | + auto format(const pow_format& s, FormatContext &ctx) -> decltype (ctx.out()) { |
| 45 | + const char* units[] = { "", "k", "M", "G", "T", "P", nullptr}; |
| 46 | + |
| 47 | + auto base = s.binary_ ? 1024ull : 1000ll; |
| 48 | + auto fraction = (double) s.val_; |
| 49 | + |
| 50 | + int pow; |
| 51 | + for (pow = 0; units[pow+1] != nullptr && fraction / base >= 1; ++pow) { |
| 52 | + fraction /= base; |
| 53 | + } |
| 54 | + |
| 55 | + auto max_width = 4 // coeff in {:.3g} format |
| 56 | + + 1 // prefix from units array |
| 57 | + + s.binary_ // for the 'i' in GiB. |
| 58 | + + s.unit_.length(); |
| 59 | + |
| 60 | + const char * format; |
| 61 | + std::string string; |
| 62 | + switch (spec) { |
| 63 | + case '>': |
| 64 | + return format_to(ctx.out(), "{:>{}}", fmt::format("{}", s), max_width); |
| 65 | + case '<': |
| 66 | + return format_to(ctx.out(), "{:<{}}", fmt::format("{}", s), max_width); |
| 67 | + case '=': |
| 68 | + format = "{coefficient:<4.3g}{padding}{prefix}{unit}"; |
| 69 | + break; |
| 70 | + case 0: |
| 71 | + default: |
| 72 | + format = "{coefficient:.3g}{prefix}{unit}"; |
| 73 | + break; |
| 74 | + } |
| 75 | + return format_to(ctx.out(), format |
| 76 | + , fmt::arg("coefficient", fraction) |
| 77 | + , fmt::arg("prefix", std::string() + units[pow] + ((s.binary_ && pow) ? "i" : "")) |
| 78 | + , fmt::arg("unit", s.unit_) |
| 79 | + , fmt::arg("padding", pow ? "" : s.binary_ ? " " : " ") |
| 80 | + ); |
| 81 | + } |
| 82 | + }; |
9 | 83 | }
|
| 84 | + |
0 commit comments