From b39a8ede6c7e466f80e0fd36848f6ed85c3485be Mon Sep 17 00:00:00 2001 From: Viktar Lukashonak Date: Sat, 14 May 2022 16:56:46 +0300 Subject: [PATCH 1/2] Issue#1556. Battery module: wrong string comparison 1. battery.hpp - added local bool variable. Force to print warnings the only once in order to warn user about wrong battery configuraion. And does not bring a mess when the battery is turned off (gamepads, etc.) 2. dir_name is an object which takes a part in comparison. So converted to the string. --- include/modules/battery.hpp | 1 + src/modules/battery.cpp | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index 11028cf6..99950ae3 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -46,6 +46,7 @@ class Battery : public ALabel { int global_watch_fd_; std::mutex battery_list_mutex_; std::string old_status_; + bool warnFirstTime_{true}; util::SleeperThread thread_; util::SleeperThread thread_battery_update_; diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index cb7237a3..4d025157 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -20,7 +20,6 @@ waybar::modules::Battery::Battery(const std::string& id, const Json::Value& conf throw std::runtime_error("Could not watch for battery plug/unplug"); } - refreshBatteries(); worker(); } @@ -73,7 +72,6 @@ void waybar::modules::Battery::worker() { void waybar::modules::Battery::refreshBatteries() { std::lock_guard guard(battery_list_mutex_); - // Mark existing list of batteries as not necessarily found std::map check_map; for (auto const& bat : batteries_) { @@ -85,7 +83,7 @@ void waybar::modules::Battery::refreshBatteries() { if (!fs::is_directory(node)) { continue; } - auto dir_name = node.path().filename(); + auto dir_name = node.path().filename().string(); auto bat_defined = config_["bat"].isString(); if (((bat_defined && dir_name == config_["bat"].asString()) || !bat_defined) && (fs::exists(node.path() / "capacity") || fs::exists(node.path() / "charge_now")) && @@ -117,12 +115,14 @@ void waybar::modules::Battery::refreshBatteries() { } catch (fs::filesystem_error& e) { throw std::runtime_error(e.what()); } - if (batteries_.empty()) { + if (warnFirstTime_ && batteries_.empty()) { if (config_["bat"].isString()) { - spdlog::warn("No battery named {}", config_["bat"].asString()); + spdlog::warn("No battery named {0}", config_["bat"].asString()); } else { spdlog::warn("No batteries."); } + + warnFirstTime_ = false; } // Remove any batteries that are no longer present and unwatch them From 7f995507fec1133c8000b1dbb93ca23b1be1e720 Mon Sep 17 00:00:00 2001 From: Viktar Lukashonak Date: Sat, 14 May 2022 17:10:44 +0300 Subject: [PATCH 2/2] std::filesystem::path is a string representation --- src/modules/battery.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 4d025157..60194f0f 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -83,7 +83,7 @@ void waybar::modules::Battery::refreshBatteries() { if (!fs::is_directory(node)) { continue; } - auto dir_name = node.path().filename().string(); + auto dir_name = node.path().filename(); auto bat_defined = config_["bat"].isString(); if (((bat_defined && dir_name == config_["bat"].asString()) || !bat_defined) && (fs::exists(node.path() / "capacity") || fs::exists(node.path() / "charge_now")) &&