Refactor parse_title_format()

master
Ryan Dwyer 7 years ago
parent d10a0a8c41
commit da7d6642d3

@ -494,9 +494,17 @@ void view_child_destroy(struct sway_view_child *child) {
} }
} }
static char *parse_title_format(struct sway_view *view) { /**
* Calculate and return the length of the formatted title.
* If buffer is not NULL, also populate the buffer with the formatted title.
*/
static size_t parse_title_format(struct sway_view *view, char *buffer) {
if (!view->title_format || strcmp(view->title_format, "%title") == 0) { if (!view->title_format || strcmp(view->title_format, "%title") == 0) {
return strdup(view_get_title(view)); const char *title = view_get_title(view);
if (buffer) {
strcpy(buffer, title);
}
return strlen(title);
} }
const char *title = view_get_title(view); const char *title = view_get_title(view);
const char *class = view_get_class(view); const char *class = view_get_class(view);
@ -507,74 +515,56 @@ static char *parse_title_format(struct sway_view *view) {
size_t instance_len = instance ? strlen(instance) : 0; size_t instance_len = instance ? strlen(instance) : 0;
size_t shell_len = shell ? strlen(shell) : 0; size_t shell_len = shell ? strlen(shell) : 0;
// First, determine the length
size_t len = 0; size_t len = 0;
char *format = view->title_format; char *format = view->title_format;
char *next = strchr(format, '%'); char *next = strchr(format, '%');
while (next) { while (next) {
len += next - format; if (buffer) {
format = next;
if (strncmp(next, "%title", 6) == 0) {
len += title_len;
format += 6;
} else if (strncmp(next, "%class", 6) == 0) {
len += class_len;
format += 6;
} else if (strncmp(next, "%instance", 9) == 0) {
len += instance_len;
format += 9;
} else if (strncmp(next, "%shell", 6) == 0) {
len += shell_len;
format += 6;
} else {
++format;
++len;
}
next = strchr(format, '%');
}
len += strlen(format);
char *buffer = calloc(len + 1, 1);
if (!sway_assert(buffer, "Unable to allocate title string")) {
return NULL;
}
// Now build the title
format = view->title_format;
next = strchr(format, '%');
while (next) {
// Copy everything up to the % // Copy everything up to the %
strncat(buffer, format, next - format); strncat(buffer, format, next - format);
}
len += next - format;
format = next; format = next;
if (strncmp(next, "%title", 6) == 0) { if (strncmp(next, "%title", 6) == 0) {
if (title) { if (buffer && title) {
strcat(buffer, title); strcat(buffer, title);
} }
len += title_len;
format += 6; format += 6;
} else if (strncmp(next, "%class", 6) == 0) { } else if (strncmp(next, "%class", 6) == 0) {
if (class) { if (buffer && class) {
strcat(buffer, class); strcat(buffer, class);
} }
len += class_len;
format += 6; format += 6;
} else if (strncmp(next, "%instance", 9) == 0) { } else if (strncmp(next, "%instance", 9) == 0) {
if (instance) { if (buffer && instance) {
strcat(buffer, instance); strcat(buffer, instance);
} }
len += instance_len;
format += 9; format += 9;
} else if (strncmp(next, "%shell", 6) == 0) { } else if (strncmp(next, "%shell", 6) == 0) {
if (buffer) {
strcat(buffer, shell); strcat(buffer, shell);
}
len += shell_len;
format += 6; format += 6;
} else { } else {
if (buffer) {
strcat(buffer, "%"); strcat(buffer, "%");
}
++format; ++format;
++len;
} }
next = strchr(format, '%'); next = strchr(format, '%');
} }
if (buffer) {
strcat(buffer, format); strcat(buffer, format);
}
len += strlen(format);
return buffer; return len;
} }
void view_update_title(struct sway_view *view, bool force) { void view_update_title(struct sway_view *view, bool force) {
@ -592,18 +582,19 @@ void view_update_title(struct sway_view *view, bool force) {
} }
} }
if (title) {
if (view->swayc->name) {
free(view->swayc->name); free(view->swayc->name);
}
if (view->swayc->formatted_title) {
free(view->swayc->formatted_title); free(view->swayc->formatted_title);
if (title) {
size_t len = parse_title_format(view, NULL);
char *buffer = calloc(len + 1, 1);
if (!sway_assert(buffer, "Unable to allocate title string")) {
return;
} }
parse_title_format(view, buffer);
view->swayc->name = strdup(title); view->swayc->name = strdup(title);
view->swayc->formatted_title = parse_title_format(view); view->swayc->formatted_title = buffer;
} else { } else {
free(view->swayc->name);
free(view->swayc->formatted_title);
view->swayc->name = NULL; view->swayc->name = NULL;
view->swayc->formatted_title = NULL; view->swayc->formatted_title = NULL;
} }

Loading…
Cancel
Save