Add initial framework for more powerful formatting of command output and

use it for list-{panes,windows,sessions}. This allows more descriptive
replacements (such as #{session_name}) and conditionals.

Later this will be used for status_replace and list-keys and other
places.
This commit is contained in:
Nicholas Marriott
2011-08-26 10:53:16 +00:00
parent 4697b35d4f
commit 4a5dff3f11
7 changed files with 578 additions and 96 deletions

View File

@ -31,40 +31,45 @@ int cmd_list_sessions_exec(struct cmd *, struct cmd_ctx *);
const struct cmd_entry cmd_list_sessions_entry = {
"list-sessions", "ls",
"", 0, 0,
"",
"F:", 0, 0,
"[-F format]",
0,
NULL,
NULL,
cmd_list_sessions_exec
};
/* ARGSUSED */
int
cmd_list_sessions_exec(unused struct cmd *self, struct cmd_ctx *ctx)
cmd_list_sessions_exec(struct cmd *self, struct cmd_ctx *ctx)
{
struct args *args = self->args;
struct session *s;
struct session_group *sg;
char *tim, tmp[64];
u_int idx;
time_t t;
u_int n;
struct format_tree *ft;
const char *template;
char *line;
template = args_get(args, 'F');
if (template == NULL) {
template = "#{session_name}: #{session_windows} windows "
"(created #{session_created_string}) [#{session_width}x"
"#{session_height}]#{?session_grouped, (group ,}"
"#{session_group}#{?session_grouped,),}"
"#{?session_attached, (attached),}";
}
n = 0;
RB_FOREACH(s, sessions, &sessions) {
sg = session_group_find(s);
if (sg == NULL)
*tmp = '\0';
else {
idx = session_group_index(sg);
xsnprintf(tmp, sizeof tmp, " (group %u)", idx);
}
ft = format_create();
format_add(ft, "line", "%u", n);
format_session(ft, s);
t = s->creation_time.tv_sec;
tim = ctime(&t);
*strchr(tim, '\n') = '\0';
line = format_expand(ft, template);
ctx->print(ctx, "%s", line);
xfree(line);
ctx->print(ctx, "%s: %u windows (created %s) [%ux%u]%s%s",
s->name, winlink_count(&s->windows), tim, s->sx, s->sy,
tmp, s->flags & SESSION_UNATTACHED ? "" : " (attached)");
format_free(ft);
n++;
}
return (0);