From 5d0504ee115a7edd4fe53476a2721c180b0cbc26 Mon Sep 17 00:00:00 2001 From: nicm Date: Mon, 25 Nov 2019 15:02:48 +0000 Subject: Allow multiple substitutions in a single format. --- format.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'format.c') diff --git a/format.c b/format.c index 71d4e116..4a377287 100644 --- a/format.c +++ b/format.c @@ -1560,15 +1560,15 @@ static int format_replace(struct format_tree *ft, const char *key, size_t keylen, char **buf, size_t *len, size_t *off) { - struct window_pane *wp = ft->wp; - const char *errptr, *copy, *cp, *marker = NULL; - char *copy0, *condition, *found, *new; - char *value, *left, *right; - size_t valuelen; - int modifiers = 0, limit = 0, j; - struct format_modifier *list, *fm, *cmp = NULL, *search = NULL; - struct format_modifier *sub = NULL; - u_int i, count; + struct window_pane *wp = ft->wp; + const char *errptr, *copy, *cp, *marker = NULL; + char *copy0, *condition, *found, *new; + char *value, *left, *right; + size_t valuelen; + int modifiers = 0, limit = 0, j; + struct format_modifier *list, *fm, *cmp = NULL, *search = NULL; + struct format_modifier **sub = NULL; + u_int i, count, nsub = 0; /* Make a copy of the key. */ copy = copy0 = xstrndup(key, keylen); @@ -1597,7 +1597,9 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen, case 's': if (fm->argc < 2) break; - sub = fm; + sub = xreallocarray (sub, nsub + 1, + sizeof *sub); + sub[nsub++] = fm; break; case '=': if (fm->argc < 1) @@ -1809,10 +1811,10 @@ done: } /* Perform substitution if any. */ - if (sub != NULL) { - left = format_expand(ft, sub->argv[0]); - right = format_expand(ft, sub->argv[1]); - new = format_sub(sub, value, left, right); + for (i = 0; i < nsub; i++) { + left = format_expand(ft, sub[i]->argv[0]); + right = format_expand(ft, sub[i]->argv[1]); + new = format_sub(sub[i], value, left, right); format_log(ft, "substitute '%s' to '%s': %s", left, right, new); free(value); value = new; @@ -1855,12 +1857,15 @@ done: format_log(ft, "replaced '%s' with '%s'", copy0, value); free(value); + free(sub); format_free_modifiers(list, count); free(copy0); return (0); fail: format_log(ft, "failed %s", copy0); + + free(sub); format_free_modifiers(list, count); free(copy0); return (-1); -- cgit From 1ebd8c123415a60960dcd088d75d13f761bd3b3b Mon Sep 17 00:00:00 2001 From: nicm Date: Mon, 25 Nov 2019 15:04:15 +0000 Subject: Add p format modifier for padding to width. --- format.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'format.c') diff --git a/format.c b/format.c index 4a377287..ffa9b8d5 100644 --- a/format.c +++ b/format.c @@ -1299,7 +1299,7 @@ format_build_modifiers(struct format_tree *ft, const char **s, u_int *count) } /* Now try single character with arguments. */ - if (strchr("mCs=", cp[0]) == NULL) + if (strchr("mCs=p", cp[0]) == NULL) break; c = cp[0]; @@ -1565,7 +1565,7 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen, char *copy0, *condition, *found, *new; char *value, *left, *right; size_t valuelen; - int modifiers = 0, limit = 0, j; + int modifiers = 0, limit = 0, width = 0, j; struct format_modifier *list, *fm, *cmp = NULL, *search = NULL; struct format_modifier **sub = NULL; u_int i, count, nsub = 0; @@ -1611,6 +1611,14 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen, if (fm->argc >= 2 && fm->argv[1] != NULL) marker = fm->argv[1]; break; + case 'p': + if (fm->argc < 1) + break; + width = strtonum(fm->argv[0], INT_MIN, INT_MAX, + &errptr); + if (errptr != NULL) + width = 0; + break; case 'l': modifiers |= FORMAT_LITERAL; break; @@ -1845,6 +1853,19 @@ done: format_log(ft, "applied length limit %d: %s", limit, value); } + /* Pad the value if needed. */ + if (width > 0) { + new = utf8_padcstr(value, width); + free(value); + value = new; + format_log(ft, "applied padding width %d: %s", width, value); + } else if (width < 0) { + new = utf8_rpadcstr(value, -width); + free(value); + value = new; + format_log(ft, "applied padding width %d: %s", width, value); + } + /* Expand the buffer and copy in the value. */ valuelen = strlen(value); while (*len - *off < valuelen + 1) { -- cgit