2018-08-09 01:42:52 +02:00
|
|
|
#include "modules/memory.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
|
2018-08-09 13:30:11 +02:00
|
|
|
waybar::modules::Memory::Memory(Json::Value config)
|
|
|
|
: _config(config)
|
2018-08-09 01:42:52 +02:00
|
|
|
{
|
2018-08-09 10:50:16 +02:00
|
|
|
_label.get_style_context()->add_class("memory");
|
2018-08-09 01:42:52 +02:00
|
|
|
_thread = [this] {
|
2018-08-09 12:05:48 +02:00
|
|
|
update();
|
2018-08-09 14:34:39 +02:00
|
|
|
int interval = _config["interval"] ? _config["inveral"].asInt() : 30;
|
|
|
|
_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
|
|
|
|
{
|
|
|
|
struct sysinfo info;
|
|
|
|
if (!sysinfo(&info)) {
|
2018-08-09 13:30:11 +02:00
|
|
|
int available = ((double)info.freeram / (double)info.totalram) * 100;
|
|
|
|
auto format = _config["format"] ? _config["format"].asString() : "{}%";
|
|
|
|
_label.set_text(fmt::format(format, available));
|
2018-08-09 17:30:23 +02:00
|
|
|
auto used = (info.totalram - (info.totalram - info.freeram)) / std::pow(1024, 3);
|
|
|
|
_label.set_tooltip_text(fmt::format("{:.{}f}Gb used", used, 1));
|
2018-08-09 12:05:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 01:42:52 +02:00
|
|
|
waybar::modules::Memory::operator Gtk::Widget &() {
|
|
|
|
return _label;
|
|
|
|
}
|