diff options
author | 2018-10-12 21:53:40 -0400 | |
---|---|---|
committer | 2018-10-13 17:56:11 -0400 | |
commit | 2a0c7ebd43a82197754586de32deb3cdbbfa3cd9 (patch) | |
tree | 871912b319fa448b4dea83be4cd0bd1fe5e4c8cd /sway | |
parent | cmd_bar: fix bar id issues (diff) | |
download | sway-2a0c7ebd43a82197754586de32deb3cdbbfa3cd9.tar.gz sway-2a0c7ebd43a82197754586de32deb3cdbbfa3cd9.tar.zst sway-2a0c7ebd43a82197754586de32deb3cdbbfa3cd9.zip |
cmd_bar: simplify logic
Diffstat (limited to 'sway')
-rw-r--r-- | sway/commands/bar.c | 62 |
1 files changed, 27 insertions, 35 deletions
diff --git a/sway/commands/bar.c b/sway/commands/bar.c index 8d118fb6..c808aef2 100644 --- a/sway/commands/bar.c +++ b/sway/commands/bar.c | |||
@@ -46,37 +46,34 @@ static bool is_subcommand(char *name) { | |||
46 | 46 | ||
47 | struct cmd_results *cmd_bar(int argc, char **argv) { | 47 | struct cmd_results *cmd_bar(int argc, char **argv) { |
48 | struct cmd_results *error = NULL; | 48 | struct cmd_results *error = NULL; |
49 | if ((error = checkarg(argc, "bar", EXPECTED_AT_LEAST, 1))) { | 49 | if ((error = checkarg(argc, "bar", EXPECTED_AT_LEAST, 2))) { |
50 | return error; | 50 | return error; |
51 | } | 51 | } |
52 | 52 | ||
53 | bool spawn = false; | 53 | bool spawn = false; |
54 | if (argc > 1) { | 54 | struct bar_config *bar = NULL; |
55 | struct bar_config *bar = NULL; | 55 | if (strcmp(argv[0], "id") != 0 && is_subcommand(argv[1])) { |
56 | if (!is_subcommand(argv[0]) || | 56 | for (int i = 0; i < config->bars->length; ++i) { |
57 | (strcmp(argv[0], "id") != 0 && is_subcommand(argv[1]))) { | 57 | struct bar_config *item = config->bars->items[i]; |
58 | for (int i = 0; i < config->bars->length; ++i) { | 58 | if (strcmp(item->id, argv[0]) == 0) { |
59 | struct bar_config *item = config->bars->items[i]; | 59 | wlr_log(WLR_DEBUG, "Selecting bar: %s", argv[0]); |
60 | if (strcmp(item->id, argv[0]) == 0) { | 60 | bar = item; |
61 | wlr_log(WLR_DEBUG, "Selecting bar: %s", argv[0]); | 61 | break; |
62 | bar = item; | ||
63 | break; | ||
64 | } | ||
65 | } | 62 | } |
63 | } | ||
64 | if (!bar) { | ||
65 | spawn = !config->reading; | ||
66 | wlr_log(WLR_DEBUG, "Creating bar: %s", argv[0]); | ||
67 | bar = default_bar_config(); | ||
66 | if (!bar) { | 68 | if (!bar) { |
67 | spawn = !config->reading; | 69 | return cmd_results_new(CMD_FAILURE, "bar", |
68 | wlr_log(WLR_DEBUG, "Creating bar: %s", argv[0]); | 70 | "Unable to allocate bar state"); |
69 | bar = default_bar_config(); | ||
70 | if (!bar) { | ||
71 | return cmd_results_new(CMD_FAILURE, "bar", | ||
72 | "Unable to allocate bar state"); | ||
73 | } | ||
74 | |||
75 | bar->id = strdup(argv[0]); | ||
76 | } | 71 | } |
77 | config->current_bar = bar; | 72 | |
78 | ++argv; --argc; | 73 | bar->id = strdup(argv[0]); |
79 | } | 74 | } |
75 | config->current_bar = bar; | ||
76 | ++argv; --argc; | ||
80 | } | 77 | } |
81 | 78 | ||
82 | if (!config->current_bar && config->reading) { | 79 | if (!config->current_bar && config->reading) { |
@@ -88,18 +85,13 @@ struct cmd_results *cmd_bar(int argc, char **argv) { | |||
88 | } | 85 | } |
89 | 86 | ||
90 | // set bar id | 87 | // set bar id |
91 | for (int i = 0; i < config->bars->length; ++i) { | 88 | const int len = 5 + numlen(config->bars->length - 1); // "bar-"+i+\0 |
92 | if (bar == config->bars->items[i]) { | 89 | bar->id = malloc(len * sizeof(char)); |
93 | const int len = 5 + numlen(i); // "bar-" + i + \0 | 90 | if (bar->id) { |
94 | bar->id = malloc(len * sizeof(char)); | 91 | snprintf(bar->id, len, "bar-%d", config->bars->length - 1); |
95 | if (bar->id) { | 92 | } else { |
96 | snprintf(bar->id, len, "bar-%d", i); | 93 | return cmd_results_new(CMD_FAILURE, |
97 | } else { | 94 | "bar", "Unable to allocate bar ID"); |
98 | return cmd_results_new(CMD_FAILURE, | ||
99 | "bar", "Unable to allocate bar ID"); | ||
100 | } | ||
101 | break; | ||
102 | } | ||
103 | } | 95 | } |
104 | 96 | ||
105 | // Set current bar | 97 | // Set current bar |