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.
This commit is contained in:
Viktar Lukashonak 2022-05-14 16:56:46 +03:00
parent 9bc821bdac
commit b39a8ede6c
No known key found for this signature in database
GPG Key ID: 08A413AA87200A6F
2 changed files with 6 additions and 5 deletions

View File

@ -46,6 +46,7 @@ class Battery : public ALabel {
int global_watch_fd_; int global_watch_fd_;
std::mutex battery_list_mutex_; std::mutex battery_list_mutex_;
std::string old_status_; std::string old_status_;
bool warnFirstTime_{true};
util::SleeperThread thread_; util::SleeperThread thread_;
util::SleeperThread thread_battery_update_; util::SleeperThread thread_battery_update_;

View File

@ -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"); throw std::runtime_error("Could not watch for battery plug/unplug");
} }
refreshBatteries();
worker(); worker();
} }
@ -73,7 +72,6 @@ void waybar::modules::Battery::worker() {
void waybar::modules::Battery::refreshBatteries() { void waybar::modules::Battery::refreshBatteries() {
std::lock_guard<std::mutex> guard(battery_list_mutex_); std::lock_guard<std::mutex> guard(battery_list_mutex_);
// Mark existing list of batteries as not necessarily found // Mark existing list of batteries as not necessarily found
std::map<fs::path, bool> check_map; std::map<fs::path, bool> check_map;
for (auto const& bat : batteries_) { for (auto const& bat : batteries_) {
@ -85,7 +83,7 @@ void waybar::modules::Battery::refreshBatteries() {
if (!fs::is_directory(node)) { if (!fs::is_directory(node)) {
continue; continue;
} }
auto dir_name = node.path().filename(); auto dir_name = node.path().filename().string();
auto bat_defined = config_["bat"].isString(); auto bat_defined = config_["bat"].isString();
if (((bat_defined && dir_name == config_["bat"].asString()) || !bat_defined) && if (((bat_defined && dir_name == config_["bat"].asString()) || !bat_defined) &&
(fs::exists(node.path() / "capacity") || fs::exists(node.path() / "charge_now")) && (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) { } catch (fs::filesystem_error& e) {
throw std::runtime_error(e.what()); throw std::runtime_error(e.what());
} }
if (batteries_.empty()) { if (warnFirstTime_ && batteries_.empty()) {
if (config_["bat"].isString()) { if (config_["bat"].isString()) {
spdlog::warn("No battery named {}", config_["bat"].asString()); spdlog::warn("No battery named {0}", config_["bat"].asString());
} else { } else {
spdlog::warn("No batteries."); spdlog::warn("No batteries.");
} }
warnFirstTime_ = false;
} }
// Remove any batteries that are no longer present and unwatch them // Remove any batteries that are no longer present and unwatch them