mirror of
https://github.com/hyprwm/hyprlang.git
synced 2024-12-22 18:09:49 +01:00
core: add env
This commit is contained in:
parent
582d35fee8
commit
c5d9b60170
4 changed files with 25 additions and 2 deletions
|
@ -7,6 +7,7 @@
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
using namespace Hyprlang;
|
using namespace Hyprlang;
|
||||||
|
extern "C" char** environ;
|
||||||
|
|
||||||
static std::string removeBeginEndSpacesTabs(std::string str) {
|
static std::string removeBeginEndSpacesTabs(std::string str) {
|
||||||
if (str.empty())
|
if (str.empty())
|
||||||
|
@ -35,6 +36,16 @@ CConfig::CConfig(const char* path) {
|
||||||
|
|
||||||
if (!std::filesystem::exists(impl->path))
|
if (!std::filesystem::exists(impl->path))
|
||||||
throw "File does not exist";
|
throw "File does not exist";
|
||||||
|
|
||||||
|
impl->envVariables.clear();
|
||||||
|
for (char** env = environ; *env; ++env) {
|
||||||
|
const std::string ENVVAR = *env;
|
||||||
|
const auto VARIABLE = ENVVAR.substr(0, ENVVAR.find_first_of('='));
|
||||||
|
const auto VALUE = ENVVAR.substr(ENVVAR.find_first_of('=') + 1);
|
||||||
|
impl->envVariables.push_back({VARIABLE, VALUE});
|
||||||
|
}
|
||||||
|
|
||||||
|
std::sort(impl->envVariables.begin(), impl->envVariables.end(), [&](const auto& a, const auto& b) { return a.name.length() > b.name.length(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
CConfig::~CConfig() {
|
CConfig::~CConfig() {
|
||||||
|
@ -301,7 +312,7 @@ CParseResult CConfig::parseLine(std::string line, bool dynamic) {
|
||||||
replaceAll(RHS, "$" + var.name, var.value);
|
replaceAll(RHS, "$" + var.name, var.value);
|
||||||
|
|
||||||
if (RHSIT == std::string::npos && LHSIT == std::string::npos)
|
if (RHSIT == std::string::npos && LHSIT == std::string::npos)
|
||||||
break;
|
continue;
|
||||||
else
|
else
|
||||||
var.linesContainingVar.push_back(line);
|
var.linesContainingVar.push_back(line);
|
||||||
|
|
||||||
|
@ -398,7 +409,11 @@ CParseResult CConfig::parse() {
|
||||||
|
|
||||||
iffile.close();
|
iffile.close();
|
||||||
|
|
||||||
clearState();
|
if (!impl->categories.empty()) {
|
||||||
|
fileParseResult.setError("Unclosed category at EOF");
|
||||||
|
impl->categories.clear();
|
||||||
|
return fileParseResult;
|
||||||
|
}
|
||||||
|
|
||||||
return fileParseResult;
|
return fileParseResult;
|
||||||
}
|
}
|
||||||
|
@ -414,6 +429,7 @@ CParseResult CConfig::parseDynamic(const char* command, const char* value) {
|
||||||
void CConfig::clearState() {
|
void CConfig::clearState() {
|
||||||
impl->categories.clear();
|
impl->categories.clear();
|
||||||
impl->parseError = "";
|
impl->parseError = "";
|
||||||
|
impl->variables = impl->envVariables;
|
||||||
}
|
}
|
||||||
|
|
||||||
CConfigValue* CConfig::getConfigValuePtr(const char* name) {
|
CConfigValue* CConfig::getConfigValuePtr(const char* name) {
|
||||||
|
|
|
@ -24,6 +24,7 @@ class CConfigImpl {
|
||||||
std::unordered_map<std::string, Hyprlang::CConfigValue> defaultValues;
|
std::unordered_map<std::string, Hyprlang::CConfigValue> defaultValues;
|
||||||
std::vector<SHandler> handlers;
|
std::vector<SHandler> handlers;
|
||||||
std::vector<SVariable> variables;
|
std::vector<SVariable> variables;
|
||||||
|
std::vector<SVariable> envVariables;
|
||||||
|
|
||||||
std::vector<std::string> categories;
|
std::vector<std::string> categories;
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,8 @@ $MY_VAR = 1337
|
||||||
$MY_VAR_2 = $MY_VAR
|
$MY_VAR_2 = $MY_VAR
|
||||||
testVar = $MY_VAR$MY_VAR_2
|
testVar = $MY_VAR$MY_VAR_2
|
||||||
|
|
||||||
|
testEnv = $XDG_SESSION_TYPE
|
||||||
|
|
||||||
testCategory {
|
testCategory {
|
||||||
testValueInt = 123456
|
testValueInt = 123456
|
||||||
testValueHex = 0xF
|
testValueHex = 0xF
|
||||||
|
|
|
@ -53,6 +53,7 @@ int main(int argc, char** argv, char** envp) {
|
||||||
config.addConfigValue("testFloat", 0.F);
|
config.addConfigValue("testFloat", 0.F);
|
||||||
config.addConfigValue("testVec", Hyprlang::SVector2D{69, 420});
|
config.addConfigValue("testVec", Hyprlang::SVector2D{69, 420});
|
||||||
config.addConfigValue("testString", "");
|
config.addConfigValue("testString", "");
|
||||||
|
config.addConfigValue("testEnv", "");
|
||||||
config.addConfigValue("testVar", 0L);
|
config.addConfigValue("testVar", 0L);
|
||||||
config.addConfigValue("testStringQuotes", "");
|
config.addConfigValue("testStringQuotes", "");
|
||||||
config.addConfigValue("testCategory:testValueInt", 0L);
|
config.addConfigValue("testCategory:testValueInt", 0L);
|
||||||
|
@ -116,6 +117,9 @@ int main(int argc, char** argv, char** envp) {
|
||||||
EXPECT(config.parseDynamic("$MY_VAR_2 = 420").error, false);
|
EXPECT(config.parseDynamic("$MY_VAR_2 = 420").error, false);
|
||||||
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testVar")), 1337420);
|
EXPECT(std::any_cast<int64_t>(config.getConfigValue("testVar")), 1337420);
|
||||||
|
|
||||||
|
// test env variables
|
||||||
|
EXPECT(std::any_cast<const char*>(config.getConfigValue("testEnv")), std::string{"wayland"});
|
||||||
|
|
||||||
} catch (const char* e) {
|
} catch (const char* e) {
|
||||||
std::cout << Colors::RED << "Error: " << Colors::RESET << e << "\n";
|
std::cout << Colors::RED << "Error: " << Colors::RESET << e << "\n";
|
||||||
return 1;
|
return 1;
|
||||||
|
|
Loading…
Reference in a new issue