Add a way to append or prepend to a format if the length has been limited.

pull/1761/head
nicm 2019-05-26 12:02:42 +00:00
parent 6dee409981
commit 6431005169
2 changed files with 43 additions and 16 deletions

View File

@ -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. */

11
tmux.1
View File

@ -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