From 14f626d422a77b51bca4bbbe5282ee6e995169e0 Mon Sep 17 00:00:00 2001 From: Oskar Carl Date: Sun, 24 Jan 2021 17:36:30 +0100 Subject: [PATCH] Add recursive config includes --- include/client.hpp | 1 + src/client.cpp | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/include/client.hpp b/include/client.hpp index ec3866a..af6eeef 100644 --- a/include/client.hpp +++ b/include/client.hpp @@ -39,6 +39,7 @@ class Client { void handleOutput(struct waybar_output &output); bool isValidOutput(const Json::Value &config, struct waybar_output &output); auto setupConfig(const std::string &config_file) -> void; + auto mergeConfig(Json::Value &a_config_, Json::Value &b_config_) -> void; auto setupCss(const std::string &css_file) -> void; struct waybar_output & getOutput(void *); std::vector getOutputConfigs(struct waybar_output &output); diff --git a/src/client.cpp b/src/client.cpp index ced9e49..752574d 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -241,7 +241,24 @@ auto waybar::Client::setupConfig(const std::string &config_file) -> void { } std::string str((std::istreambuf_iterator(file)), std::istreambuf_iterator()); util::JsonParser parser; - config_ = parser.parse(str); + Json::Value tmp_config_ = parser.parse(str); + if (tmp_config_["include"].isArray()) { + for (const auto& include : tmp_config_["include"]) { + spdlog::info("Including resource file: {}", include.asString()); + setupConfig(getValidPath({include.asString()})); + } + } + mergeConfig(config_, tmp_config_); +} + +auto waybar::Client::mergeConfig(Json::Value &a_config_, Json::Value &b_config_) -> void { + for (const auto& key : b_config_.getMemberNames()) { + if (a_config_[key].type() == Json::objectValue && b_config_[key].type() == Json::objectValue) { + mergeConfig(a_config_[key], b_config_[key]); + } else { + a_config_[key] = b_config_[key]; + } + } } auto waybar::Client::setupCss(const std::string &css_file) -> void {