From 64310051694fd5c82ccbcff9fda6488ea2dcfb96 Mon Sep 17 00:00:00 2001 From: nicm Date: Sun, 26 May 2019 12:02:42 +0000 Subject: [PATCH] Add a way to append or prepend to a format if the length has been limited. --- format.c | 48 ++++++++++++++++++++++++++++++++++-------------- tmux.1 | 11 +++++++++-- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/format.c b/format.c index b1f82cc6..c352d253 100644 --- a/format.c +++ b/format.c @@ -755,6 +755,16 @@ format_merge(struct format_tree *ft, struct format_tree *from) } } +/* Add item bits to tree. */ +static void +format_create_add_item(struct format_tree *ft, struct cmdq_item *item) +{ + if (item->cmd != NULL) + format_add(ft, "command", "%s", item->cmd->entry->name); + if (item->shared != NULL && item->shared->formats != NULL) + format_merge(ft, item->shared->formats); +} + /* Create a new tree. */ struct format_tree * format_create(struct client *c, struct cmdq_item *item, int tag, int flags) @@ -800,12 +810,8 @@ format_create(struct client *c, struct cmdq_item *item, int tag, int flags) format_add(ft, "window_menu", "%s", DEFAULT_WINDOW_MENU); format_add(ft, "pane_menu", "%s", DEFAULT_PANE_MENU); - if (item != NULL) { - if (item->cmd != NULL) - format_add(ft, "command", "%s", item->cmd->entry->name); - if (item->shared != NULL && item->shared->formats != NULL) - format_merge(ft, item->shared->formats); - } + if (item != NULL) + format_create_add_item(ft, item); return (ft); } @@ -1408,7 +1414,7 @@ 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; + const char *errptr, *copy, *cp, *marker; char *copy0, *condition, *found, *new; char *value, *left, *right; size_t valuelen; @@ -1448,12 +1454,16 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen, sub = fm; break; case '=': - if (fm->argc != 1) + if (fm->argc != 1 && fm->argc != 2) break; limit = strtonum(fm->argv[0], INT_MIN, INT_MAX, &errptr); if (errptr != NULL) limit = 0; + if (fm->argc == 2 && fm->argv[1] != NULL) + marker = fm->argv[1]; + else + marker = NULL; break; case 'l': modifiers |= FORMAT_LITERAL; @@ -1668,14 +1678,24 @@ done: /* Truncate the value if needed. */ if (limit > 0) { new = format_trim_left(value, limit); - format_log(ft, "applied length limit %d: %s", limit, new); - free(value); - value = new; + if (marker != NULL && strcmp(new, value) != 0) { + free(value); + xasprintf(&value, "%s%s", new, marker); + } else { + free(value); + value = new; + } + format_log(ft, "applied length limit %d: %s", limit, value); } else if (limit < 0) { new = format_trim_right(value, -limit); - format_log(ft, "applied length limit %d: %s", limit, new); - free(value); - value = new; + if (marker != NULL && strcmp(new, value) != 0) { + free(value); + xasprintf(&value, "%s%s", marker, new); + } else { + free(value); + value = new; + } + format_log(ft, "applied length limit %d: %s", limit, value); } /* Expand the buffer and copy in the value. */ diff --git a/tmux.1 b/tmux.1 index 5e1a223e..534f451e 100644 --- a/tmux.1 +++ b/tmux.1 @@ -3989,9 +3989,16 @@ a number and a colon. Positive numbers count from the start of the string and negative from the end, so .Ql #{=5:pane_title} -will include at most the first 5 characters of the pane title, or +will include at most the first five characters of the pane title, or .Ql #{=-5:pane_title} -the last 5 characters. +the last five characters. +A suffix or prefix may be given as a second argument - if provided then it is +appended or prepended to the string if the length has been trimmed, for example +.Ql #{=/5/...:pane_title} +will append +.Ql ... +if the pane title is more than five characters. +.Pp Prefixing a time variable with .Ql t: will convert it to a string, so if