Adding options to allow disk outputs to always be in a specific unit

pull/2558/head
SWarrener 2023-10-08 16:24:50 +01:00
parent 58e506a675
commit c16e791cdc
2 changed files with 39 additions and 2 deletions

View File

@ -20,6 +20,9 @@ class Disk : public ALabel {
private:
util::SleeperThread thread_;
std::string path_;
std::string unit_;
float calc_specific_divisor(const std::string divisor);
};
} // namespace waybar::modules

View File

@ -11,6 +11,9 @@ waybar::modules::Disk::Disk(const std::string& id, const Json::Value& config)
if (config["path"].isString()) {
path_ = config["path"].asString();
}
if (config["unit"].isString()) {
unit_ = config["unit"].asString();
}
}
auto waybar::modules::Disk::update() -> void {
@ -43,6 +46,13 @@ auto waybar::modules::Disk::update() -> void {
return;
}
float specific_free, specific_used, specific_total, divisor;
divisor = calc_specific_divisor(unit_);
specific_free = (stats.f_bavail * stats.f_frsize)/divisor;
specific_used = ((stats.f_blocks - stats.f_bfree) * stats.f_frsize)/divisor;
specific_total = (stats.f_blocks * stats.f_frsize)/divisor;
auto free = pow_format(stats.f_bavail * stats.f_frsize, "B", true);
auto used = pow_format((stats.f_blocks - stats.f_bfree) * stats.f_frsize, "B", true);
auto total = pow_format(stats.f_blocks * stats.f_frsize, "B", true);
@ -62,7 +72,8 @@ auto waybar::modules::Disk::update() -> void {
fmt::runtime(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", percentage_used), fmt::arg("total", total),
fmt::arg("path", path_)));
fmt::arg("path", path_), fmt::arg("specific_free", specific_free),
fmt::arg("specific_used", specific_used), fmt::arg("specific_total", specific_total)));
}
if (tooltipEnabled()) {
@ -74,8 +85,31 @@ auto waybar::modules::Disk::update() -> void {
fmt::runtime(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", percentage_used), fmt::arg("total", total),
fmt::arg("path", path_)));
fmt::arg("path", path_), fmt::arg("specific_free", specific_free),
fmt::arg("specific_used", specific_used), fmt::arg("specific_total", specific_total)));
}
// Call parent update
ALabel::update();
}
float waybar::modules::Disk::calc_specific_divisor(std::string divisor) {
if (divisor == "kB") {
return 1000.0;
} else if (divisor == "kiB") {
return 1024.0;
} else if (divisor == "MB") {
return 1000.0 * 1000.0;
} else if (divisor == "MiB") {
return 1024.0 * 1024.0;
} else if (divisor == "GB") {
return 1000.0 * 1000.0 * 1000.0;
} else if (divisor == "GiB") {
return 1024.0 * 1024.0 * 1024.0;
} else if (divisor == "TB") {
return 1000.0 * 1000.0 * 1000.0 * 1000.0;
} else if (divisor == "TiB") {
return 1024.0 * 1024.0 * 1024.0 * 1024.0;
} else { //default to Bytes if it is anything that we don't recongnise
return 1.0;
}
}