Added support for hostname based module addition

This commit is contained in:
jpk
2026-03-05 09:04:21 +01:00
parent 68d4360c26
commit 5de21f4a07
4 changed files with 42 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
#pragma once
#include <json/value.h>
#include <string>
namespace waybar::util {
bool valid_host(const Json::Value& config);
} // namespace waybar::util
+1
View File
@@ -181,6 +181,7 @@ src_files = files(
'src/util/prepare_for_sleep.cpp',
'src/util/ustring_clen.cpp',
'src/util/sanitize_str.cpp',
'src/util/hosts_check.cpp',
'src/util/rewrite_string.cpp',
'src/util/gtk_icon.cpp',
'src/util/icon_loader.cpp',
+14
View File
@@ -10,6 +10,7 @@
#include "group.hpp"
#include "util/enum.hpp"
#include "util/kill_signal.hpp"
#include "util/hosts_check.hpp"
#ifdef HAVE_SWAY
#include "modules/sway/bar.hpp"
@@ -531,6 +532,19 @@ void waybar::Bar::getModules(const Factory& factory, const std::string& pos,
for (const auto& name : module_list) {
try {
auto ref = name.asString();
const Json::Value* module_config = nullptr;
if (config.isMember(ref)) {
module_config = &config[ref];
} else {
auto hash_pos = ref.find('#');
if (hash_pos != std::string::npos) {
std::string ref_base = ref.substr(0, hash_pos);
if (config.isMember(ref_base)) {
module_config = &config[ref_base];
}
}
}
if (module_config && !waybar::util::valid_host(*module_config)) continue;
AModule* module;
if (ref.compare(0, 6, "group/") == 0 && ref.size() > 6) {
+19
View File
@@ -0,0 +1,19 @@
#include <glibmm/miscutils.h>
#include "util/hosts_check.hpp"
namespace waybar::util {
// replaces ``<>&"'`` with their encoded counterparts
bool valid_host(const Json::Value& config) {
if (config.isMember("hosts") && config["hosts"].isArray()) {
auto hostname = Glib::get_host_name();
for (const auto& host : config["hosts"]) {
if (host.asString() == hostname) {
return true;
}
}
return false;
}
return true;
}
} // namespace waybar::util