waybar/src/modules/memory.cpp

31 lines
1.0 KiB
C++
Raw Normal View History

2018-08-09 01:42:52 +02:00
#include "modules/memory.hpp"
2018-08-09 13:30:11 +02:00
waybar::modules::Memory::Memory(Json::Value config)
2018-08-16 14:29:41 +02:00
: config_(std::move(config))
2018-08-09 01:42:52 +02:00
{
2018-08-16 14:29:41 +02:00
label_.set_name("memory");
uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 30;
thread_ = [this, interval] {
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Memory::update));
2018-08-16 14:29:41 +02:00
thread_.sleep_for(chrono::seconds(interval));
2018-08-09 01:42:52 +02:00
};
};
2018-08-09 12:05:48 +02:00
auto waybar::modules::Memory::update() -> void
{
2018-08-17 14:24:00 +02:00
struct sysinfo info = {0};
2018-08-16 14:29:41 +02:00
if (sysinfo(&info) == 0) {
2018-08-09 23:55:38 +02: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-16 14:29:41 +02:00
auto format = config_["format"] ? config_["format"].asString() : "{}%";
label_.set_text(fmt::format(format, used_ram_percentage));
2018-08-09 23:55:38 +02:00
auto used_ram_gigabytes = (total - freeram) / std::pow(1024, 3);
2018-08-16 14:29:41 +02:00
label_.set_tooltip_text(fmt::format("{:.{}f}Gb used", used_ram_gigabytes, 1));
2018-08-09 12:05:48 +02:00
}
}
2018-08-09 01:42:52 +02:00
waybar::modules::Memory::operator Gtk::Widget &() {
2018-08-16 14:29:41 +02:00
return label_;
2018-08-09 01:42:52 +02:00
}