2019-09-23 22:25:54 +02:00
|
|
|
#pragma once
|
|
|
|
|
2019-09-24 11:21:28 +02:00
|
|
|
#include <fmt/format.h>
|
2019-09-23 22:25:54 +02:00
|
|
|
|
2019-09-24 11:21:28 +02:00
|
|
|
class pow_format {
|
|
|
|
public:
|
|
|
|
pow_format(long long val, std::string&& unit, bool binary = false):
|
|
|
|
val_(val), unit_(unit), binary_(binary) { };
|
2019-09-23 22:25:54 +02:00
|
|
|
|
2019-09-24 11:21:28 +02:00
|
|
|
long long val_;
|
|
|
|
std::string unit_;
|
|
|
|
bool binary_;
|
|
|
|
};
|
2019-09-23 22:25:54 +02:00
|
|
|
|
2019-09-24 11:21:28 +02:00
|
|
|
|
|
|
|
namespace fmt {
|
|
|
|
template <>
|
|
|
|
struct formatter<pow_format> {
|
|
|
|
char spec = 0;
|
|
|
|
int width = 0;
|
|
|
|
|
|
|
|
template <typename ParseContext>
|
|
|
|
constexpr auto parse(ParseContext& ctx) -> decltype (ctx.begin()) {
|
|
|
|
auto it = ctx.begin(), end = ctx.end();
|
|
|
|
if (it != end && *it == ':') ++it;
|
2020-08-10 20:53:29 +02:00
|
|
|
if (it && (*it == '>' || *it == '<' || *it == '=')) {
|
2019-09-24 11:21:28 +02:00
|
|
|
spec = *it;
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
if (it == end || *it == '}') return it;
|
|
|
|
if ('0' <= *it && *it <= '9') {
|
|
|
|
// We ignore it for now, but keep it for compatibility with
|
|
|
|
// existing configs where the format for pow_format'ed numbers was
|
|
|
|
// 'string' and specifications such as {:>9} were valid.
|
|
|
|
// The rationale for ignoring it is that the only reason to specify
|
|
|
|
// an alignment and a with is to get a fixed width bar, and ">" is
|
|
|
|
// sufficient in this implementation.
|
2021-06-30 04:29:12 +02:00
|
|
|
#if FMT_VERSION < 80000
|
2019-09-24 11:21:28 +02:00
|
|
|
width = parse_nonnegative_int(it, end, ctx);
|
2021-06-30 04:29:12 +02:00
|
|
|
#else
|
|
|
|
width = detail::parse_nonnegative_int(it, end, -1);
|
|
|
|
#endif
|
2019-09-24 11:21:28 +02:00
|
|
|
}
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class FormatContext>
|
|
|
|
auto format(const pow_format& s, FormatContext &ctx) -> decltype (ctx.out()) {
|
2020-12-25 09:28:05 +01:00
|
|
|
const char* units[] = { "", "k", "M", "G", "T", "P", nullptr};
|
2019-09-24 11:21:28 +02:00
|
|
|
|
|
|
|
auto base = s.binary_ ? 1024ull : 1000ll;
|
|
|
|
auto fraction = (double) s.val_;
|
|
|
|
|
|
|
|
int pow;
|
|
|
|
for (pow = 0; units[pow+1] != nullptr && fraction / base >= 1; ++pow) {
|
|
|
|
fraction /= base;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto max_width = 4 // coeff in {:.3g} format
|
|
|
|
+ 1 // prefix from units array
|
|
|
|
+ s.binary_ // for the 'i' in GiB.
|
|
|
|
+ s.unit_.length();
|
|
|
|
|
|
|
|
const char * format;
|
|
|
|
std::string string;
|
|
|
|
switch (spec) {
|
|
|
|
case '>':
|
|
|
|
return format_to(ctx.out(), "{:>{}}", fmt::format("{}", s), max_width);
|
|
|
|
case '<':
|
|
|
|
return format_to(ctx.out(), "{:<{}}", fmt::format("{}", s), max_width);
|
|
|
|
case '=':
|
|
|
|
format = "{coefficient:<4.3g}{padding}{prefix}{unit}";
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
default:
|
|
|
|
format = "{coefficient:.3g}{prefix}{unit}";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return format_to(ctx.out(), format
|
|
|
|
, fmt::arg("coefficient", fraction)
|
|
|
|
, fmt::arg("prefix", std::string() + units[pow] + ((s.binary_ && pow) ? "i" : ""))
|
|
|
|
, fmt::arg("unit", s.unit_)
|
|
|
|
, fmt::arg("padding", pow ? "" : s.binary_ ? " " : " ")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2019-09-23 22:25:54 +02:00
|
|
|
}
|
2019-09-24 11:21:28 +02:00
|
|
|
|