2018-08-08 23:42:52 +00:00
|
|
|
#include "modules/memory.hpp"
|
|
|
|
|
2018-08-09 11:30:11 +00:00
|
|
|
waybar::modules::Memory::Memory(Json::Value config)
|
|
|
|
: _config(config)
|
2018-08-08 23:42:52 +00:00
|
|
|
{
|
2018-08-09 08:50:16 +00:00
|
|
|
_label.get_style_context()->add_class("memory");
|
2018-08-08 23:42:52 +00:00
|
|
|
_thread = [this] {
|
2018-08-09 10:05:48 +00:00
|
|
|
update();
|
2018-08-09 12:34:39 +00:00
|
|
|
int interval = _config["interval"] ? _config["inveral"].asInt() : 30;
|
|
|
|
_thread.sleep_for(chrono::seconds(interval));
|
2018-08-08 23:42:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-08-09 10:05:48 +00:00
|
|
|
auto waybar::modules::Memory::update() -> void
|
|
|
|
{
|
|
|
|
struct sysinfo info;
|
|
|
|
if (!sysinfo(&info)) {
|
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-09 11:30:11 +00:00
|
|
|
auto format = _config["format"] ? _config["format"].asString() : "{}%";
|
2018-08-09 19:47:28 +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-09 19:47:28 +00:00
|
|
|
_label.set_tooltip_text(fmt::format("{:.{}f}Gb used", used_ram_gigabytes, 1));
|
2018-08-09 10:05:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 23:42:52 +00:00
|
|
|
waybar::modules::Memory::operator Gtk::Widget &() {
|
|
|
|
return _label;
|
|
|
|
}
|