2016-12-13 12:24:01 -05:00
|
|
|
#include <fstream>
|
2016-12-14 21:30:41 -05:00
|
|
|
#include <istream>
|
2016-12-13 12:24:01 -05:00
|
|
|
|
2016-11-02 15:22:45 -04:00
|
|
|
#include "modules/memory.hpp"
|
2016-11-20 17:04:31 -05:00
|
|
|
|
2016-11-19 09:49:03 -05:00
|
|
|
#include "drawtypes/label.hpp"
|
|
|
|
#include "drawtypes/progressbar.hpp"
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-11-20 17:04:31 -05:00
|
|
|
#include "modules/meta/base.inl"
|
|
|
|
#include "modules/meta/timer_module.inl"
|
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS
|
2016-11-02 15:22:45 -04:00
|
|
|
|
|
|
|
namespace modules {
|
2016-11-20 17:04:31 -05:00
|
|
|
template class module<memory_module>;
|
|
|
|
template class timer_module<memory_module>;
|
|
|
|
|
2016-12-21 02:00:09 -05:00
|
|
|
memory_module::memory_module(const bar_settings& bar, string name_) : timer_module<memory_module>(bar, move(name_)) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_interval = chrono::duration<double>(m_conf.get<float>(name(), "interval", 1));
|
|
|
|
|
|
|
|
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL, TAG_BAR_USED, TAG_BAR_FREE});
|
|
|
|
|
2016-11-25 07:55:15 -05:00
|
|
|
if (m_formatter->has(TAG_BAR_USED)) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_bars[memtype::USED] = load_progressbar(m_bar, m_conf, name(), TAG_BAR_USED);
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
|
|
|
if (m_formatter->has(TAG_BAR_FREE)) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_bars[memtype::FREE] = load_progressbar(m_bar, m_conf, name(), TAG_BAR_FREE);
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
|
|
|
if (m_formatter->has(TAG_LABEL)) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_label = load_optional_label(m_conf, name(), TAG_LABEL, "%percentage_used%");
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool memory_module::update() {
|
|
|
|
float kb_total;
|
|
|
|
float kb_avail;
|
|
|
|
// long kb_free;
|
|
|
|
|
|
|
|
try {
|
|
|
|
std::ifstream in(PATH_MEMORY_INFO);
|
|
|
|
std::stringstream buffer;
|
|
|
|
string str, rdbuf;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
in.exceptions(in.failbit);
|
|
|
|
|
|
|
|
buffer.imbue(std::locale::classic());
|
|
|
|
|
|
|
|
while (std::getline(in, str) && i++ < 3) {
|
|
|
|
size_t off = str.find_first_of("1234567890", str.find(':'));
|
2016-11-25 07:55:15 -05:00
|
|
|
buffer << std::strtol(&str[off], nullptr, 10) << std::endl;
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer >> rdbuf;
|
|
|
|
kb_total = std::atol(rdbuf.c_str());
|
|
|
|
buffer >> rdbuf; // kb_free = std::atol(rdbuf.c_str());
|
|
|
|
buffer >> rdbuf;
|
|
|
|
kb_avail = std::atol(rdbuf.c_str());
|
|
|
|
} catch (const std::ios_base::failure& e) {
|
|
|
|
kb_total = 0;
|
|
|
|
// kb_free = 0;
|
|
|
|
kb_avail = 0;
|
|
|
|
m_log.err("Failed to read memory values (what: %s)", e.what());
|
|
|
|
}
|
|
|
|
|
2016-11-25 07:55:15 -05:00
|
|
|
if (kb_total > 0) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_perc[memtype::FREE] = (kb_avail / kb_total) * 100.0f + 0.5f;
|
2016-11-25 07:55:15 -05:00
|
|
|
} else {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_perc[memtype::FREE] = 0;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
|
|
|
|
m_perc[memtype::USED] = 100 - m_perc[memtype::FREE];
|
|
|
|
|
|
|
|
// replace tokens
|
|
|
|
if (m_label) {
|
|
|
|
m_label->reset_tokens();
|
|
|
|
|
|
|
|
auto replace_unit = [](label_t& label, string token, float value, string unit) {
|
2016-11-19 09:49:03 -05:00
|
|
|
auto formatted =
|
|
|
|
string_util::from_stream(stringstream() << std::setprecision(2) << std::fixed << value << " " << unit);
|
2016-11-02 15:22:45 -04:00
|
|
|
label->replace_token(token, formatted);
|
|
|
|
};
|
|
|
|
|
|
|
|
replace_unit(m_label, "%gb_used%", (kb_total - kb_avail) / 1024 / 1024, "GB");
|
|
|
|
replace_unit(m_label, "%gb_free%", kb_avail / 1024 / 1024, "GB");
|
|
|
|
replace_unit(m_label, "%gb_total%", kb_total / 1024 / 1024, "GB");
|
|
|
|
replace_unit(m_label, "%mb_used%", (kb_total - kb_avail) / 1024, "MB");
|
|
|
|
replace_unit(m_label, "%mb_free%", kb_avail / 1024, "MB");
|
|
|
|
replace_unit(m_label, "%mb_total%", kb_total / 1024, "MB");
|
|
|
|
|
|
|
|
m_label->replace_token("%percentage_used%", to_string(m_perc[memtype::USED]) + "%");
|
|
|
|
m_label->replace_token("%percentage_free%", to_string(m_perc[memtype::FREE]) + "%");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-25 07:55:15 -05:00
|
|
|
bool memory_module::build(builder* builder, const string& tag) const {
|
|
|
|
if (tag == TAG_BAR_USED) {
|
2016-11-02 15:22:45 -04:00
|
|
|
builder->node(m_bars.at(memtype::USED)->output(m_perc.at(memtype::USED)));
|
2016-11-25 07:55:15 -05:00
|
|
|
} else if (tag == TAG_BAR_FREE) {
|
2016-11-02 15:22:45 -04:00
|
|
|
builder->node(m_bars.at(memtype::FREE)->output(m_perc.at(memtype::FREE)));
|
2016-11-25 07:55:15 -05:00
|
|
|
} else if (tag == TAG_LABEL) {
|
2016-11-02 15:22:45 -04:00
|
|
|
builder->node(m_label);
|
2016-11-25 07:55:15 -05:00
|
|
|
} else {
|
2016-11-02 15:22:45 -04:00
|
|
|
return false;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS_END
|