~emersion/public-inbox

mako: Add --config flag v1 PROPOSED

Hi, thanks for your patch!

The idea looks good, but I'm not really a fan of adding a new
parse_config_arguments return code to re-load a different configuration
file.

I think it'd make more sense to process arguments in two passes: first
process the normal flags (such as --help, --config), then load the
config file, then process the style flags.

What do you think?
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/public-inbox/patches/8076/mbox | git am -3
Learn more about email & git

[PATCH mako] Add --config flag Export this patch

---
 config.c         | 25 ++++++++++++++++++++-----
 include/config.h |  1 +
 main.c           |  1 +
 3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/config.c b/config.c
index b58eb95..c9f20fd 100644
--- a/config.c
+++ b/config.c
@@ -52,6 +52,7 @@ void init_default_config(struct mako_config *config) {
 	config->hidden_style.spec.format = true;
 
 	config->output = strdup("");
+	config->config_path = strdup("");
 	config->layer = ZWLR_LAYER_SHELL_V1_LAYER_TOP;
 
 	config->max_visible = 5;
@@ -375,6 +376,10 @@ static bool apply_config_option(struct mako_config *config, const char *name,
 		free(config->output);
 		config->output = strdup(value);
 		return true;
+	} else if (strcmp(name, "config") == 0) {
+		free(config->config_path);
+		config->config_path = strdup(value);
+		return true;
 	} else if (strcmp(name, "layer") == 0) {
 		if (strcmp(value, "background") == 0) {
 			config->layer = ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND;
@@ -512,12 +517,16 @@ static bool file_exists(const char *path) {
 	return path && access(path, R_OK) != -1;
 }
 
-static char *get_config_path(void) {
+static char *get_config_path(char *custom_config_path) {
 	static const char *config_paths[] = {
 		"$HOME/.mako/config",
 		"$XDG_CONFIG_HOME/mako/config",
 	};
 
+	if (strcmp(custom_config_path, "")) {
+		config_paths[0] = custom_config_path;
+	}
+
 	if (!getenv("XDG_CONFIG_HOME")) {
 		char *home = getenv("HOME");
 		if (!home) {
@@ -545,7 +554,7 @@ static char *get_config_path(void) {
 }
 
 int load_config_file(struct mako_config *config) {
-	char *path = get_config_path();
+	char *path = get_config_path(config->config_path);
 	if (!path) {
 		return 0;
 	}
@@ -642,6 +651,7 @@ int load_config_file(struct mako_config *config) {
 int parse_config_arguments(struct mako_config *config, int argc, char **argv) {
 	static const struct option long_options[] = {
 		{"help", no_argument, 0, 'h'},
+		{"config", required_argument, 0, 'c'},
 		{"font", required_argument, 0, 0},
 		{"background-color", required_argument, 0, 0},
 		{"text-color", required_argument, 0, 0},
@@ -674,13 +684,16 @@ int parse_config_arguments(struct mako_config *config, int argc, char **argv) {
 		wl_container_of(config->criteria.next, root_criteria, link);
 
 	optind = 1;
+	bool custom_config_path = false;
 	while (1) {
 		int option_index = -1;
 		int c = getopt_long(argc, argv, "h", long_options, &option_index);
 		if (c < 0) {
 			break;
 		} else if (c == 'h') {
-			return 1;
+			return 2;
+		} else if (c == 'c') {
+			custom_config_path = true;
 		} else if (c != 0) {
 			return -1;
 		}
@@ -693,7 +706,7 @@ int parse_config_arguments(struct mako_config *config, int argc, char **argv) {
 		}
 	}
 
-	return 0;
+	return custom_config_path ? 1 : 0;
 }
 
 // Returns zero on success, negative on error, positive if we should exit
@@ -705,9 +718,11 @@ int reload_config(struct mako_config *config, int argc, char **argv) {
 	int config_status = load_config_file(&new_config);
 	int args_status = parse_config_arguments(&new_config, argc, argv);
 
-	if (args_status > 0) {
+	if (args_status > 1) {
 		finish_config(&new_config);
 		return args_status;
+	} else if (args_status == 1) {
+		config_status = load_config_file(&new_config);
 	} else if (config_status < 0 || args_status < 0) {
 		fprintf(stderr, "Failed to parse config\n");
 		finish_config(&new_config);
diff --git a/include/config.h b/include/config.h
index 420466d..8df7ac9 100644
--- a/include/config.h
+++ b/include/config.h
@@ -72,6 +72,7 @@ struct mako_config {
 
 	int32_t max_visible;
 	char *output;
+	char *config_path;
 	enum zwlr_layer_shell_v1_layer layer;
 	uint32_t anchor;
 	uint32_t sort_criteria; //enum mako_sort_criteria
diff --git a/main.c b/main.c
index f281b14..4961eed 100644
--- a/main.c
+++ b/main.c
@@ -15,6 +15,7 @@ static const char usage[] =
 	"Usage: mako [options...]\n"
 	"\n"
 	"  -h, --help                          Show help message and quit.\n"
+	"      --config <path>                 Custom config path.\n"
 	"      --font <font>                   Font family and size.\n"
 	"      --background-color <color>      Background color.\n"
 	"      --text-color <color>            Text color.\n"
-- 
2.23.0
Hi, thanks for your patch!

The idea looks good, but I'm not really a fan of adding a new
parse_config_arguments return code to re-load a different configuration
file.

I think it'd make more sense to process arguments in two passes: first
process the normal flags (such as --help, --config), then load the
config file, then process the style flags.

What do you think?
View this thread in the archives