From 45bb8b1a1f8099592d505ed856b07e45d7349d2f Mon Sep 17 00:00:00 2001 From: Alexis Date: Fri, 9 Nov 2018 22:55:25 +0100 Subject: [PATCH] refactor: simpler memory code --- include/modules/memory.hpp | 1 + src/modules/memory.cpp | 69 +++++++++++++++++--------------------- 2 files changed, 32 insertions(+), 38 deletions(-) diff --git a/include/modules/memory.hpp b/include/modules/memory.hpp index 5301e93..0172f01 100644 --- a/include/modules/memory.hpp +++ b/include/modules/memory.hpp @@ -12,6 +12,7 @@ class Memory : public ALabel { Memory(const Json::Value&); auto update() -> void; private: + static inline const std::string data_dir_ = "/proc/meminfo"; unsigned long memtotal_; unsigned long memfree_; void parseMeminfo(); diff --git a/src/modules/memory.cpp b/src/modules/memory.cpp index 6de1eb3..38307ed 100644 --- a/src/modules/memory.cpp +++ b/src/modules/memory.cpp @@ -3,7 +3,6 @@ waybar::modules::Memory::Memory(const Json::Value& config) : ALabel(config, "{}%") { - label_.set_name("memory"); uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 30; thread_ = [this, interval] { dp.emit(); @@ -14,56 +13,50 @@ waybar::modules::Memory::Memory(const Json::Value& config) auto waybar::modules::Memory::update() -> void { parseMeminfo(); - if(memtotal_ > 0 && memfree_ >= 0) { + if (memtotal_ > 0 && memfree_ >= 0) { int used_ram_percentage = 100 * (memtotal_ - memfree_) / memtotal_; label_.set_text(fmt::format(format_, used_ram_percentage)); 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_name("memory"); label_.show(); } else { + label_.set_name(""); label_.hide(); } } void waybar::modules::Memory::parseMeminfo() { - long memtotal = -1, memfree = -1, membuffer = -1, memcache = -1, memavail = -1; - int count = 0; + long memfree = -1, membuffer = -1, memcache = -1, memavail = -1; + std::ifstream info(data_dir_); + if (!info.is_open()) { + throw std::runtime_error("Can't open " + data_dir_); + } std::string line; - std::ifstream info("/proc/meminfo"); - if(info.is_open()) { - while(getline(info, line)) { - auto posDelim = line.find(":"); - std::string name = line.substr(0, posDelim); - long value = std::stol(line.substr(posDelim + 1)); - - if(name.compare("MemTotal") == 0) { - memtotal = value; - count++; - } else if(name.compare("MemAvailable") == 0) { - memavail = value; - count++; - } else if(name.compare("MemFree") == 0) { - memfree = value; - count++; - } else if(name.compare("Buffers") == 0) { - membuffer = value; - count++; - } else if(name.compare("Cached") == 0) { - memcache = value; - count++; - } - if (count >= 5 || (count >= 4 && memavail >= -1)) { - info.close(); - } + while (getline(info, line)) { + auto posDelim = line.find(":"); + if (posDelim == std::string::npos) { + continue; + } + std::string name = line.substr(0, posDelim); + long value = std::stol(line.substr(posDelim + 1)); + + if (name.compare("MemTotal") == 0) { + memtotal_ = value; + } else if (name.compare("MemAvailable") == 0) { + memavail = value; + } else if (name.compare("MemFree") == 0) { + memfree = value; + } else if (name.compare("Buffers") == 0) { + membuffer = value; + } else if (name.compare("Cached") == 0) { + memcache = value; + } + if (memtotal_ > 0 && + (memavail >= 0 || (memfree > -1 && membuffer > -1 && memcache > -1))) { + break; } - } 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; }