1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-12-09 14:10:19 -05:00

feat(battery): add %percentage_raw% token (#1756)

Displays real percentage instead of being set to 100 if percentage > full-at

* battery: added percentage_raw token, which ignores full-at

* battery: current_percentage returns raw, added clamping function instead

* battery: clamp percentage used by build()
Made clamp_percentage() const to allow its usage inside build()

* battery: read and return percentage in one line
This commit is contained in:
Roberto Santalla 2019-05-14 15:01:54 +02:00 committed by Patrick Ziegler
parent 65e72fd71e
commit b2b73b5d91
2 changed files with 14 additions and 9 deletions

View file

@ -56,7 +56,8 @@ namespace modules {
protected: protected:
state current_state(); state current_state();
int current_percentage(state state); int current_percentage();
int clamp_percentage(int percentage, state state) const;
string current_time(); string current_time();
string current_consumption(); string current_consumption();
void subthread(); void subthread();

View file

@ -109,7 +109,7 @@ namespace modules {
// Load state and capacity level // Load state and capacity level
m_state = current_state(); m_state = current_state();
m_percentage = current_percentage(m_state); m_percentage = current_percentage();
// Add formats and elements // Add formats and elements
m_formatter->add(FORMAT_CHARGING, TAG_LABEL_CHARGING, m_formatter->add(FORMAT_CHARGING, TAG_LABEL_CHARGING,
@ -199,7 +199,7 @@ namespace modules {
*/ */
bool battery_module::on_event(inotify_event* event) { bool battery_module::on_event(inotify_event* event) {
auto state = current_state(); auto state = current_state();
auto percentage = current_percentage(state); auto percentage = current_percentage();
// Reset timer to avoid unnecessary polling // Reset timer to avoid unnecessary polling
m_lastpoll = chrono::system_clock::now(); m_lastpoll = chrono::system_clock::now();
@ -229,7 +229,8 @@ namespace modules {
if (label) { if (label) {
label->reset_tokens(); label->reset_tokens();
label->replace_token("%percentage%", to_string(m_percentage)); label->replace_token("%percentage%", to_string(clamp_percentage(m_percentage, m_state)));
label->replace_token("%percentage_raw%", to_string(m_percentage));
label->replace_token("%consumption%", current_consumption()); label->replace_token("%consumption%", current_consumption());
if (m_state != battery_module::state::FULL && !m_timeformat.empty()) { if (m_state != battery_module::state::FULL && !m_timeformat.empty()) {
@ -262,9 +263,9 @@ namespace modules {
} else if (tag == TAG_ANIMATION_DISCHARGING) { } else if (tag == TAG_ANIMATION_DISCHARGING) {
builder->node(m_animation_discharging->get()); builder->node(m_animation_discharging->get());
} else if (tag == TAG_BAR_CAPACITY) { } else if (tag == TAG_BAR_CAPACITY) {
builder->node(m_bar_capacity->output(m_percentage)); builder->node(m_bar_capacity->output(clamp_percentage(m_percentage, m_state)));
} else if (tag == TAG_RAMP_CAPACITY) { } else if (tag == TAG_RAMP_CAPACITY) {
builder->node(m_ramp_capacity->get_by_percentage(m_percentage)); builder->node(m_ramp_capacity->get_by_percentage(clamp_percentage(m_percentage, m_state)));
} else if (tag == TAG_LABEL_CHARGING) { } else if (tag == TAG_LABEL_CHARGING) {
builder->node(m_label_charging); builder->node(m_label_charging);
} else if (tag == TAG_LABEL_DISCHARGING) { } else if (tag == TAG_LABEL_DISCHARGING) {
@ -294,10 +295,13 @@ namespace modules {
/** /**
* Get the current capacity level * Get the current capacity level
*/ */
int battery_module::current_percentage(state state) { int battery_module::current_percentage() {
int percentage{read(*m_capacity_reader)}; return read(*m_capacity_reader);
}
int battery_module::clamp_percentage(int percentage, state state) const {
if (state == battery_module::state::FULL && percentage >= m_fullat) { if (state == battery_module::state::FULL && percentage >= m_fullat) {
percentage = 100; return 100;
} }
return percentage; return percentage;
} }