From ae7686d24b8f21c1a6185d4662245f5cc6f545e2 Mon Sep 17 00:00:00 2001
From: patrick96
Date: Mon, 21 Feb 2022 02:14:54 +0100
Subject: [PATCH] fix(battery): Replace tokens for all labels
Otherwise we have to replicate the fallback logic for LOW -> DISCHARGING
in multiple places.
Fixes #2379
---
src/modules/battery.cpp | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp
index 63b1494b..7b8c4163 100644
--- a/src/modules/battery.cpp
+++ b/src/modules/battery.cpp
@@ -229,16 +229,11 @@ namespace modules {
m_state = state;
m_percentage = percentage;
- const auto label = [this] {
- switch (m_state) {
- case battery_module::state::FULL: return m_label_full;
- case battery_module::state::DISCHARGING: return m_label_discharging;
- case battery_module::state::LOW: return m_label_low;
- default: return m_label_charging;
+ const auto replace_tokens = [&](label_t& label) {
+ if (!label) {
+ return;
}
- }();
- if (label) {
label->reset_tokens();
label->replace_token("%percentage%", to_string(clamp_percentage(m_percentage, m_state)));
label->replace_token("%percentage_raw%", to_string(m_percentage));
@@ -247,7 +242,12 @@ namespace modules {
if (m_state != battery_module::state::FULL && !m_timeformat.empty()) {
label->replace_token("%time%", current_time());
}
- }
+ };
+
+ replace_tokens(m_label_full);
+ replace_tokens(m_label_discharging);
+ replace_tokens(m_label_low);
+ replace_tokens(m_label_charging);
return true;
}