Merge pull request #2058 from RyanDwyer/refactor-parse-title-format

Refactor parse_title_format
This commit is contained in:
emersion 2018-05-28 09:18:07 +01:00 committed by GitHub
commit 015878e5db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -641,6 +641,14 @@ void view_child_destroy(struct sway_view_child *child) {
} }
} }
static size_t append_prop(char *buffer, const char *value) {
if (!value) {
return 0;
}
lenient_strcat(buffer, value);
return strlen(value);
}
/** /**
* Calculate and return the length of the formatted title. * Calculate and return the length of the formatted title.
* If buffer is not NULL, also populate the buffer with the formatted title. * If buffer is not NULL, also populate the buffer with the formatted title.
@ -653,16 +661,6 @@ static size_t parse_title_format(struct sway_view *view, char *buffer) {
} }
return title ? strlen(title) : 0; return title ? strlen(title) : 0;
} }
const char *title = view_get_title(view);
const char *app_id = view_get_app_id(view);
const char *class = view_get_class(view);
const char *instance = view_get_instance(view);
const char *shell = view_get_shell(view);
size_t title_len = title ? strlen(title) : 0;
size_t app_id_len = app_id ? strlen(app_id) : 0;
size_t class_len = class ? strlen(class) : 0;
size_t instance_len = instance ? strlen(instance) : 0;
size_t shell_len = shell ? strlen(shell) : 0;
size_t len = 0; size_t len = 0;
char *format = view->title_format; char *format = view->title_format;
@ -674,24 +672,19 @@ static size_t parse_title_format(struct sway_view *view, char *buffer) {
format = next; format = next;
if (strncmp(next, "%title", 6) == 0) { if (strncmp(next, "%title", 6) == 0) {
lenient_strcat(buffer, title); len += append_prop(buffer, view_get_title(view));
len += title_len;
format += 6; format += 6;
} else if (strncmp(next, "%app_id", 7) == 0) { } else if (strncmp(next, "%app_id", 7) == 0) {
lenient_strcat(buffer, app_id); len += append_prop(buffer, view_get_app_id(view));
len += app_id_len;
format += 7; format += 7;
} else if (strncmp(next, "%class", 6) == 0) { } else if (strncmp(next, "%class", 6) == 0) {
lenient_strcat(buffer, class); len += append_prop(buffer, view_get_class(view));
len += class_len;
format += 6; format += 6;
} else if (strncmp(next, "%instance", 9) == 0) { } else if (strncmp(next, "%instance", 9) == 0) {
lenient_strcat(buffer, instance); len += append_prop(buffer, view_get_instance(view));
len += instance_len;
format += 9; format += 9;
} else if (strncmp(next, "%shell", 6) == 0) { } else if (strncmp(next, "%shell", 6) == 0) {
lenient_strcat(buffer, shell); len += append_prop(buffer, view_get_shell(view));
len += shell_len;
format += 6; format += 6;
} else { } else {
lenient_strcat(buffer, "%"); lenient_strcat(buffer, "%");