From 9c57df505c4401ce42dc205d07049715ca6aa79d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20L=2E=20Treffenst=C3=A4dt?= Date: Fri, 2 Nov 2018 19:41:00 +0100 Subject: [PATCH 1/6] Add class for full battery and give option to interpret unknown as full --- src/modules/battery.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 70892fa8..3b1ebb8b 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -74,6 +74,8 @@ std::tuple waybar::modules::Battery::getInfos() std::ifstream(bat / "status") >> _status; if (_status != "Unknown") { status = _status; + }else if (config_["full-is-unknown"].isString() && config_["full-is-unknown"] == "true") { + status = "Full"; //Some notebooks (e.g. Thinkpad T430s) report a full battery as "Unknown". } total += capacity; } @@ -125,8 +127,13 @@ auto waybar::modules::Battery::update() -> void } } else { label_.get_style_context()->remove_class("charging"); - if (status == "Full" && config_["format-full"].isString()) { - format = config_["format-full"].asString(); + if (status == "Full"){ + label_.get_style_context()->add_class("full"); + if (config_["format-full"].isString()) { + format = config_["format-full"].asString(); + } + }else{ + label_.get_style_context()->remove_class("full"); } } auto state = getState(capacity, charging); From b6cad0548913c77f4c292c27c4af482be9364d5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20L=2E=20Treffenst=C3=A4dt?= Date: Fri, 2 Nov 2018 21:13:57 +0100 Subject: [PATCH 2/6] fix formatting --- src/modules/battery.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 3b1ebb8b..90ed004f 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -74,7 +74,7 @@ std::tuple waybar::modules::Battery::getInfos() std::ifstream(bat / "status") >> _status; if (_status != "Unknown") { status = _status; - }else if (config_["full-is-unknown"].isString() && config_["full-is-unknown"] == "true") { + } else if (config_["full-is-unknown"].isString() && config_["full-is-unknown"] == "true") { status = "Full"; //Some notebooks (e.g. Thinkpad T430s) report a full battery as "Unknown". } total += capacity; @@ -127,12 +127,12 @@ auto waybar::modules::Battery::update() -> void } } else { label_.get_style_context()->remove_class("charging"); - if (status == "Full"){ + if (status == "Full") { label_.get_style_context()->add_class("full"); if (config_["format-full"].isString()) { format = config_["format-full"].asString(); } - }else{ + } else { label_.get_style_context()->remove_class("full"); } } From 0522577fe53c6376c8566c3851df41bce8878667 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20L=2E=20Treffenst=C3=A4dt?= Date: Fri, 2 Nov 2018 22:04:43 +0100 Subject: [PATCH 3/6] make status and state fully configurable formats --- include/modules/battery.hpp | 1 + src/modules/battery.cpp | 29 +++++++++-------------------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index e587ea11..5c155d01 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -29,6 +29,7 @@ class Battery : public ALabel { util::SleeperThread threadTimer_; std::vector batteries_; int fd_; + std::string old_state_; }; } diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 90ed004f..06476f37 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -74,8 +74,6 @@ std::tuple waybar::modules::Battery::getInfos() std::ifstream(bat / "status") >> _status; if (_status != "Unknown") { status = _status; - } else if (config_["full-is-unknown"].isString() && config_["full-is-unknown"] == "true") { - status = "Full"; //Some notebooks (e.g. Thinkpad T430s) report a full battery as "Unknown". } total += capacity; } @@ -118,26 +116,17 @@ auto waybar::modules::Battery::update() -> void { auto [capacity, status] = getInfos(); label_.set_tooltip_text(status); - bool charging = status == "Charging"; + std::transform(status.begin(), status.end(), status.begin(), ::tolower); auto format = format_; - if (charging) { - label_.get_style_context()->add_class("charging"); - if (config_["format-charging"].isString()) { - format = config_["format-charging"].asString(); - } - } else { - label_.get_style_context()->remove_class("charging"); - if (status == "Full") { - label_.get_style_context()->add_class("full"); - if (config_["format-full"].isString()) { - format = config_["format-full"].asString(); - } - } else { - label_.get_style_context()->remove_class("full"); - } - } auto state = getState(capacity, charging); - if (!state.empty() && config_["format-" + state].isString()) { + label_.get_style_context()->remove_class(old_status_); + label_.get_style_context()->add_class(status); + old_status_ = status; + if (!state.empty() && config_["format-" + status + "-" + state].isString()) { + format = config_["format-" + status + "-" + state].asString(); + }else if (config_["format-" + status].isString()) { + format = config_["format-" + status].asString(); + }else if (!state.empty() && config_["format-" + state].isString()) { format = config_["format-" + state].asString(); } if (format.empty()) { From 123ce083b46bdff6cf3113347f72c553d3c01425 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20L=2E=20Treffenst=C3=A4dt?= Date: Fri, 2 Nov 2018 22:08:55 +0100 Subject: [PATCH 4/6] fix typo and initialize old_status_ --- include/modules/battery.hpp | 2 +- src/modules/battery.cpp | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index 5c155d01..093367d6 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -29,7 +29,7 @@ class Battery : public ALabel { util::SleeperThread threadTimer_; std::vector batteries_; int fd_; - std::string old_state_; + std::string old_status_; }; } diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 06476f37..0986fff1 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -34,6 +34,7 @@ waybar::modules::Battery::Battery(const Json::Value& config) for (auto const& bat : batteries_) { inotify_add_watch(fd_, (bat / "uevent").c_str(), IN_ACCESS); } + old_status_ = ""; worker(); } @@ -85,7 +86,7 @@ std::tuple waybar::modules::Battery::getInfos() } } -std::string waybar::modules::Battery::getState(uint16_t capacity, bool charging) +std::string waybar::modules::Battery::getState(uint16_t capacity) { // Get current state std::vector> states; @@ -102,7 +103,7 @@ std::string waybar::modules::Battery::getState(uint16_t capacity, bool charging) }); std::string validState = ""; for (auto state : states) { - if (capacity <= state.second && !charging && validState.empty()) { + if (capacity <= state.second && validState.empty()) { label_.get_style_context()->add_class(state.first); validState = state.first; } else { @@ -118,7 +119,7 @@ auto waybar::modules::Battery::update() -> void label_.set_tooltip_text(status); std::transform(status.begin(), status.end(), status.begin(), ::tolower); auto format = format_; - auto state = getState(capacity, charging); + auto state = getState(capacity); label_.get_style_context()->remove_class(old_status_); label_.get_style_context()->add_class(status); old_status_ = status; From d8b620163212c670acde4cb56c4b9297b9652e4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20L=2E=20Treffenst=C3=A4dt?= Date: Fri, 2 Nov 2018 22:15:54 +0100 Subject: [PATCH 5/6] ...and fix the function signature in the header --- include/modules/battery.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index 093367d6..50df78d3 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -23,7 +23,7 @@ class Battery : public ALabel { void worker(); std::tuple getInfos(); - std::string getState(uint16_t, bool); + std::string getState(uint16_t); util::SleeperThread thread_; util::SleeperThread threadTimer_; From 25f31b19f6a71306119bc4381115bf710c8d3948 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20L=2E=20Treffenst=C3=A4dt?= Date: Fri, 2 Nov 2018 22:50:01 +0100 Subject: [PATCH 6/6] formatting is hard. --- src/modules/battery.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 0986fff1..83ebd08f 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -34,7 +34,6 @@ waybar::modules::Battery::Battery(const Json::Value& config) for (auto const& bat : batteries_) { inotify_add_watch(fd_, (bat / "uevent").c_str(), IN_ACCESS); } - old_status_ = ""; worker(); } @@ -67,7 +66,7 @@ std::tuple waybar::modules::Battery::getInfos() { try { uint16_t total = 0; - std::string status; + std::string status = "Unknown"; for (auto const& bat : batteries_) { uint16_t capacity; std::string _status; @@ -125,9 +124,9 @@ auto waybar::modules::Battery::update() -> void old_status_ = status; if (!state.empty() && config_["format-" + status + "-" + state].isString()) { format = config_["format-" + status + "-" + state].asString(); - }else if (config_["format-" + status].isString()) { + } else if (config_["format-" + status].isString()) { format = config_["format-" + status].asString(); - }else if (!state.empty() && config_["format-" + state].isString()) { + } else if (!state.empty() && config_["format-" + state].isString()) { format = config_["format-" + state].asString(); } if (format.empty()) {