Add a CLI option to override the config dir

The CLI option -c/--config overrides the search path, and appends itself
to the front of the CSS search path.

Partially implements https://github.com/scorpion-26/gBar/issues/75
This commit is contained in:
scorpion-26 2024-01-21 17:13:45 +01:00
parent 2eeea75c1b
commit e717e0e7ad
9 changed files with 49 additions and 30 deletions

View file

@ -9,31 +9,36 @@ namespace CSS
{ {
static GtkCssProvider* sProvider; static GtkCssProvider* sProvider;
void Load() void Load(const std::string& overrideConfigLocation)
{ {
sProvider = gtk_css_provider_new(); sProvider = gtk_css_provider_new();
std::vector<std::string> locations; std::vector<std::string> locations;
const char* home = std::getenv("HOME"); const char* home = std::getenv("HOME");
if (overrideConfigLocation != "")
{
locations.push_back(overrideConfigLocation);
}
const char* configHome = std::getenv("XDG_CONFIG_HOME"); const char* configHome = std::getenv("XDG_CONFIG_HOME");
if (configHome && strlen(configHome) != 0) if (configHome && strlen(configHome) != 0)
{ {
locations.push_back(configHome); locations.push_back(std::string(configHome) + "/gBar");
} }
else if (home) else if (home)
{ {
locations.push_back(std::string(home) + "/.config"); locations.push_back(std::string(home) + "/.config/gBar");
} }
const char* dataHome = std::getenv("XDG_DATA_HOME"); const char* dataHome = std::getenv("XDG_DATA_HOME");
if (dataHome && strlen(dataHome) != 0) if (dataHome && strlen(dataHome) != 0)
{ {
locations.push_back(dataHome); locations.push_back(std::string(dataHome) + "/gBar");
} }
else if (home) else if (home)
{ {
locations.push_back(std::string(home) + "/.local/share"); locations.push_back(std::string(home) + "/.local/share/gBar");
} }
const char* dataDirs = std::getenv("XDG_DATA_DIRS"); const char* dataDirs = std::getenv("XDG_DATA_DIRS");
@ -42,18 +47,18 @@ namespace CSS
std::stringstream ss(dataDirs); std::stringstream ss(dataDirs);
std::string dir; std::string dir;
while (std::getline(ss, dir, ':')) while (std::getline(ss, dir, ':'))
locations.push_back(dir); locations.push_back(dir + "/gBar");
} }
else else
{ {
locations.push_back("/usr/local/share"); locations.push_back("/usr/local/share/gBar");
locations.push_back("/usr/share"); locations.push_back("/usr/share/gBar");
} }
GError* err = nullptr; GError* err = nullptr;
for (auto& dir : locations) for (auto& dir : locations)
{ {
std::string file = dir + "/gBar/style.css"; std::string file = dir + "/style.css";
if (!std::ifstream(file).is_open()) if (!std::ifstream(file).is_open())
{ {

View file

@ -1,8 +1,9 @@
#pragma once #pragma once
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <string>
namespace CSS namespace CSS
{ {
void Load(); void Load(const std::string& overrideConfigLocation);
GtkCssProvider* GetProvider(); GtkCssProvider* GetProvider();
} }

View file

@ -197,11 +197,15 @@ void AddConfigVar(const std::string& propertyName, MapLike& propertyToSet, std::
} }
} }
void Config::Load() void Config::Load(const std::string& overrideConfigLocation)
{ {
const char* xdgConfigHome = getenv("XDG_CONFIG_HOME"); const char* xdgConfigHome = getenv("XDG_CONFIG_HOME");
std::ifstream file; std::ifstream file;
if (xdgConfigHome) if (overrideConfigLocation != "")
{
file = std::ifstream(overrideConfigLocation + "/config");
}
else if (xdgConfigHome)
{ {
file = std::ifstream(std::string(xdgConfigHome) + "/gBar/config"); file = std::ifstream(std::string(xdgConfigHome) + "/gBar/config");
} }

View file

@ -91,7 +91,7 @@ public:
double audioMinVolume = 0.f; // Map the minimum volume to this value double audioMinVolume = 0.f; // Map the minimum volume to this value
double audioMaxVolume = 100.f; // Map the maximum volume to this value double audioMaxVolume = 100.f; // Map the maximum volume to this value
static void Load(); static void Load(const std::string& overrideConfigLocation);
static const Config& Get(); static const Config& Get();
}; };

View file

@ -658,11 +658,11 @@ namespace System
system(Config::Get().suspendCommand.c_str()); system(Config::Get().suspendCommand.c_str());
} }
void Init() void Init(const std::string& overrideConfigLocation)
{ {
Logging::Init(); Logging::Init();
Config::Load(); Config::Load(overrideConfigLocation);
Wayland::Init(); Wayland::Init();

View file

@ -118,6 +118,6 @@ namespace System
void Lock(); void Lock();
void Suspend(); void Suspend();
void Init(); void Init(const std::string& overrideConfigLocation);
void FreeResources(); void FreeResources();
} }

View file

@ -19,12 +19,12 @@ Window::~Window()
} }
} }
void Window::Init(int argc, char** argv) void Window::Init(const std::string& overideConfigLocation)
{ {
gtk_init(&argc, &argv); gtk_init(NULL, NULL);
// Style // Style
CSS::Load(); CSS::Load(overideConfigLocation);
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), (GtkStyleProvider*)CSS::GetProvider(), GTK_STYLE_PROVIDER_PRIORITY_USER); gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), (GtkStyleProvider*)CSS::GetProvider(), GTK_STYLE_PROVIDER_PRIORITY_USER);

