From 7f1f217d84ae7f3b020ce28cd920f15722e15f52 Mon Sep 17 00:00:00 2001 From: Alexis Date: Thu, 25 Oct 2018 17:30:26 +0200 Subject: [PATCH] feat: multiple config per modules --- resources/config | 5 ++++- src/factory.cpp | 23 ++++++++++++----------- src/modules/battery.cpp | 19 +++++++++++++++---- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/resources/config b/resources/config index de1a99a..45b6a0a 100644 --- a/resources/config +++ b/resources/config @@ -6,7 +6,7 @@ // Choose the order of the modules "modules-left": ["sway/workspaces", "custom/spotify"], "modules-center": ["sway/window"], - "modules-right": ["pulseaudio", "network", "cpu", "memory", "battery", "clock", "tray"], + "modules-right": ["pulseaudio", "network", "cpu", "memory", "battery", "battery#bat2", "clock", "tray"], // Modules configuration // "sway/workspaces": { // "disable-scroll": true, @@ -39,6 +39,9 @@ "format": "{capacity}% {icon}", "format-icons": ["", "", "", "", ""] }, + "battery#bat2": { + "bat": "BAT2" + }, "network": { // "interface": "wlp2s0", // (Optional) To force the use of this interface "format-wifi": "{essid} ({signalStrength}%) ", diff --git a/src/factory.cpp b/src/factory.cpp index 5961395..479ab79 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -7,43 +7,44 @@ waybar::Factory::Factory(Bar& bar, const Json::Value& config) waybar::IModule* waybar::Factory::makeModule(const std::string &name) const { try { - if (name == "battery") { + auto ref = name.substr(0, name.find("#")); + if (ref == "battery") { return new waybar::modules::Battery(config_[name]); } #ifdef HAVE_SWAY - if (name == "sway/workspaces") { + if (ref == "sway/workspaces") { return new waybar::modules::sway::Workspaces(bar_, config_[name]); } - if (name == "sway/window") { + if (ref == "sway/window") { return new waybar::modules::sway::Window(bar_, config_[name]); } #endif - if (name == "memory") { + if (ref == "memory") { return new waybar::modules::Memory(config_[name]); } - if (name == "cpu") { + if (ref == "cpu") { return new waybar::modules::Cpu(config_[name]); } - if (name == "clock") { + if (ref == "clock") { return new waybar::modules::Clock(config_[name]); } #ifdef HAVE_DBUSMENU - if (name == "tray") { + if (ref == "tray") { return new waybar::modules::SNI::Tray(config_[name]); } #endif #ifdef HAVE_LIBNL - if (name == "network") { + if (ref == "network") { return new waybar::modules::Network(config_[name]); } #endif #ifdef HAVE_LIBPULSE - if (name == "pulseaudio") { + if (ref == "pulseaudio") { return new waybar::modules::Pulseaudio(config_[name]); } #endif - if (name.compare(0, 7, "custom/") == 0 && name.size() > 7) { - return new waybar::modules::Custom(name.substr(7), config_[name]); + if (ref.compare(0, 7, "custom/") == 0 && ref.size() > 7) { + return new waybar::modules::Custom(ref.substr(7), config_[name]); } } catch (const std::exception& e) { auto err = fmt::format("Disabling module \"{}\", {}", name, e.what()); diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 002b8ed..ddfcb4f 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -4,16 +4,27 @@ waybar::modules::Battery::Battery(const Json::Value& config) : ALabel(config, "{capacity}%") { try { - for (auto const& node : fs::directory_iterator(data_dir_)) { - if (fs::is_directory(node) && fs::exists(node / "capacity") - && fs::exists(node / "status") && fs::exists(node / "uevent")) { - batteries_.push_back(node); + if (config_["bat"]) { + auto dir = data_dir_ / config_["bat"].asString(); + if (fs::is_directory(dir) && fs::exists(dir / "capacity") + && fs::exists(dir / "status") && fs::exists(dir / "uevent")) { + batteries_.push_back(dir); + } + } else { + for (auto const& node : fs::directory_iterator(data_dir_)) { + if (fs::is_directory(node) && fs::exists(node / "capacity") + && fs::exists(node / "status") && fs::exists(node / "uevent")) { + batteries_.push_back(node); + } } } } catch (fs::filesystem_error &e) { throw std::runtime_error(e.what()); } if (batteries_.empty()) { + if (config_["bat"]) { + throw std::runtime_error("No battery named " + config_["bat"].asString()); + } throw std::runtime_error("No batteries."); } fd_ = inotify_init1(IN_CLOEXEC);