Use unit in config for memory module tooltip
This commit is contained in:
@@ -19,8 +19,6 @@ class Memory : public ALabel {
|
|||||||
private:
|
private:
|
||||||
void parseMeminfo();
|
void parseMeminfo();
|
||||||
|
|
||||||
static float calc_divisor(const std::string& divisor);
|
|
||||||
|
|
||||||
std::unordered_map<std::string, unsigned long> meminfo_;
|
std::unordered_map<std::string, unsigned long> meminfo_;
|
||||||
|
|
||||||
util::SleeperThread thread_;
|
util::SleeperThread thread_;
|
||||||
|
|||||||
@@ -1,5 +1,18 @@
|
|||||||
#include "modules/memory.hpp"
|
#include "modules/memory.hpp"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
const std::unordered_map<std::string, float> kUnits = {
|
||||||
|
{"kB", 1.000},
|
||||||
|
{"kiB", 1.024},
|
||||||
|
{"MB", 1.000 * 1000.0},
|
||||||
|
{"MiB", 1.024 * 1024.0},
|
||||||
|
{"GB", 1.000 * 1000.0 * 1000.0},
|
||||||
|
{"GiB", 1.024 * 1024.0 * 1024.0},
|
||||||
|
{"TB", 1.000 * 1000.0 * 1000.0 * 1000.0},
|
||||||
|
{"TiB", 1.024 * 1024.0 * 1024.0 * 1024.0}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
waybar::modules::Memory::Memory(const std::string& id, const Json::Value& config)
|
waybar::modules::Memory::Memory(const std::string& id, const Json::Value& config)
|
||||||
: ALabel(config, "memory", id, "{}%", 30) {
|
: ALabel(config, "memory", id, "{}%", 30) {
|
||||||
thread_ = [this] {
|
thread_ = [this] {
|
||||||
@@ -8,6 +21,11 @@ waybar::modules::Memory::Memory(const std::string& id, const Json::Value& config
|
|||||||
};
|
};
|
||||||
if (config["unit"].isString()) {
|
if (config["unit"].isString()) {
|
||||||
unit_ = config["unit"].asString();
|
unit_ = config["unit"].asString();
|
||||||
|
if (!kUnits.contains(unit_)) {
|
||||||
|
unit_ = "GiB";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
unit_ = "GiB";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +58,7 @@ auto waybar::modules::Memory::update() -> void {
|
|||||||
used_swap_percentage = 100 * (swaptotal - swapfree) / swaptotal;
|
used_swap_percentage = 100 * (swaptotal - swapfree) / swaptotal;
|
||||||
}
|
}
|
||||||
|
|
||||||
float divisor = calc_divisor(unit_);
|
float divisor = kUnits.at(unit_);
|
||||||
float total_ram = memtotal / divisor;
|
float total_ram = memtotal / divisor;
|
||||||
float total_swap = swaptotal / divisor;
|
float total_swap = swaptotal / divisor;
|
||||||
float used_ram = (memtotal - memfree) / divisor;
|
float used_ram = (memtotal - memfree) / divisor;
|
||||||
@@ -82,7 +100,7 @@ auto waybar::modules::Memory::update() -> void {
|
|||||||
fmt::arg("swapUsed", used_swap), fmt::arg("avail", available_ram),
|
fmt::arg("swapUsed", used_swap), fmt::arg("avail", available_ram),
|
||||||
fmt::arg("swapAvail", available_swap)));
|
fmt::arg("swapAvail", available_swap)));
|
||||||
} else {
|
} else {
|
||||||
label_.set_tooltip_markup(fmt::format("{:.{}f}GiB used", used_ram, 1));
|
label_.set_tooltip_markup(fmt::format("{:.{}f}{} used", used_ram, 1, unit_));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -91,25 +109,3 @@ auto waybar::modules::Memory::update() -> void {
|
|||||||
// Call parent update
|
// Call parent update
|
||||||
ALabel::update();
|
ALabel::update();
|
||||||
}
|
}
|
||||||
|
|
||||||
float waybar::modules::Memory::calc_divisor(const std::string& divisor) {
|
|
||||||
if (divisor == "kB") {
|
|
||||||
return 1.0;
|
|
||||||
} else if (divisor == "kiB") {
|
|
||||||
return 1.024;
|
|
||||||
} else if (divisor == "MB") {
|
|
||||||
return 1.000 * 1000.0;
|
|
||||||
} else if (divisor == "MiB") {
|
|
||||||
return 1.024 * 1024.0;
|
|
||||||
} else if (divisor == "GB") {
|
|
||||||
return 1.000 * 1000.0 * 1000.0;
|
|
||||||
} else if (divisor == "GiB") {
|
|
||||||
return 1.024 * 1024.0 * 1024.0;
|
|
||||||
} else if (divisor == "TB") {
|
|
||||||
return 1.000 * 1000.0 * 1000.0 * 1000.0;
|
|
||||||
} else if (divisor == "TiB") {
|
|
||||||
return 1.024 * 1024.0 * 1024.0 * 1024.0;
|
|
||||||
} else { // default to GiB if it is anything that we don't recongnise
|
|
||||||
return 1.024 * 1024.0 * 1024.0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user