2016-12-13 17:24:01 +00:00
|
|
|
#include <fstream>
|
2016-12-26 08:40:15 +00:00
|
|
|
#include <iomanip>
|
2016-12-31 03:32:11 +00:00
|
|
|
#include <istream>
|
2016-12-13 17:24:01 +00:00
|
|
|
|
2016-11-19 14:49:03 +00:00
|
|
|
#include "drawtypes/label.hpp"
|
|
|
|
#include "drawtypes/progressbar.hpp"
|
2017-01-12 19:28:44 +00:00
|
|
|
#include "modules/memory.hpp"
|
|
|
|
#include "utils/math.hpp"
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-11-20 22:04:31 +00:00
|
|
|
#include "modules/meta/base.inl"
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
namespace modules {
|
2016-11-20 22:04:31 +00:00
|
|
|
template class module<memory_module>;
|
|
|
|
|
2016-12-21 07:00:09 +00:00
|
|
|
memory_module::memory_module(const bar_settings& bar, string name_) : timer_module<memory_module>(bar, move(name_)) {
|
2016-12-31 02:04:01 +00:00
|
|
|
m_interval = m_conf.get<decltype(m_interval)>(name(), "interval", 1s);
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL, TAG_BAR_USED, TAG_BAR_FREE});
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
if (m_formatter->has(TAG_BAR_USED)) {
|
2017-01-12 19:28:44 +00:00
|
|
|
m_bar_memused = load_progressbar(m_bar, m_conf, name(), TAG_BAR_USED);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (m_formatter->has(TAG_BAR_FREE)) {
|
2017-01-12 19:28:44 +00:00
|
|
|
m_bar_memfree = load_progressbar(m_bar, m_conf, name(), TAG_BAR_FREE);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (m_formatter->has(TAG_LABEL)) {
|
2017-01-13 19:03:08 +00:00
|
|
|
m_label = load_optional_label(m_conf, name(), TAG_LABEL, "%percentage_used%%");
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool memory_module::update() {
|
2017-01-12 19:28:44 +00:00
|
|
|
unsigned long long kb_total{0ULL};
|
|
|
|
unsigned long long kb_avail{0ULL};
|
|
|
|
unsigned long long kb_free{0ULL};
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
std::ifstream in(PATH_MEMORY_INFO);
|
|
|
|
std::stringstream buffer;
|
2017-01-12 19:28:44 +00:00
|
|
|
string str;
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
buffer.imbue(std::locale::classic());
|
|
|
|
|
2017-01-12 19:28:44 +00:00
|
|
|
for (int i = 3; i > 0 && std::getline(in, str); i--) {
|
2016-11-02 19:22:45 +00:00
|
|
|
size_t off = str.find_first_of("1234567890", str.find(':'));
|
2017-01-12 19:25:57 +00:00
|
|
|
if (off != string::npos && str.size() > off) {
|
2017-01-12 19:28:44 +00:00
|
|
|
buffer << std::strtoull(&str[off], nullptr, 10) << std::endl;
|
2017-01-12 19:25:57 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 19:28:44 +00:00
|
|
|
buffer >> kb_total;
|
|
|
|
buffer >> kb_free;
|
|
|
|
buffer >> kb_avail;
|
|
|
|
} catch (const std::exception& err) {
|
|
|
|
m_log.err("Failed to read memory values (what: %s)", err.what());
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 19:28:44 +00:00
|
|
|
m_perc_memfree = math_util::percentage(kb_avail, kb_total);
|
|
|
|
m_perc_memused = 100 - m_perc_memfree;
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
// replace tokens
|
|
|
|
if (m_label) {
|
|
|
|
m_label->reset_tokens();
|
2017-01-12 19:28:44 +00:00
|
|
|
m_label->replace_token("%gb_used%", string_util::filesize_gb(kb_total - kb_avail, 2, m_bar.locale));
|
|
|
|
m_label->replace_token("%gb_free%", string_util::filesize_gb(kb_avail, 2, m_bar.locale));
|
|
|
|
m_label->replace_token("%gb_total%", string_util::filesize_gb(kb_total, 2, m_bar.locale));
|
|
|
|
m_label->replace_token("%mb_used%", string_util::filesize_mb(kb_total - kb_avail, 2, m_bar.locale));
|
|
|
|
m_label->replace_token("%mb_free%", string_util::filesize_mb(kb_avail, 2, m_bar.locale));
|
|
|
|
m_label->replace_token("%mb_total%", string_util::filesize_mb(kb_total, 2, m_bar.locale));
|
2017-01-13 19:03:08 +00:00
|
|
|
m_label->replace_token("%percentage_used%", to_string(m_perc_memused));
|
|
|
|
m_label->replace_token("%percentage_free%", to_string(m_perc_memfree));
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
bool memory_module::build(builder* builder, const string& tag) const {
|
|
|
|
if (tag == TAG_BAR_USED) {
|
2017-01-12 19:28:44 +00:00
|
|
|
builder->node(m_bar_memused->output(m_perc_memused));
|
2016-11-25 12:55:15 +00:00
|
|
|
} else if (tag == TAG_BAR_FREE) {
|
2017-01-12 19:28:44 +00:00
|
|
|
builder->node(m_bar_memfree->output(m_perc_memfree));
|
2016-11-25 12:55:15 +00:00
|
|
|
} else if (tag == TAG_LABEL) {
|
2016-11-02 19:22:45 +00:00
|
|
|
builder->node(m_label);
|
2016-11-25 12:55:15 +00:00
|
|
|
} else {
|
2016-11-02 19:22:45 +00:00
|
|
|
return false;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS_END
|