Add a -v flag to display-message to show verbose messages as the format

is parsed, this gives the user a way to debug problems with formats
rather than just being confronted with (for example) a blank status
line.
pull/1644/head
nicm 2019-03-15 10:04:13 +00:00
parent 25e2e22791
commit 27578815da
4 changed files with 154 additions and 54 deletions

View File

@ -39,8 +39,8 @@ const struct cmd_entry cmd_display_message_entry = {
.name = "display-message",
.alias = "display",
.args = { "c:pt:F:", 0, 1 },
.usage = "[-p] [-c target-client] [-F format] "
.args = { "c:pt:F:v", 0, 1 },
.usage = "[-pv] [-c target-client] [-F format] "
CMD_TARGET_PANE_USAGE " [message]",
.target = { 't', CMD_FIND_PANE, 0 },
@ -60,6 +60,7 @@ cmd_display_message_exec(struct cmd *self, struct cmdq_item *item)
const char *template;
char *msg;
struct format_tree *ft;
int flags;
if (args_has(args, 'F') && args->argc != 0) {
cmdq_error(item, "only one of -F or argument must be given");
@ -83,7 +84,11 @@ cmd_display_message_exec(struct cmd *self, struct cmdq_item *item)
target_c = c;
else
target_c = cmd_find_best_client(s);
ft = format_create(item->client, item, FORMAT_NONE, 0);
if (args_has(self->args, 'v'))
flags = FORMAT_VERBOSE;
else
flags = 0;
ft = format_create(item->client, item, FORMAT_NONE, flags);
format_defaults(ft, target_c, s, wl, wp);
msg = format_expand_time(ft, template);

190
format.c
View File

@ -208,6 +208,36 @@ static const char *format_lower[] = {
NULL /* z */
};
/* Is logging enabled? */
static inline int
format_logging(struct format_tree *ft)
{
return (log_get_level() != 0 || (ft->flags & FORMAT_VERBOSE));
}
/* Log a message if verbose. */
static void printflike(3, 4)
format_log1(struct format_tree *ft, const char *from, const char *fmt, ...)
{
va_list ap;
char *s;
static const char spaces[] = " ";
if (!format_logging(ft))
return;
va_start(ap, fmt);
vasprintf(&s, fmt, ap);
va_end(ap);
log_debug("%s: %s", from, s);
if (ft->item != NULL)
cmdq_print(ft->item, "#%.*s%s", ft->loop, spaces, s);
free(s);
}
#define format_log(ft, fmt, ...) format_log1(ft, __func__, fmt, ##__VA_ARGS__)
/* Format job update callback. */
static void
format_job_update(struct job *job)
@ -1153,6 +1183,7 @@ format_loop_sessions(struct format_tree *ft, const char *fmt)
valuelen = 1;
RB_FOREACH(s, sessions, &sessions) {
format_log(ft, "session loop: $%u", s->id);
expanded = format_single(item, fmt, ft->c, ft->s, NULL, NULL);
valuelen += strlen(expanded);
@ -1174,8 +1205,10 @@ format_loop_windows(struct format_tree *ft, const char *fmt)
size_t valuelen;
struct winlink *wl;
if (ft->s == NULL)
if (ft->s == NULL) {
format_log(ft, "window loop but no session");
return (NULL);
}
if (format_choose(ft, fmt, &all, &active, 0) != 0) {
all = xstrdup(fmt);
@ -1186,6 +1219,7 @@ format_loop_windows(struct format_tree *ft, const char *fmt)
valuelen = 1;
RB_FOREACH(wl, winlinks, &ft->s->windows) {
format_log(ft, "window loop: %u @%u", wl->idx, wl->window->id);
if (active != NULL && wl == ft->s->curw)
use = active;
else
@ -1214,8 +1248,10 @@ format_loop_panes(struct format_tree *ft, const char *fmt)
size_t valuelen;
struct window_pane *wp;
if (ft->w == NULL)
if (ft->w == NULL) {
format_log(ft, "pane loop but no window");
return (NULL);
}
if (format_choose(ft, fmt, &all, &active, 0) != 0) {
all = xstrdup(fmt);
@ -1226,6 +1262,7 @@ format_loop_panes(struct format_tree *ft, const char *fmt)
valuelen = 1;
TAILQ_FOREACH(wp, &ft->w->panes, entry) {
format_log(ft, "pane loop: %%%u", wp->id);
if (active != NULL && wp == ft->w->active)
use = active;
else
@ -1254,25 +1291,27 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
const char *errptr, *copy, *cp;
char *copy0, *condition, *found, *new;
char *value, *left, *right;
char tmp[64];
size_t valuelen;
int modifiers = 0, limit = 0;
struct format_modifier *list, *fm, *cmp = NULL, *search = NULL;
struct format_modifier *sub = NULL;
u_int i, count;
int j;
/* Make a copy of the key. */
copy = copy0 = xstrndup(key, keylen);
log_debug("%s: format = '%s'", __func__, copy);
/* Process modifier list. */
list = format_build_modifiers(ft, &copy, &count);
for (i = 0; i < count; i++) {
xsnprintf(tmp, sizeof tmp, "%s: modifier %u", __func__, i);
log_debug("%s = %s", tmp, list[i].modifier);
cmd_log_argv(list[i].argc, list[i].argv, tmp);
fm = &list[i];
if (format_logging(ft)) {
format_log(ft, "modifier %u is %s", i, fm->modifier);
for (j = 0; j < fm->argc; j++) {
format_log(ft, "modifier %u argument %d: %s", i,
j, fm->argv[j]);
}
}
if (fm->size == 1) {
switch (fm->modifier[0]) {
case 'm':
@ -1355,14 +1394,22 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
goto fail;
} else if (search != NULL) {
/* Search in pane. */
if (wp == NULL)
if (wp == NULL) {
format_log(ft, "search '%s' but no pane", copy);
value = xstrdup("0");
else
} else {
format_log(ft, "search '%s' pane %%%u", copy, wp->id);
xasprintf(&value, "%u", window_pane_search(wp, copy));
}
} else if (cmp != NULL) {
/* Comparison of left and right. */
if (format_choose(ft, copy, &left, &right, 1) != 0)
if (format_choose(ft, copy, &left, &right, 1) != 0) {
format_log(ft, "compare %s syntax error: %s",
cmp->modifier, copy);
goto fail;
}
format_log(ft, "compare %s left is: %s", cmp->modifier, left);
format_log(ft, "compare %s right is: %s", cmp->modifier, right);
if (strcmp(cmp->modifier, "||") == 0) {
if (format_true(left) || format_true(right))
@ -1397,9 +1444,12 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
} else if (*copy == '?') {
/* Conditional: check first and choose second or third. */
cp = format_skip(copy + 1, ",");
if (cp == NULL)
if (cp == NULL) {
format_log(ft, "condition syntax error: %s", copy + 1);
goto fail;
}
condition = xstrndup(copy + 1, cp - (copy + 1));
format_log(ft, "condition is: %s", condition);
found = format_find(ft, condition, modifiers);
if (found == NULL) {
@ -1412,27 +1462,42 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
if (strcmp(found, condition) == 0) {
free(found);
found = xstrdup("");
format_log(ft, "condition '%s' found: %s",
condition, found);
} else {
format_log(ft,
"condition '%s' not found; assuming false",
condition);
}
}
free(condition);
} else
format_log(ft, "condition '%s' found", condition);
if (format_choose(ft, cp + 1, &left, &right, 0) != 0) {
format_log(ft, "condition '%s' syntax error: %s",
condition, cp + 1);
free(found);
goto fail;
}
if (format_true(found))
if (format_true(found)) {
format_log(ft, "condition '%s' is true", condition);
value = format_expand(ft, left);
else
} else {
format_log(ft, "condition '%s' is false", condition);
value = format_expand(ft, right);
}
free(right);
free(left);
free(condition);
free(found);
} else {
/* Neither: look up directly. */
value = format_find(ft, copy, modifiers);
if (value == NULL)
if (value == NULL) {
format_log(ft, "format '%s' not found", copy);
value = xstrdup("");
} else
format_log(ft, "format '%s' found: %s", copy, value);
}
done:
@ -1451,6 +1516,8 @@ done:
/* Perform substitution if any. */
if (sub != NULL) {
new = format_substitute(value, sub->argv[0], sub->argv[1]);
format_log(ft, "substituted '%s' to '%s: %s", sub->argv[0],
sub->argv[1], new);
free(value);
value = new;
}
@ -1458,16 +1525,17 @@ done:
/* Truncate the value if needed. */
if (limit > 0) {
new = utf8_trimcstr(value, limit);
format_log(ft, "applied length limit %d: %s", limit, new);
free(value);
value = new;
} else if (limit < 0) {
new = utf8_rtrimcstr(value, -limit);
format_log(ft, "applied length limit %d: %s", limit, new);
free(value);
value = new;
}
/* Expand the buffer and copy in the value. */
log_debug("%s: '%s' -> '%s'", __func__, copy0, value);
valuelen = strlen(value);
while (*len - *off < valuelen + 1) {
*buf = xreallocarray(*buf, 2, *len);
@ -1476,50 +1544,51 @@ done:
memcpy(*buf + *off, value, valuelen);
*off += valuelen;
format_log(ft, "replaced '%s' with '%s'", copy0, value);
free(value);
format_free_modifiers(list, count);
free(copy0);
return (0);
fail:
format_log(ft, "failed %s", copy0);
format_free_modifiers(list, count);
free(copy0);
return (-1);
}
/* Expand keys in a template, passing through strftime first. */
char *
format_expand_time(struct format_tree *ft, const char *fmt)
{
struct tm *tm;
char s[2048];
if (fmt == NULL || *fmt == '\0')
return (xstrdup(""));
tm = localtime(&ft->time);
if (strftime(s, sizeof s, fmt, tm) == 0)
return (xstrdup(""));
return (format_expand(ft, s));
}
/* Expand keys in a template. */
char *
format_expand(struct format_tree *ft, const char *fmt)
static char *
format_expand1(struct format_tree *ft, const char *fmt, int time)
{
char *buf, *out, *name;
const char *ptr, *s, *saved = fmt;
const char *ptr, *s;
size_t off, len, n, outlen;
int ch, brackets;
struct tm *tm;
char expanded[8192];
if (fmt == NULL)
if (fmt == NULL || *fmt == '\0')
return (xstrdup(""));
if (ft->loop == FORMAT_LOOP_LIMIT)
return (xstrdup(""));
ft->loop++;
format_log(ft, "expanding format: %s", fmt);
if (time) {
tm = localtime(&ft->time);
if (strftime(expanded, sizeof expanded, fmt, tm) == 0) {
format_log(ft, "format is too long");
return (xstrdup(""));
}
if (format_logging(ft) && strcmp(expanded, fmt) != 0)
format_log(ft, "after time expanded: %s", expanded);
fmt = expanded;
}
len = 64;
buf = xmalloc(len);
off = 0;
@ -1535,7 +1604,7 @@ format_expand(struct format_tree *ft, const char *fmt)
}
fmt++;
ch = (u_char) *fmt++;
ch = (u_char)*fmt++;
switch (ch) {
case '(':
brackets = 1;
@ -1549,15 +1618,19 @@ format_expand(struct format_tree *ft, const char *fmt)
break;
n = ptr - fmt;
if (ft->flags & FORMAT_NOJOBS)
out = xstrdup("");
else {
name = xstrndup(fmt, n);
out = format_job_get(ft, name);
free(name);
}
outlen = strlen(out);
name = xstrndup(fmt, n);
format_log(ft, "found #(): %s", name);
if (ft->flags & FORMAT_NOJOBS) {
out = xstrdup("");
format_log(ft, "#() is disabled");
} else {
out = format_job_get(ft, name);
format_log(ft, "#() result: %s", out);
}
free(name);
outlen = strlen(out);
while (len - off < outlen + 1) {
buf = xreallocarray(buf, 2, len);
len *= 2;
@ -1575,6 +1648,7 @@ format_expand(struct format_tree *ft, const char *fmt)
break;
n = ptr - fmt;
format_log(ft, "found #{}: %.*s", (int)n, fmt);
if (format_replace(ft, fmt, n, &buf, &len, &off) != 0)
break;
fmt += n + 1;
@ -1582,6 +1656,7 @@ format_expand(struct format_tree *ft, const char *fmt)
case '}':
case '#':
case ',':
format_log(ft, "found #%c", ch);
while (len - off < 2) {
buf = xreallocarray(buf, 2, len);
len *= 2;
@ -1604,6 +1679,7 @@ format_expand(struct format_tree *ft, const char *fmt)
continue;
}
n = strlen(s);
format_log(ft, "found #%c: %s", ch, s);
if (format_replace(ft, s, n, &buf, &len, &off) != 0)
break;
continue;
@ -1613,12 +1689,26 @@ format_expand(struct format_tree *ft, const char *fmt)
}
buf[off] = '\0';
log_debug("%s: '%s' -> '%s'", __func__, saved, buf);
format_log(ft, "result is: %s", buf);
ft->loop--;
return (buf);
}
/* Expand keys in a template, passing through strftime first. */
char *
format_expand_time(struct format_tree *ft, const char *fmt)
{
return (format_expand1(ft, fmt, 1));
}
/* Expand keys in a template. */
char *
format_expand(struct format_tree *ft, const char *fmt)
{
return (format_expand1(ft, fmt, 0));
}
/* Expand a single string. */
char *
format_single(struct cmdq_item *item, const char *fmt, struct client *c,

6
tmux.1
View File

@ -4174,7 +4174,7 @@ option.
This command works only from inside
.Nm .
.It Xo Ic display-message
.Op Fl p
.Op Fl pv
.Op Fl c Ar target-client
.Op Fl t Ar target-pane
.Op Ar message
@ -4196,6 +4196,10 @@ if
.Fl t
is given, otherwise the active pane for the session attached to
.Ar target-client .
.Pp
With
.Fl v ,
verbose logging is printed as the format is parsed.
.El
.Sh BUFFERS
.Nm

1
tmux.h
View File

@ -1575,6 +1575,7 @@ char *paste_make_sample(struct paste_buffer *);
#define FORMAT_STATUS 0x1
#define FORMAT_FORCE 0x2
#define FORMAT_NOJOBS 0x4
#define FORMAT_VERBOSE 0x8
#define FORMAT_NONE 0
#define FORMAT_PANE 0x80000000U
#define FORMAT_WINDOW 0x40000000U