From 26cd7d73e5b601836b1b061dee50b543d95eb76a Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Mon, 16 May 2022 09:38:42 +0200 Subject: [PATCH] Added user-defined vars in config --- src/config/ConfigManager.cpp | 30 +++++++++++++++++++++++++++++- src/config/ConfigManager.hpp | 2 ++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index 6cb51797..6f6a3bc2 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -101,7 +101,14 @@ void CConfigManager::init() { void CConfigManager::configSetValueSafe(const std::string& COMMAND, const std::string& VALUE) { if (configValues.find(COMMAND) == configValues.end()) { - parseError = "Error setting value <" + VALUE + "> for field <" + COMMAND + ">: No such field."; + if (COMMAND[0] == '$') { + // register a dynamic var + Debug::log(LOG, "Registered dynamic var \"%s\" -> %s", COMMAND, VALUE); + configDynamicVars[COMMAND.substr(1)] = VALUE; + } else { + parseError = "Error setting value <" + VALUE + "> for field <" + COMMAND + ">: No such field."; + } + return; } @@ -456,6 +463,23 @@ std::string CConfigManager::parseKeyword(const std::string& COMMAND, const std:: return parseError; } +void CConfigManager::applyUserDefinedVars(std::string& line, const size_t equalsPlace) { + auto dollarPlace = line.find_first_of('$', equalsPlace); + + while (dollarPlace != std::string::npos) { + + const auto STRAFTERDOLLAR = line.substr(dollarPlace + 1); + for (auto&[var, value] : configDynamicVars) { + if (STRAFTERDOLLAR.find(var) == 0) { + line.replace(dollarPlace, var.length() + 1, value); + break; + } + } + + dollarPlace = line.find_first_of('$', dollarPlace + 1); + } +} + void CConfigManager::parseLine(std::string& line) { // first check if its not a comment const auto COMMENTSTART = line.find_first_of('#'); @@ -494,6 +518,9 @@ void CConfigManager::parseLine(std::string& line) { // check if command const auto EQUALSPLACE = line.find_first_of('='); + // apply vars + applyUserDefinedVars(line, EQUALSPLACE); + if (EQUALSPLACE == std::string::npos) return; @@ -516,6 +543,7 @@ void CConfigManager::loadConfigLoadVars() { g_pKeybindManager->clearKeybinds(); g_pAnimationManager->removeAllBeziers(); m_mAdditionalReservedAreas.clear(); + configDynamicVars.clear(); const char* const ENVHOME = getenv("HOME"); const std::string CONFIGPATH = ENVHOME + (ISDEBUG ? (std::string) "/.config/hypr/hyprlandd.conf" : (std::string) "/.config/hypr/hyprland.conf"); diff --git a/src/config/ConfigManager.hpp b/src/config/ConfigManager.hpp index 9398b4f6..0539a530 100644 --- a/src/config/ConfigManager.hpp +++ b/src/config/ConfigManager.hpp @@ -76,6 +76,7 @@ public: std::string parseKeyword(const std::string&, const std::string&, bool dynamic = false); private: + std::unordered_map configDynamicVars; // stores dynamic vars declared by the user std::unordered_map configValues; time_t lastModifyTime = 0; // for reloading the config if changed @@ -94,6 +95,7 @@ private: // internal methods void setDefaultVars(); + void applyUserDefinedVars(std::string&, const size_t); void loadConfigLoadVars(); SConfigValue getConfigValueSafe(std::string); void parseLine(std::string&);