Skip to content

Commit

Permalink
even more batching
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixKratz committed Sep 11, 2021
1 parent 1e13c87 commit 95923db
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 48 deletions.
8 changes: 8 additions & 0 deletions src/bar_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,11 @@ void bar_manager_handle_notification(struct bar_manager* bar_manager, char* cont
if (!name) return;
bar_manager_custom_events_trigger(bar_manager, name);
}

void bar_manager_freeze(struct bar_manager *bar_manager) {
bar_manager->frozen = true;
}

void bar_manager_unfreeze(struct bar_manager *bar_manager) {
bar_manager->frozen = false;
}
2 changes: 2 additions & 0 deletions src/bar_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ void bar_manager_set_height(struct bar_manager *bar_manager, uint32_t height);
void bar_manager_set_padding_left(struct bar_manager *bar_manager, uint32_t padding);
void bar_manager_set_padding_right(struct bar_manager *bar_manager, uint32_t padding);
void bar_manager_set_display(struct bar_manager *bar_manager, char *display);
void bar_manager_freeze(struct bar_manager *bar_manager);
void bar_manager_unfreeze(struct bar_manager *bar_manager);

void bar_manager_display_changed(struct bar_manager *bar_manager);
void bar_manager_refresh(struct bar_manager *bar_manager, bool forced);
Expand Down
124 changes: 76 additions & 48 deletions src/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ extern bool g_verbose;

#define DOMAIN_BATCH "batch"
#define COMMAND_BATCH_CONFIG "--config"
#define COMMAND_BATCH_ADD "--add"
#define COMMAND_BATCH_SET "--set"
#define COMMAND_BATCH_DEFAULT "--default"
#define COMMAND_BATCH_SUBSCRIBE "--subscribe"

#define DOMAIN_ADD "add"
#define COMMAND_ADD_ITEM "item"
Expand Down Expand Up @@ -486,60 +488,86 @@ static void handle_domain_config(FILE *rsp, struct token domain, char *message)
}
}

// Syntax: sketchybar -m batch --<key>=<value> ... <key>=<value>
static char* reformat_batch_key_value_pair(struct token token) {
char* key = NULL;
char* value = NULL;
get_key_value_pair(token.text, &key, &value);
if (!key) return NULL;
char* rbr_msg = malloc((strlen(key) + (value ? strlen(value) : 0) + 3) * sizeof(char));
pack_key_value_pair(rbr_msg, key, value);
return rbr_msg;
}

static char* get_batch_line(char** message) {
char* cursor = *message;
bool end_of_batch = false;
while (true) {
if (*cursor == '\0' && *(cursor + 1) == '\0') { end_of_batch = true; break; }
if (*cursor == '\0' && *(cursor + 1) == '-') break;
cursor++;
}
char* rbr_msg = malloc(sizeof(char) * (cursor - *message + 2));
memcpy(rbr_msg, *message, sizeof(char) * (cursor - *message + 1));
*(rbr_msg + (cursor - *message + 1)) = '\0';
if (end_of_batch) *message = cursor;
else *message = cursor + 1;
return rbr_msg;
}
// Syntax: sketchybar -m batch --<mode> <key>=<value> ... <key>=<value> \
// --<mode> <key>=<value> ... <key>=<value>
static void handle_domain_batch(FILE* rsp, struct token domain, char* message) {
bar_manager_freeze(&g_bar_manager);
struct token command = get_token(&message);

if (token_equals(command, COMMAND_BATCH_SET)) {
struct token name = get_token(&message);
int item_index_for_name = bar_manager_get_item_index_for_name(&g_bar_manager, name.text);
if (item_index_for_name < 0) {
printf("Name: %s not found in bar items \n", name.text);
return;
}
struct bar_item* bar_item = g_bar_manager.bar_items[item_index_for_name];
struct token token = get_token(&message);
while (token.text && token.length > 0) {
char* key = NULL;
char* value = NULL;
get_key_value_pair(token.text, &key, &value);
char* rbr_msg = malloc((strlen(key) + (value ? strlen(value) : 0) + 3) * sizeof(char));
pack_key_value_pair(rbr_msg, key, value);
if (!key) break;

bar_item_parse_set_message(bar_item, rbr_msg);
free(rbr_msg);
token = get_token(&message);
}
} else if (token_equals(command, COMMAND_BATCH_DEFAULT)) {
struct token token = get_token(&message);
while (token.text && token.length > 0) {
char* key = NULL;
char* value = NULL;
get_key_value_pair(token.text, &key, &value);
if (!key) break;
char* rbr_msg = malloc((strlen(key) + (value ? strlen(value) : 0) + 3) * sizeof(char));
pack_key_value_pair(rbr_msg, key, value);

handle_domain_default(rsp, domain, rbr_msg);
while (command.text && command.length > 0) {
if (token_equals(command, COMMAND_BATCH_SET)) {
struct token name = get_token(&message);
int item_index_for_name = bar_manager_get_item_index_for_name(&g_bar_manager, name.text);
if (item_index_for_name < 0) {
printf("Name: %s not found in bar items \n", name.text);
break;
}
struct bar_item* bar_item = g_bar_manager.bar_items[item_index_for_name];
struct token token = get_token(&message);
while (token.text && token.length > 0) {
char* rbr_msg = reformat_batch_key_value_pair(token);
if (!rbr_msg) break;
bar_item_parse_set_message(bar_item, rbr_msg);
free(rbr_msg);
if (message && message[0] == '-') break;
token = get_token(&message);
}
} else if (token_equals(command, COMMAND_BATCH_DEFAULT)) {
struct token token = get_token(&message);
while (token.text && token.length > 0) {
char* rbr_msg = reformat_batch_key_value_pair(token);
if (!rbr_msg) break;
handle_domain_default(rsp, domain, rbr_msg);
free(rbr_msg);
if (message && message[0] == '-') break;
token = get_token(&message);
}
} else if (token_equals(command, COMMAND_BATCH_CONFIG)) {
struct token token = get_token(&message);
while (token.text && token.length > 0) {
char* rbr_msg = reformat_batch_key_value_pair(token);
if (!rbr_msg) break;
handle_domain_config(rsp, domain, rbr_msg);
free(rbr_msg);
if (message && message[0] == '-') break;
token = get_token(&message);
}
} else if (token_equals(command, COMMAND_BATCH_ADD)) {
char* rbr_msg = get_batch_line(&message);
handle_domain_add(rsp, domain, rbr_msg);
free(rbr_msg);
token = get_token(&message);
}
} else if (token_equals(command, COMMAND_BATCH_CONFIG)) {
struct token token = get_token(&message);
while (token.text && token.length > 0) {
char* key = NULL;
char* value = NULL;
get_key_value_pair(token.text, &key, &value);
if (!key) break;
char* rbr_msg = malloc((strlen(key) + (value ? strlen(value) : 0) + 3) * sizeof(char));
pack_key_value_pair(rbr_msg, key, value);

if (key && value) handle_domain_config(rsp, domain, rbr_msg);
} else if (token_equals(command, COMMAND_BATCH_SUBSCRIBE)) {
char* rbr_msg = get_batch_line(&message);
handle_domain_subscribe(rsp, domain, rbr_msg);
free(rbr_msg);
token = get_token(&message);
}
command = get_token(&message);
}
bar_manager_unfreeze(&g_bar_manager);
bar_manager_refresh(&g_bar_manager, false);
}

Expand Down

0 comments on commit 95923db

Please sign in to comment.