mirror of
https://github.com/polybar/polybar.git
synced 2026-03-11 02:35:06 +00:00
feat(temperature): Support for <ramp>
This commit is contained in:
@ -1,4 +1,6 @@
|
||||
#include "modules/temperature.hpp"
|
||||
#include "utils/file.hpp"
|
||||
#include "utils/math.hpp"
|
||||
|
||||
LEMONBUDDY_NS
|
||||
|
||||
@ -13,34 +15,35 @@ namespace modules {
|
||||
if (!file_util::exists(m_path))
|
||||
throw module_error("The file '" + m_path + "' does not exist");
|
||||
|
||||
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL});
|
||||
m_formatter->add(FORMAT_WARN, TAG_LABEL_WARN, {TAG_LABEL_WARN});
|
||||
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL, TAG_RAMP});
|
||||
m_formatter->add(FORMAT_WARN, TAG_LABEL_WARN, {TAG_LABEL_WARN, TAG_RAMP});
|
||||
|
||||
if (m_formatter->has(TAG_LABEL))
|
||||
m_label[temp_state::NORMAL] = load_optional_label(m_conf, name(), TAG_LABEL, "%temperature%");
|
||||
if (m_formatter->has(TAG_LABEL_WARN))
|
||||
m_label[temp_state::WARN] = load_optional_label(m_conf, name(), TAG_LABEL_WARN, "%temperature%");
|
||||
if (m_formatter->has(TAG_RAMP))
|
||||
m_ramp = load_ramp(m_conf, name(), TAG_RAMP);
|
||||
}
|
||||
|
||||
bool temperature_module::update() {
|
||||
|
||||
m_temp = std::atoi(file_util::get_contents(m_path).c_str()) / 1000.0f + 0.5f;
|
||||
|
||||
// replace tokens
|
||||
const auto replace_tokens = [&](label_t& label) {
|
||||
label->reset_tokens();
|
||||
label->replace_token("%temperature%", to_string(m_temp) + "°C");
|
||||
};
|
||||
if (m_label[temp_state::NORMAL]) {
|
||||
|
||||
if (m_label[temp_state::NORMAL])
|
||||
replace_tokens(m_label[temp_state::NORMAL]);
|
||||
}
|
||||
if (m_label[temp_state::WARN]) {
|
||||
if (m_label[temp_state::WARN])
|
||||
replace_tokens(m_label[temp_state::WARN]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
string temperature_module::get_format() const {
|
||||
if(m_temp > m_tempwarn)
|
||||
if (m_temp > m_tempwarn)
|
||||
return FORMAT_WARN;
|
||||
else
|
||||
return DEFAULT_FORMAT;
|
||||
@ -51,6 +54,8 @@ namespace modules {
|
||||
builder->node(m_label.at(temp_state::NORMAL));
|
||||
else if (tag == TAG_LABEL_WARN)
|
||||
builder->node(m_label.at(temp_state::WARN));
|
||||
else if (tag == TAG_RAMP)
|
||||
builder->node(m_ramp->get_by_percentage(math_util::cap(m_temp, 0, m_tempwarn)));
|
||||
else
|
||||
return false;
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user