custom formatter for numbers in 'pow' units format

This commit is contained in:
Guillaume Maudoux
2019-09-24 11:21:28 +02:00
parent 211b1c2785
commit f4d2ca2736
5 changed files with 105 additions and 39 deletions

View File

@ -44,15 +44,33 @@ auto waybar::modules::Disk::update() -> void {
return;
}
label_.set_markup(fmt::format(format_,
stats.f_bavail * 100 / stats.f_blocks,
fmt::arg("free", pow_format(stats.f_bavail * stats.f_bsize, "B", true)),
fmt::arg("percentage_free", stats.f_bavail * 100 / stats.f_blocks),
fmt::arg("used", pow_format((stats.f_blocks - stats.f_bavail) * stats.f_bsize, "B", true)),
fmt::arg("percentage_used", (stats.f_blocks - stats.f_bavail) * 100 / stats.f_blocks)
));
auto free = pow_format(stats.f_bavail * stats.f_bsize, "B", true);
auto used = pow_format((stats.f_blocks - stats.f_bavail) * stats.f_bsize, "B", true);
auto total = pow_format(stats.f_blocks * stats.f_bsize, "B", true);
label_.set_markup(fmt::format(format_
, stats.f_bavail * 100 / stats.f_blocks
, fmt::arg("free", free)
, fmt::arg("percentage_free", stats.f_bavail * 100 / stats.f_blocks)
, fmt::arg("used", used)
, fmt::arg("percentage_used", (stats.f_blocks - stats.f_bavail) * 100 / stats.f_blocks)
, fmt::arg("total", total)
, fmt::arg("path", path_)
));
if (tooltipEnabled()) {
label_.set_tooltip_text(fmt::format("{} used", pow_format(stats.f_bavail * stats.f_bsize, "B", true)));
std::string tooltip_format = "{used} used out of {total} on {path} ({percentage_used}%)";
if (config_["tooltip-format"].isString()) {
tooltip_format = config_["tooltip-format"].asString();
}
label_.set_tooltip_text(fmt::format(tooltip_format
, stats.f_bavail * 100 / stats.f_blocks
, fmt::arg("free", free)
, fmt::arg("percentage_free", stats.f_bavail * 100 / stats.f_blocks)
, fmt::arg("used", used)
, fmt::arg("percentage_used", (stats.f_blocks - stats.f_bavail) * 100 / stats.f_blocks)
, fmt::arg("total", total)
, fmt::arg("path", path_)
));
}
event_box_.show();
}

View File

@ -515,7 +515,7 @@ bool waybar::modules::Network::checkInterface(struct ifinfomsg *rtif, std::strin
return false;
}
int waybar::modules::Network::getPreferredIface(int skip_idx, bool wait = true) const {
int waybar::modules::Network::getPreferredIface(int skip_idx, bool wait) const {
int ifid = -1;
if (config_["interface"].isString()) {
ifid = if_nametoindex(config_["interface"].asCString());

View File

@ -1,26 +0,0 @@
#include <sstream>
namespace waybar::util {
std::string pow_format(unsigned long long value, const std::string &unit, bool binary = false) {
auto base = binary ? 1024ull : 1000ull;
const char* units[] = { "", "k", "M", "G", "T", "P", nullptr};
auto fraction = (double) value;
int pow;
for (pow = 0; units[pow+1] != nullptr && fraction / base >= 2; ++pow) {
fraction /= base;
}
std::ostringstream ss;
if (pow > 0) {
auto quotient = (unsigned long long) fraction;
auto remainder = (unsigned long long) ((fraction - quotient) * 10);
ss << quotient << "." << remainder << units[pow] << (binary ? "i" : "") << unit;
} else {
ss << value << unit;
}
return ss.str();
};
}