-
-
Notifications
You must be signed in to change notification settings - Fork 725
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
custom formatter for numbers in 'pow' units format
- Loading branch information
Showing
4 changed files
with
90 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,70 @@ | ||
#pragma once | ||
|
||
#include <sstream> | ||
#include <fmt/format.h> | ||
|
||
namespace waybar::util { | ||
class pow_format { | ||
public: | ||
pow_format(long long val, std::string&& unit, bool binary = false): | ||
val_(val), unit_(unit), binary_(binary) { }; | ||
|
||
std::string pow_format(unsigned long long value, const std::string &unit, bool binary = false); | ||
long long val_; | ||
std::string unit_; | ||
bool binary_; | ||
}; | ||
|
||
|
||
namespace fmt { | ||
template <> | ||
struct formatter<pow_format> { | ||
char spec = 0; | ||
|
||
template <typename ParseContext> | ||
constexpr auto parse(ParseContext& ctx) -> decltype (ctx.begin()) { | ||
auto it = ctx.begin(); | ||
if (it != ctx.end() && *it == ':') ++it; | ||
if (*it == '>' || *it == '<' || *it == '=') { | ||
spec = *it; | ||
++it; | ||
} | ||
return it; | ||
} | ||
|
||
template<class FormatContext> | ||
auto format(const pow_format& s, FormatContext &ctx) -> decltype (ctx.out()) { | ||
const char* units[] = { "", "k", "M", "G", "T", "P", nullptr}; | ||
|
||
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; | ||
} | ||
|
||
const char * format; | ||
std::string string; | ||
switch (spec) { | ||
case '>': | ||
case '<': | ||
return format_to(ctx.out() | ||
, spec == '<' ? "{:<{}}" : "{:>{}}" | ||
, fmt::format("{}", s) | ||
, 4 /*coeff in :.3g format*/ + 1 /*prefix*/ + s.binary_ + s.unit_.length() | ||
); | ||
case '=': | ||
format = "{coefficient:<4.3g}{padding}{prefix}{unit}"; | ||
break; | ||
case 0: | ||
default: | ||
format = "{coefficient:.3g}{prefix}{unit}"; | ||
} | ||
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_ ? " " : " ") | ||
); | ||
} | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.