View file

@ -27,7 +27,7 @@ public:
Window& operator=(Window&& other) noexcept = default; Window& operator=(Window&& other) noexcept = default;
~Window(); ~Window();
void Init(int argc, char** argv); void Init(const std::string& overrideConfigLocation);
void Run(); void Run();
void Close(); void Close();

View file

@ -55,24 +55,27 @@ void PrintHelp()
"\tgBar [OPTIONS...] WIDGET [MONITOR]\n" "\tgBar [OPTIONS...] WIDGET [MONITOR]\n"
"\n" "\n"
"Sample usage:\n" "Sample usage:\n"
"\tgBar bar 0\tOpens the status bar on monitor 0\n" "\tgBar bar 0 \tOpens the status bar on monitor 0\n"
"\tgBar audio\tOpens the audio flyin on the current monitor\n" "\tgBar audio \tOpens the audio flyin on the current monitor\n"
"\n" "\n"
"All options:\n" "All options:\n"
"\t--help/-h \tPrints this help page and exits afterwards\n" "\t--help/-h \tPrints this help page and exits afterwards\n"
"\t--config/-c DIR\tOverrides the config search path to DIR and appends DIR to the CSS search path.\n"
"\t \t DIR cannot contain path shorthands like e.g. \"~\""
"\n" "\n"
"All available widgets:\n" "All available widgets:\n"
"\tbar \tThe main status bar\n" "\tbar \tThe main status bar\n"
"\taudio \tAn audio volume slider flyin\n" "\taudio \tAn audio volume slider flyin\n"
"\tmic \tA microphone volume slider flyin\n" "\tmic \tA microphone volume slider flyin\n"
"\tbluetooth \tA bluetooth connection widget\n" "\tbluetooth \tA bluetooth connection widget\n"
"\t[plugin] \tTries to open and run the plugin lib[plugin].so\n"); "\t[plugin] \tTries to open and run the plugin lib[plugin].so\n");
} }
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
std::string widget; std::string widget;
int32_t monitor = -1; int32_t monitor = -1;
std::string overrideConfigLocation = "";
// Arg parsing // Arg parsing
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
@ -103,6 +106,12 @@ int main(int argc, char** argv)
PrintHelp(); PrintHelp();
return 0; return 0;
} }
else if (arg == "-c" || arg == "--config")
{
ASSERT(i + 1 < argc, "Not enough arguments provided for -c/--config!");
overrideConfigLocation = argv[i + 1];
i += 1;
}
else else
{ {
LOG("Warning: Unknown CLI option \"" << arg << "\"") LOG("Warning: Unknown CLI option \"" << arg << "\"")
@ -122,10 +131,10 @@ int main(int argc, char** argv)
} }
signal(SIGINT, CloseTmpFiles); signal(SIGINT, CloseTmpFiles);
System::Init(); System::Init(overrideConfigLocation);
Window window(monitor); Window window(monitor);
window.Init(argc, argv); window.Init(overrideConfigLocation);
if (widget == "bar") if (widget == "bar")
{ {
Bar::Create(window, monitor); Bar::Create(window, monitor);