Add format variables for the default formats for the various modes

(tree_mode_format and so on) and add a -a flag to display-message to
list variables with values.
This commit is contained in:
nicm 2019-03-18 14:10:25 +00:00
parent ce6be7afd4
commit 2628af573d
8 changed files with 74 additions and 7 deletions

View File

@ -39,8 +39,8 @@ const struct cmd_entry cmd_display_message_entry = {
.name = "display-message",
.alias = "display",
.args = { "c:pt:F:v", 0, 1 },
.usage = "[-pv] [-c target-client] [-F format] "
.args = { "ac:pt:F:v", 0, 1 },
.usage = "[-apv] [-c target-client] [-F format] "
CMD_TARGET_PANE_USAGE " [message]",
.target = { 't', CMD_FIND_PANE, 0 },
@ -49,6 +49,14 @@ const struct cmd_entry cmd_display_message_entry = {
.exec = cmd_display_message_exec
};
static void
cmd_display_message_each(const char *key, const char *value, void *arg)
{
struct cmdq_item *item = arg;
cmdq_print(item, "%s=%s", key, value);
}
static enum cmd_retval
cmd_display_message_exec(struct cmd *self, struct cmdq_item *item)
{
@ -91,6 +99,12 @@ cmd_display_message_exec(struct cmd *self, struct cmdq_item *item)
ft = format_create(item->client, item, FORMAT_NONE, flags);
format_defaults(ft, target_c, s, wl, wp);
if (args_has(args, 'a')) {
if (item != NULL)
format_each(ft, cmd_display_message_each, item);
return (CMD_RETURN_NORMAL);
}
msg = format_expand_time(ft, template);
if (args_has(self->args, 'p'))
cmdq_print(item, "%s", msg);

View File

@ -701,7 +701,9 @@ format_merge(struct format_tree *ft, struct format_tree *from)
struct format_tree *
format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
{
struct format_tree *ft;
struct format_tree *ft;
const struct window_mode **wm;
char tmp[64];
if (!event_initialized(&format_job_event)) {
evtimer_set(&format_job_event, format_job_timer, NULL);
@ -727,6 +729,14 @@ format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
format_add(ft, "socket_path", "%s", socket_path);
format_add_tv(ft, "start_time", &start_time);
for (wm = all_window_modes; *wm != NULL; wm++) {
if ((*wm)->default_format != NULL) {
xsnprintf(tmp, sizeof tmp, "%s_format", (*wm)->name);
tmp[strcspn(tmp, "-")] = '_';
format_add(ft, tmp, "%s", (*wm)->default_format);
}
}
if (item != NULL) {
if (item->cmd != NULL)
format_add(ft, "command", "%s", item->cmd->entry->name);
@ -755,6 +765,30 @@ format_free(struct format_tree *ft)
free(ft);
}
/* Walk each format. */
void
format_each(struct format_tree *ft, void (*cb)(const char *, const char *,
void *), void *arg)
{
struct format_entry *fe;
static char s[64];
RB_FOREACH(fe, format_entry_tree, &ft->tree) {
if (fe->t != 0) {
xsnprintf(s, sizeof s, "%lld", (long long)fe->t);
cb(fe->key, fe->value, s);
} else {
if (fe->value == NULL && fe->cb != NULL) {
fe->cb(ft, fe);
if (fe->value == NULL)
fe->value = xstrdup("");
}
cb(fe->key, fe->value, arg);
}
}
}
/* Add a key-value pair. */
void
format_add(struct format_tree *ft, const char *key, const char *fmt, ...)

9
tmux.1
View File

@ -4177,7 +4177,7 @@ option.
This command works only from inside
.Nm .
.It Xo Ic display-message
.Op Fl pv
.Op Fl apv
.Op Fl c Ar target-client
.Op Fl t Ar target-pane
.Op Ar message
@ -4200,9 +4200,10 @@ if
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.
.Fl v
prints verbose logging as the format is parsed and
.Fl a
lists the format variables and their values.
.El
.Sh BUFFERS
.Nm

4
tmux.h
View File

@ -704,6 +704,7 @@ struct screen_write_ctx {
struct window_mode_entry;
struct window_mode {
const char *name;
const char *default_format;
struct screen *(*init)(struct window_mode_entry *,
struct cmd_find_state *, struct args *);
@ -1587,6 +1588,8 @@ struct format_tree *format_create(struct client *, struct cmdq_item *, int,
void format_free(struct format_tree *);
void printflike(3, 4) format_add(struct format_tree *, const char *,
const char *, ...);
void format_each(struct format_tree *, void (*)(const char *,
const char *, void *), void *);
char *format_expand_time(struct format_tree *, const char *);
char *format_expand(struct format_tree *, const char *);
char *format_single(struct cmdq_item *, const char *,
@ -2156,6 +2159,7 @@ void screen_select_cell(struct screen *, struct grid_cell *,
/* window.c */
extern struct windows windows;
extern struct window_pane_tree all_window_panes;
extern const struct window_mode *all_window_modes[];
int window_cmp(struct window *, struct window *);
RB_PROTOTYPE(windows, window, entry, window_cmp);
int winlink_cmp(struct winlink *, struct winlink *);

View File

@ -41,6 +41,7 @@ static void window_buffer_key(struct window_mode_entry *,
const struct window_mode window_buffer_mode = {
.name = "buffer-mode",
.default_format = WINDOW_BUFFER_DEFAULT_FORMAT,
.init = window_buffer_init,
.free = window_buffer_free,

View File

@ -42,6 +42,7 @@ static void window_client_key(struct window_mode_entry *,
const struct window_mode window_client_mode = {
.name = "client-mode",
.default_format = WINDOW_CLIENT_DEFAULT_FORMAT,
.init = window_client_init,
.free = window_client_free,

View File

@ -55,6 +55,7 @@ static void window_tree_key(struct window_mode_entry *,
const struct window_mode window_tree_mode = {
.name = "tree-mode",
.default_format = WINDOW_TREE_DEFAULT_FORMAT,
.init = window_tree_init,
.free = window_tree_free,

View File

@ -62,6 +62,17 @@ static u_int next_window_pane_id;
static u_int next_window_id;
static u_int next_active_point;
/* List of window modes. */
const struct window_mode *all_window_modes[] = {
&window_buffer_mode,
&window_client_mode,
&window_clock_mode,
&window_copy_mode,
&window_tree_mode,
&window_view_mode,
NULL
};
static void window_destroy(struct window *);
static struct window_pane *window_pane_create(struct window *, u_int, u_int,