Waybar/src/modules/memory.cpp

26 lines
827 B
C++
Raw Normal View History

2018-08-08 23:42:52 +00:00
#include "modules/memory.hpp"
2018-08-20 12:50:45 +00:00
waybar::modules::Memory::Memory(const Json::Value& config)
2018-08-26 23:36:25 +00:00
: ALabel(config, "{}%")
2018-08-08 23:42:52 +00:00
{
2018-08-16 12:29:41 +00:00
label_.set_name("memory");
2018-10-26 07:27:16 +00:00
uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 30;
2018-08-16 12:29:41 +00:00
thread_ = [this, interval] {
2018-08-20 12:50:45 +00:00
dp.emit();
2018-08-16 12:29:41 +00:00
thread_.sleep_for(chrono::seconds(interval));
2018-08-08 23:42:52 +00:00
};
}
2018-08-08 23:42:52 +00:00
2018-08-09 10:05:48 +00:00
auto waybar::modules::Memory::update() -> void
{
2018-09-10 09:16:57 +00:00
struct sysinfo info = {};
2018-08-16 12:29:41 +00:00
if (sysinfo(&info) == 0) {
2018-08-09 21:55:38 +00:00
auto total = info.totalram * info.mem_unit;
auto freeram = info.freeram * info.mem_unit;
int used_ram_percentage = 100 * (total - freeram) / total;
2018-08-26 23:36:25 +00:00
label_.set_text(fmt::format(format_, used_ram_percentage));
2018-08-09 21:55:38 +00:00
auto used_ram_gigabytes = (total - freeram) / std::pow(1024, 3);
2018-08-16 12:29:41 +00:00
label_.set_tooltip_text(fmt::format("{:.{}f}Gb used", used_ram_gigabytes, 1));
2018-08-09 10:05:48 +00:00
}
}