cmd_bar: fix bar id issues

Allows bar-subcommand to be a valid bar-ids
Destroys runtime created bar if trying to use a config only subcommand
Allow subcommands (except for id) to be ids
master
Brian Ashworth 6 years ago
parent 2340b14eba
commit 84b28dc593

@ -38,6 +38,12 @@ static struct cmd_handler bar_config_handlers[] = {
{ "swaybar_command", bar_cmd_swaybar_command }, { "swaybar_command", bar_cmd_swaybar_command },
}; };
// Determines whether the subcommand is valid in any bar handler struct
static bool is_subcommand(char *name) {
return find_handler(name, bar_handlers, sizeof(bar_handlers)) ||
find_handler(name, bar_config_handlers, sizeof(bar_config_handlers));
}
struct cmd_results *cmd_bar(int argc, char **argv) { struct cmd_results *cmd_bar(int argc, char **argv) {
struct cmd_results *error = NULL; struct cmd_results *error = NULL;
if ((error = checkarg(argc, "bar", EXPECTED_AT_LEAST, 1))) { if ((error = checkarg(argc, "bar", EXPECTED_AT_LEAST, 1))) {
@ -47,8 +53,8 @@ struct cmd_results *cmd_bar(int argc, char **argv) {
bool spawn = false; bool spawn = false;
if (argc > 1) { if (argc > 1) {
struct bar_config *bar = NULL; struct bar_config *bar = NULL;
if (!find_handler(argv[0], bar_handlers, sizeof(bar_handlers)) if (!is_subcommand(argv[0]) ||
&& find_handler(argv[1], bar_handlers, sizeof(bar_handlers))) { (strcmp(argv[0], "id") != 0 && is_subcommand(argv[1]))) {
for (int i = 0; i < config->bars->length; ++i) { for (int i = 0; i < config->bars->length; ++i) {
struct bar_config *item = config->bars->items[i]; struct bar_config *item = config->bars->items[i];
if (strcmp(item->id, argv[0]) == 0) { if (strcmp(item->id, argv[0]) == 0) {
@ -106,6 +112,15 @@ struct cmd_results *cmd_bar(int argc, char **argv) {
if (config->reading) { if (config->reading) {
return config_subcommand(argv, argc, bar_config_handlers, return config_subcommand(argv, argc, bar_config_handlers,
sizeof(bar_config_handlers)); sizeof(bar_config_handlers));
} else if (spawn) {
for (int i = config->bars->length - 1; i >= 0; i--) {
struct bar_config *bar = config->bars->items[i];
if (bar == config->current_bar) {
list_del(config->bars, i);
free_bar_config(bar);
break;
}
}
} }
return cmd_results_new(CMD_INVALID, "bar", return cmd_results_new(CMD_INVALID, "bar",
"Can only be used in the config file."); "Can only be used in the config file.");

@ -13,6 +13,8 @@ struct cmd_results *bar_cmd_id(int argc, char **argv) {
const char *oldname = config->current_bar->id; const char *oldname = config->current_bar->id;
if (strcmp(name, oldname) == 0) { if (strcmp(name, oldname) == 0) {
return cmd_results_new(CMD_SUCCESS, NULL, NULL); // NOP return cmd_results_new(CMD_SUCCESS, NULL, NULL); // NOP
} else if (strcmp(name, "id") == 0) {
return cmd_results_new(CMD_INVALID, "id", "id cannot be 'id'");
} }
// check if id is used by a previously defined bar // check if id is used by a previously defined bar
for (int i = 0; i < config->bars->length; ++i) { for (int i = 0; i < config->bars->length; ++i) {

Loading…
Cancel
Save