refactor: simpler memory code

This commit is contained in:
Alexis 2018-11-09 22:55:25 +01:00
parent a5bca24f9b
commit 45bb8b1a1f
2 changed files with 32 additions and 38 deletions

View File

@ -12,6 +12,7 @@ class Memory : public ALabel {
Memory(const Json::Value&); Memory(const Json::Value&);
auto update() -> void; auto update() -> void;
private: private:
static inline const std::string data_dir_ = "/proc/meminfo";
unsigned long memtotal_; unsigned long memtotal_;
unsigned long memfree_; unsigned long memfree_;
void parseMeminfo(); void parseMeminfo();

View File

@ -3,7 +3,6 @@
waybar::modules::Memory::Memory(const Json::Value& config) waybar::modules::Memory::Memory(const Json::Value& config)
: ALabel(config, "{}%") : ALabel(config, "{}%")
{ {
label_.set_name("memory");
uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 30; uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 30;
thread_ = [this, interval] { thread_ = [this, interval] {
dp.emit(); dp.emit();
@ -14,56 +13,50 @@ waybar::modules::Memory::Memory(const Json::Value& config)
auto waybar::modules::Memory::update() -> void auto waybar::modules::Memory::update() -> void
{ {
parseMeminfo(); parseMeminfo();
if(memtotal_ > 0 && memfree_ >= 0) { if (memtotal_ > 0 && memfree_ >= 0) {
int used_ram_percentage = 100 * (memtotal_ - memfree_) / memtotal_; int used_ram_percentage = 100 * (memtotal_ - memfree_) / memtotal_;
label_.set_text(fmt::format(format_, used_ram_percentage)); label_.set_text(fmt::format(format_, used_ram_percentage));
auto used_ram_gigabytes = (memtotal_ - memfree_) / std::pow(1024, 2); auto used_ram_gigabytes = (memtotal_ - memfree_) / std::pow(1024, 2);
label_.set_tooltip_text(fmt::format("{:.{}f}Gb used", used_ram_gigabytes, 1)); label_.set_tooltip_text(fmt::format("{:.{}f}Gb used", used_ram_gigabytes, 1));
label_.set_name("memory");
label_.show(); label_.show();
} else { } else {
label_.set_name("");
label_.hide(); label_.hide();
} }
} }
void waybar::modules::Memory::parseMeminfo() void waybar::modules::Memory::parseMeminfo()
{ {
long memtotal = -1, memfree = -1, membuffer = -1, memcache = -1, memavail = -1; long memfree = -1, membuffer = -1, memcache = -1, memavail = -1;
int count = 0; std::ifstream info(data_dir_);
if (!info.is_open()) {
throw std::runtime_error("Can't open " + data_dir_);
}
std::string line; std::string line;
std::ifstream info("/proc/meminfo"); while (getline(info, line)) {
if(info.is_open()) { auto posDelim = line.find(":");
while(getline(info, line)) { if (posDelim == std::string::npos) {
auto posDelim = line.find(":"); continue;
std::string name = line.substr(0, posDelim); }
long value = std::stol(line.substr(posDelim + 1)); std::string name = line.substr(0, posDelim);
long value = std::stol(line.substr(posDelim + 1));
if(name.compare("MemTotal") == 0) {
memtotal = value; if (name.compare("MemTotal") == 0) {
count++; memtotal_ = value;
} else if(name.compare("MemAvailable") == 0) { } else if (name.compare("MemAvailable") == 0) {
memavail = value; memavail = value;
count++; } else if (name.compare("MemFree") == 0) {
} else if(name.compare("MemFree") == 0) { memfree = value;
memfree = value; } else if (name.compare("Buffers") == 0) {
count++; membuffer = value;
} else if(name.compare("Buffers") == 0) { } else if (name.compare("Cached") == 0) {
membuffer = value; memcache = value;
count++; }
} else if(name.compare("Cached") == 0) { if (memtotal_ > 0 &&
memcache = value; (memavail >= 0 || (memfree > -1 && membuffer > -1 && memcache > -1))) {
count++; break;
}
if (count >= 5 || (count >= 4 && memavail >= -1)) {
info.close();
}
} }
} else {
throw std::runtime_error("Can't open /proc/meminfo");
}
memtotal_ = memtotal;
if(memavail >= 0) {
memfree_ = memavail;
} else {
memfree_ = memfree + (membuffer + memcache);
} }
memfree_ = memavail >= 0 ? memavail : memfree + membuffer + memcache;
} }