Added user-defined vars in config

This commit is contained in:
vaxerski 2022-05-16 09:38:42 +02:00
parent 1d4d52af31
commit 26cd7d73e5
2 changed files with 31 additions and 1 deletions

View file

@ -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");

View file

@ -76,6 +76,7 @@ public:
std::string parseKeyword(const std::string&, const std::string&, bool dynamic = false);
private:
std::unordered_map<std::string, std::string> configDynamicVars; // stores dynamic vars declared by the user
std::unordered_map<std::string, SConfigValue> 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&);