From b2b73b5d91daf88f68c4be512ee6318378727633 Mon Sep 17 00:00:00 2001 From: Roberto Santalla Date: Tue, 14 May 2019 15:01:54 +0200 Subject: [PATCH] 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 --- include/modules/battery.hpp | 3 ++- src/modules/battery.cpp | 20 ++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index 94036e32..1fc11b47 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -56,7 +56,8 @@ namespace modules { protected: state current_state(); - int current_percentage(state state); + int current_percentage(); + int clamp_percentage(int percentage, state state) const; string current_time(); string current_consumption(); void subthread(); diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 1a898191..be9e4ea4 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -109,7 +109,7 @@ namespace modules { // Load state and capacity level m_state = current_state(); - m_percentage = current_percentage(m_state); + m_percentage = current_percentage(); // Add formats and elements m_formatter->add(FORMAT_CHARGING, TAG_LABEL_CHARGING, @@ -199,7 +199,7 @@ namespace modules { */ bool battery_module::on_event(inotify_event* event) { auto state = current_state(); - auto percentage = current_percentage(state); + auto percentage = current_percentage(); // Reset timer to avoid unnecessary polling m_lastpoll = chrono::system_clock::now(); @@ -229,7 +229,8 @@ namespace modules { if (label) { 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()); if (m_state != battery_module::state::FULL && !m_timeformat.empty()) { @@ -262,9 +263,9 @@ namespace modules { } else if (tag == TAG_ANIMATION_DISCHARGING) { builder->node(m_animation_discharging->get()); } 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) { - 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) { builder->node(m_label_charging); } else if (tag == TAG_LABEL_DISCHARGING) { @@ -294,10 +295,13 @@ namespace modules { /** * Get the current capacity level */ - int battery_module::current_percentage(state state) { - int percentage{read(*m_capacity_reader)}; + int battery_module::current_percentage() { + return read(*m_capacity_reader); + } + + int battery_module::clamp_percentage(int percentage, state state) const { if (state == battery_module::state::FULL && percentage >= m_fullat) { - percentage = 100; + return 100; } return percentage; }