feat: multiple config per modules

This commit is contained in:
Alexis 2018-10-25 17:30:26 +02:00
parent 1ea0c1f9dd
commit 7f1f217d84
3 changed files with 31 additions and 16 deletions

View File

@ -6,7 +6,7 @@
// Choose the order of the modules // Choose the order of the modules
"modules-left": ["sway/workspaces", "custom/spotify"], "modules-left": ["sway/workspaces", "custom/spotify"],
"modules-center": ["sway/window"], "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 // Modules configuration
// "sway/workspaces": { // "sway/workspaces": {
// "disable-scroll": true, // "disable-scroll": true,
@ -39,6 +39,9 @@
"format": "{capacity}% {icon}", "format": "{capacity}% {icon}",
"format-icons": ["", "", "", "", ""] "format-icons": ["", "", "", "", ""]
}, },
"battery#bat2": {
"bat": "BAT2"
},
"network": { "network": {
// "interface": "wlp2s0", // (Optional) To force the use of this interface // "interface": "wlp2s0", // (Optional) To force the use of this interface
"format-wifi": "{essid} ({signalStrength}%) ", "format-wifi": "{essid} ({signalStrength}%) ",

View File

@ -7,43 +7,44 @@ waybar::Factory::Factory(Bar& bar, const Json::Value& config)
waybar::IModule* waybar::Factory::makeModule(const std::string &name) const waybar::IModule* waybar::Factory::makeModule(const std::string &name) const
{ {
try { try {
if (name == "battery") { auto ref = name.substr(0, name.find("#"));
if (ref == "battery") {
return new waybar::modules::Battery(config_[name]); return new waybar::modules::Battery(config_[name]);
} }
#ifdef HAVE_SWAY #ifdef HAVE_SWAY
if (name == "sway/workspaces") { if (ref == "sway/workspaces") {
return new waybar::modules::sway::Workspaces(bar_, config_[name]); 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]); return new waybar::modules::sway::Window(bar_, config_[name]);
} }
#endif #endif
if (name == "memory") { if (ref == "memory") {
return new waybar::modules::Memory(config_[name]); return new waybar::modules::Memory(config_[name]);
} }
if (name == "cpu") { if (ref == "cpu") {
return new waybar::modules::Cpu(config_[name]); return new waybar::modules::Cpu(config_[name]);
} }
if (name == "clock") { if (ref == "clock") {
return new waybar::modules::Clock(config_[name]); return new waybar::modules::Clock(config_[name]);
} }
#ifdef HAVE_DBUSMENU #ifdef HAVE_DBUSMENU
if (name == "tray") { if (ref == "tray") {
return new waybar::modules::SNI::Tray(config_[name]); return new waybar::modules::SNI::Tray(config_[name]);
} }
#endif #endif
#ifdef HAVE_LIBNL #ifdef HAVE_LIBNL
if (name == "network") { if (ref == "network") {
return new waybar::modules::Network(config_[name]); return new waybar::modules::Network(config_[name]);
} }
#endif #endif
#ifdef HAVE_LIBPULSE #ifdef HAVE_LIBPULSE
if (name == "pulseaudio") { if (ref == "pulseaudio") {
return new waybar::modules::Pulseaudio(config_[name]); return new waybar::modules::Pulseaudio(config_[name]);
} }
#endif #endif
if (name.compare(0, 7, "custom/") == 0 && name.size() > 7) { if (ref.compare(0, 7, "custom/") == 0 && ref.size() > 7) {
return new waybar::modules::Custom(name.substr(7), config_[name]); return new waybar::modules::Custom(ref.substr(7), config_[name]);
} }
} catch (const std::exception& e) { } catch (const std::exception& e) {
auto err = fmt::format("Disabling module \"{}\", {}", name, e.what()); auto err = fmt::format("Disabling module \"{}\", {}", name, e.what());

View File

@ -4,16 +4,27 @@ waybar::modules::Battery::Battery(const Json::Value& config)
: ALabel(config, "{capacity}%") : ALabel(config, "{capacity}%")
{ {
try { try {
for (auto const& node : fs::directory_iterator(data_dir_)) { if (config_["bat"]) {
if (fs::is_directory(node) && fs::exists(node / "capacity") auto dir = data_dir_ / config_["bat"].asString();
&& fs::exists(node / "status") && fs::exists(node / "uevent")) { if (fs::is_directory(dir) && fs::exists(dir / "capacity")
batteries_.push_back(node); && 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) { } catch (fs::filesystem_error &e) {
throw std::runtime_error(e.what()); throw std::runtime_error(e.what());
} }
if (batteries_.empty()) { if (batteries_.empty()) {
if (config_["bat"]) {
throw std::runtime_error("No battery named " + config_["bat"].asString());
}
throw std::runtime_error("No batteries."); throw std::runtime_error("No batteries.");
} }
fd_ = inotify_init1(IN_CLOEXEC); fd_ = inotify_init1(IN_CLOEXEC);