Merge pull request #995 from OskarCarl/master

Add recursive config includes
This commit is contained in:
Alex 2021-06-23 23:35:56 +02:00 committed by GitHub
commit 36857ae72b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 4 deletions

View File

@ -38,7 +38,8 @@ class Client {
const std::string getValidPath(const std::vector<std::string> &paths) const; const std::string getValidPath(const std::vector<std::string> &paths) const;
void handleOutput(struct waybar_output &output); void handleOutput(struct waybar_output &output);
bool isValidOutput(const Json::Value &config, struct waybar_output &output); bool isValidOutput(const Json::Value &config, struct waybar_output &output);
auto setupConfig(const std::string &config_file) -> void; auto setupConfig(const std::string &config_file, int depth) -> void;
auto mergeConfig(Json::Value &a_config_, Json::Value &b_config_) -> void;
auto setupCss(const std::string &css_file) -> void; auto setupCss(const std::string &css_file) -> void;
struct waybar_output & getOutput(void *); struct waybar_output & getOutput(void *);
std::vector<Json::Value> getOutputConfigs(struct waybar_output &output); std::vector<Json::Value> getOutputConfigs(struct waybar_output &output);

View File

@ -85,6 +85,10 @@ Also a minimal example configuration can be found on the at the bottom of this m
Option to disable the use of gtk-layer-shell for popups. Option to disable the use of gtk-layer-shell for popups.
Only functional if compiled with gtk-layer-shell support. Only functional if compiled with gtk-layer-shell support.
*include* ++
typeof: array ++
Paths to additional configuration files. In case of duplicate options, the including file's value takes precedence. Make sure to avoid circular imports.
# MODULE FORMAT # MODULE FORMAT
You can use PangoMarkupFormat (See https://developer.gnome.org/pango/stable/PangoMarkupFormat.html#PangoMarkupFormat). You can use PangoMarkupFormat (See https://developer.gnome.org/pango/stable/PangoMarkupFormat.html#PangoMarkupFormat).

View File

@ -234,14 +234,34 @@ std::tuple<const std::string, const std::string> waybar::Client::getConfigs(
return {config_file, css_file}; return {config_file, css_file};
} }
auto waybar::Client::setupConfig(const std::string &config_file) -> void { auto waybar::Client::setupConfig(const std::string &config_file, int depth) -> void {
if (depth > 100) {
throw std::runtime_error("Aborting due to likely recursive include in config files");
}
std::ifstream file(config_file); std::ifstream file(config_file);
if (!file.is_open()) { if (!file.is_open()) {
throw std::runtime_error("Can't open config file"); throw std::runtime_error("Can't open config file");
} }
std::string str((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); std::string str((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
util::JsonParser parser; 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()}), ++depth);
}
}
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 { auto waybar::Client::setupCss(const std::string &css_file) -> void {
@ -320,7 +340,7 @@ int waybar::Client::main(int argc, char *argv[]) {
} }
wl_display = gdk_wayland_display_get_wl_display(gdk_display->gobj()); wl_display = gdk_wayland_display_get_wl_display(gdk_display->gobj());
auto [config_file, css_file] = getConfigs(config, style); auto [config_file, css_file] = getConfigs(config, style);
setupConfig(config_file); setupConfig(config_file, 0);
setupCss(css_file); setupCss(css_file);
bindInterfaces(); bindInterfaces();
gtk_app->hold(); gtk_app->hold();