Improve the configuration language

Plus it is now much easier to add new config variables
This commit is contained in:
scorpion-26 2023-01-29 17:38:40 +01:00
parent 7cc929f773
commit e38f27b784
3 changed files with 117 additions and 39 deletions

View file

@ -84,12 +84,6 @@ Audio Flyin:
## Configuration for your system ## Configuration for your system
Copy my personal config(found under data/config) into ~/.config/gBar/config and modify it to your needs(Mine will 100% not work, since it is dependant on my dotfiles). Copy my personal config(found under data/config) into ~/.config/gBar/config and modify it to your needs(Mine will 100% not work, since it is dependant on my dotfiles).
The config must be *exactly* formatted the following way(Don't forget the space between the variable and the data!):
```
[Variable]: [value]
[Variable]: [value]
[...]
```
## Plugins ## Plugins
gBar utilizes a plugin system for custom widgets anyone can create without modifying the source code. gBar utilizes a plugin system for custom widgets anyone can create without modifying the source code.

View file

@ -1,7 +1,29 @@
# Example configuration.
# Everything after '#' is ignored
# Format of the variables:
# [variable]: [value]
# Whitespaces are ignored in the following locations:
# - Before the variable
# - After the ':'
# - After the value
# The CPU sensor to use
CPUThermalZone: /sys/devices/pci0000:00/0000:00:18.3/hwmon/hwmon2/temp1_input CPUThermalZone: /sys/devices/pci0000:00/0000:00:18.3/hwmon/hwmon2/temp1_input
# The command to execute on suspend
SuspendCommand: ~/.config/scripts/sys.sh suspend SuspendCommand: ~/.config/scripts/sys.sh suspend
# The command to execute on lock
LockCommand: ~/.config/scripts/sys.sh lock LockCommand: ~/.config/scripts/sys.sh lock
# The command to execute on exit
ExitCommand: killall Hyprland ExitCommand: killall Hyprland
# The folder, where the battery sensors reside
BatteryFolder: /sys/class/power_supply/BAT1 BatteryFolder: /sys/class/power_supply/BAT1
WorkspaceSymbol-1: 
# Overrides the icon of the nth (in this case the first) workspace
# WorkspaceSymbol-1: 
# The default symbol for the workspaces
DefaultWorkspaceSymbol:  DefaultWorkspaceSymbol: 

View file

@ -10,6 +10,80 @@ const Config& Config::Get()
return config; return config;
} }
template<typename T>
void ApplyProperty(T& propertyToSet, const std::string_view& value);
template<>
void ApplyProperty<std::string>(std::string& propertyToSet, const std::string_view& value)
{
propertyToSet = value;
}
template<>
void ApplyProperty<uint32_t>(uint32_t& propertyToSet, const std::string_view& value)
{
// Why, C++?
std::string valStr = std::string(value);
propertyToSet = atoi(valStr.c_str());
}
template<>
void ApplyProperty<bool>(bool& propertyToSet, const std::string_view& value)
{
// Why, C++?
if (value == "true")
{
propertyToSet = true;
}
else if (value == "false")
{
propertyToSet = false;
}
else
{
LOG("Invalid value for bool property: " << value);
}
}
template<typename T>
void AddConfigVar(const std::string& propertyName, T& propertyToSet, std::string_view line, bool& setConfig)
{
const char* whitespace = " \t";
if (setConfig)
{
// Don't bother, already found something else
return;
}
// Strip empty space at the beginning
size_t firstChar = line.find_last_not_of(whitespace);
if (firstChar == std::string::npos)
{
// Line is empty, don't need to check anymore
setConfig = true;
return;
}
line = line.substr(line.find_first_not_of(whitespace));
// Check if line starts with [propertyName]:
if (line.find(propertyName + ":") != 0)
{
return;
}
size_t colon = line.find_first_of(":");
// Now get the value part
std::string_view value = line.substr(colon + 1);
size_t beginValue = value.find_first_not_of(whitespace);
size_t endValue = value.find_last_not_of(whitespace);
value = value.substr(beginValue, endValue - beginValue + 1);
// Set value
ApplyProperty<T>(propertyToSet, value);
LOG("Set value for " << propertyName << ": " << value);
setConfig = true;
}
void Config::Load() void Config::Load()
{ {
const char* xdgConfigHome = getenv("XDG_CONFIG_HOME"); const char* xdgConfigHome = getenv("XDG_CONFIG_HOME");
@ -31,47 +105,35 @@ void Config::Load()
std::string line; std::string line;
while (std::getline(file, line)) while (std::getline(file, line))
{ {
std::string* prop = nullptr; std::string_view lineView = {line};
if (line.find("CPUThermalZone: ") != std::string::npos) // Strip comments
size_t comment = line.find_first_of('#');
if (comment == 0)
{ {
prop = &config.cpuThermalZone; continue;
} }
else if (line.find("SuspendCommand: ") != std::string::npos) if (comment != std::string_view::npos)
{ {
prop = &config.suspendCommand; lineView = lineView.substr(comment - 1);
} }
else if (line.find("LockCommand: ") != std::string::npos)
{ bool foundProperty = false;
prop = &config.lockCommand; AddConfigVar("CPUThermalZone", config.cpuThermalZone, lineView, foundProperty);
} AddConfigVar("SuspendCommand", config.suspendCommand, lineView, foundProperty);
else if (line.find("ExitCommand: ") != std::string::npos) AddConfigVar("LockCommand", config.lockCommand, lineView, foundProperty);
{ AddConfigVar("ExitCommand", config.exitCommand, lineView, foundProperty);
prop = &config.exitCommand; AddConfigVar("BatteryFolder", config.batteryFolder, lineView, foundProperty);
} AddConfigVar("DefaultWorkspaceSymbol", config.defaultWorkspaceSymbol, lineView, foundProperty);
else if (line.find("BatteryFolder: ") != std::string::npos)
{
prop = &config.batteryFolder;
}
else if (line.find("DefaultWorkspaceSymbol") != std::string::npos)
{
prop = &config.defaultWorkspaceSymbol;
}
else if (line.find("WorkspaceSymbol") != std::string::npos)
{
for (int i = 1; i < 10; i++) for (int i = 1; i < 10; i++)
{
if (line.find("WorkspaceSymbol-" + std::to_string(i)) != std::string::npos)
{ {
// Subtract 1 to index from 1 to 9 rather than 0 to 8 // Subtract 1 to index from 1 to 9 rather than 0 to 8
prop = &(config.workspaceSymbols[i - 1]); AddConfigVar("WorkspaceSymbol-" + std::to_string(i), config.workspaceSymbols[i - 1], lineView, foundProperty);
} }
}
} if (foundProperty == false)
if (prop == nullptr)
{ {
LOG("Warning: unknown config var: " << line); LOG("Warning: unknown config var: " << line);
continue; continue;
} }
*prop = line.substr(line.find(' ') + 1); // Everything after space is data
} }
} }