Skip to content

Commit

Permalink
Switch all of the various choose- and list- commands over to the format
Browse files Browse the repository at this point in the history
infrastructure, from Thomas Adam.
  • Loading branch information
nicm committed May 22, 2012
1 parent 682884e commit ebf94bc
Show file tree
Hide file tree
Showing 16 changed files with 213 additions and 114 deletions.
7 changes: 4 additions & 3 deletions cmd-break-pane.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ cmd_break_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
server_status_session_group(s);

if (args_has(args, 'P')) {
template = "#{session_name}:#{window_index}";
if (args_has(args, 'F'))
template = args_get(args, 'F');

if ((template = args_get(args, 'F')) == NULL)
template = DEFAULT_PANE_INFO_TEMPLATE;

ft = format_create();
if ((c = cmd_find_client(ctx, NULL)) != NULL)
format_client(ft, c);
Expand Down
24 changes: 17 additions & 7 deletions cmd-choose-buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ void cmd_choose_buffer_free(void *);

const struct cmd_entry cmd_choose_buffer_entry = {
"choose-buffer", NULL,
"t:", 0, 1,
CMD_TARGET_WINDOW_USAGE " [template]",
"F:t:", 0, 1,
CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
0,
NULL,
NULL,
Expand All @@ -53,14 +53,19 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
struct cmd_choose_buffer_data *cdata;
struct winlink *wl;
struct paste_buffer *pb;
struct format_tree *ft;
u_int idx;
char *tmp;
char *line;
const char *template;

if (ctx->curclient == NULL) {
ctx->error(ctx, "must be run interactively");
return (-1);
}

if ((template = args_get(args, 'F')) == NULL)
template = DEFAULT_BUFFER_LIST_TEMPLATE;

if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
return (-1);

Expand All @@ -72,10 +77,15 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)

idx = 0;
while ((pb = paste_walk_stack(&global_buffers, &idx)) != NULL) {
tmp = paste_print(pb, 50);
window_choose_add(wl->window->active, idx - 1,
"%u: %zu bytes: \"%s\"", idx - 1, pb->size, tmp);
xfree(tmp);
ft = format_create();
format_add(ft, "line", "%u", idx - 1);
format_paste_buffer(ft, pb);

line = format_expand(ft, template);
window_choose_add(wl->window->active, idx - 1, "%s", line);

xfree(line);
format_free(ft);
}

cdata = xmalloc(sizeof *cdata);
Expand Down
26 changes: 18 additions & 8 deletions cmd-choose-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ void cmd_choose_client_free(void *);

const struct cmd_entry cmd_choose_client_entry = {
"choose-client", NULL,
"t:", 0, 1,
CMD_TARGET_WINDOW_USAGE " [template]",
"F:t:", 0, 1,
CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
0,
NULL,
NULL,
Expand All @@ -51,8 +51,11 @@ cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx)
{
struct args *args = self->args;
struct cmd_choose_client_data *cdata;
struct format_tree *ft;
struct winlink *wl;
struct client *c;
char *line;
const char *template;
u_int i, idx, cur;

if (ctx->curclient == NULL) {
Expand All @@ -66,6 +69,9 @@ cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx)
if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
return (0);

if ((template = args_get(args, 'F')) == NULL)
template = DEFAULT_CLIENT_TEMPLATE;

cur = idx = 0;
for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
c = ARRAY_ITEM(&clients, i);
Expand All @@ -75,12 +81,16 @@ cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx)
cur = idx;
idx++;

window_choose_add(wl->window->active, i,
"%s: %s [%ux%u %s]%s%s", c->tty.path,
c->session->name, c->tty.sx, c->tty.sy,
c->tty.termname,
c->tty.flags & TTY_UTF8 ? " (utf8)" : "",
c->flags & CLIENT_READONLY ? " (ro)" : "");
ft = format_create();
format_add(ft, "line", "%u", i);
format_session(ft, c->session);
format_client(ft, c);

line = format_expand(ft, template);
window_choose_add(wl->window->active, i, "%s", line);
xfree(line);

format_free(ft);
}

cdata = xmalloc(sizeof *cdata);
Expand Down
33 changes: 17 additions & 16 deletions cmd-choose-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ void cmd_choose_session_free(void *);

const struct cmd_entry cmd_choose_session_entry = {
"choose-session", NULL,
"t:", 0, 1,
CMD_TARGET_WINDOW_USAGE " [template]",
"F:t:", 0, 1,
CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
0,
NULL,
NULL,
Expand All @@ -53,9 +53,10 @@ cmd_choose_session_exec(struct cmd *self, struct cmd_ctx *ctx)
struct cmd_choose_session_data *cdata;
struct winlink *wl;
struct session *s;
struct session_group *sg;
u_int idx, sgidx, cur;
char tmp[64];
struct format_tree *ft;
const char *template;
char *line;
u_int idx, cur;

if (ctx->curclient == NULL) {
ctx->error(ctx, "must be run interactively");
Expand All @@ -68,24 +69,24 @@ cmd_choose_session_exec(struct cmd *self, struct cmd_ctx *ctx)
if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
return (0);

if ((template = args_get(args, 'F')) == NULL)
template = DEFAULT_SESSION_TEMPLATE;

cur = idx = 0;
RB_FOREACH(s, sessions, &sessions) {
if (s == ctx->curclient->session)
cur = idx;
idx++;

sg = session_group_find(s);
if (sg == NULL)
*tmp = '\0';
else {
sgidx = session_group_index(sg);
xsnprintf(tmp, sizeof tmp, " (group %u)", sgidx);
}
ft = format_create();
format_add(ft, "line", "%u", idx);
format_session(ft, s);

line = format_expand(ft, template);
window_choose_add(wl->window->active, s->idx, "%s", line);
xfree(line);

window_choose_add(wl->window->active, s->idx,
"%s: %u windows [%ux%u]%s%s", s->name,
winlink_count(&s->windows), s->sx, s->sy,
tmp, s->flags & SESSION_UNATTACHED ? "" : " (attached)");
format_free(ft);
}

cdata = xmalloc(sizeof *cdata);
Expand Down
41 changes: 18 additions & 23 deletions cmd-choose-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ void cmd_choose_window_free(void *);

const struct cmd_entry cmd_choose_window_entry = {
"choose-window", NULL,
"t:", 0, 1,
CMD_TARGET_WINDOW_USAGE " [template]",
"F:t:", 0, 1,
CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
0,
NULL,
NULL,
Expand All @@ -54,10 +54,10 @@ cmd_choose_window_exec(struct cmd *self, struct cmd_ctx *ctx)
struct cmd_choose_window_data *cdata;
struct session *s;
struct winlink *wl, *wm;
struct window *w;
struct format_tree *ft;
const char *template;
char *line;
u_int idx, cur;
char *flags, *title;
const char *left, *right;

if (ctx->curclient == NULL) {
ctx->error(ctx, "must be run interactively");
Expand All @@ -71,30 +71,25 @@ cmd_choose_window_exec(struct cmd *self, struct cmd_ctx *ctx)
if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
return (0);

if ((template = args_get(args, 'F')) == NULL)
template = DEFAULT_WINDOW_TEMPLATE;

cur = idx = 0;
RB_FOREACH(wm, winlinks, &s->windows) {
w = wm->window;

if (wm == s->curw)
cur = idx;
idx++;

flags = window_printable_flags(s, wm);
title = w->active->screen->title;
if (wm == wl)
title = w->active->base.title;
left = " \"";
right = "\"";
if (*title == '\0')
left = right = "";

window_choose_add(wl->window->active,
wm->idx, "%3d: %s%s [%ux%u] (%u panes%s)%s%s%s",
wm->idx, w->name, flags, w->sx, w->sy, window_count_panes(w),
w->active->fd == -1 ? ", dead" : "",
left, title, right);

xfree(flags);
ft = format_create();
format_add(ft, "line", "%u", idx);
format_session(ft, s);
format_winlink(ft, s, wm);

line = format_expand(ft, template);
window_choose_add(wl->window->active, idx, "%s", line);

xfree(line);
format_free(ft);
}

cdata = xmalloc(sizeof *cdata);
Expand Down
2 changes: 1 addition & 1 deletion cmd-display-message.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ cmd_display_message_exec(struct cmd *self, struct cmd_ctx *ctx)
if (args->argc != 0)
template = args->argv[0];
if (template == NULL)
template = "[#S] #I:#W, current pane #P - (%H:%M %d-%b-%y)";
template = DEFAULT_DISPLAY_MESSAGE_TEMPLATE;

ft = format_create();
format_client(ft, c);
Expand Down
30 changes: 21 additions & 9 deletions cmd-find-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ void cmd_find_window_free(void *);

const struct cmd_entry cmd_find_window_entry = {
"find-window", "findw",
"CNt:T", 1, 4,
"[-CNT] " CMD_TARGET_WINDOW_USAGE " match-string",
"F:CNt:T", 1, 4,
"[-CNT] [-F format] " CMD_TARGET_WINDOW_USAGE " match-string",
0,
NULL,
NULL,
Expand Down Expand Up @@ -85,11 +85,13 @@ cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
struct cmd_find_window_data *cdata;
struct session *s;
struct winlink *wl, *wm;
struct window *w;
struct window_pane *wp;
struct format_tree *ft;
ARRAY_DECL(, u_int) list_idx;
ARRAY_DECL(, char *) list_ctx;
char *str, *sres, *sctx, *searchstr;
char *find_line;
const char *template;
u_int i, line, match_flags;

if (ctx->curclient == NULL) {
Expand All @@ -101,6 +103,9 @@ cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
return (-1);

if ((template = args_get(args, 'F')) == NULL)
template = DEFAULT_FIND_WINDOW_TEMPLATE;

match_flags = cmd_find_window_match_flags(args);
str = args->argv[0];

Expand Down Expand Up @@ -167,13 +172,20 @@ cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
for (i = 0; i < ARRAY_LENGTH(&list_idx); i++) {
wm = winlink_find_by_index(
&s->windows, ARRAY_ITEM(&list_idx, i));
w = wm->window;

sctx = ARRAY_ITEM(&list_ctx, i);
window_choose_add(wl->window->active,
wm->idx, "%3d: %s [%ux%u] (%u panes) %s", wm->idx, w->name,
w->sx, w->sy, window_count_panes(w), sctx);
xfree(sctx);
ft = format_create();
format_add(ft, "line", "%u", i);
format_add(ft, "window_find_matches", "%s",
ARRAY_ITEM(&list_ctx, i));
format_session(ft, s);
format_winlink(ft, s, wm);

find_line = format_expand(ft, template);

window_choose_add(wl->window->active, wm->idx, "%s", find_line);

xfree(find_line);
format_free(ft);
}

cdata = xmalloc(sizeof *cdata);
Expand Down
25 changes: 18 additions & 7 deletions cmd-list-buffers.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ int cmd_list_buffers_exec(struct cmd *, struct cmd_ctx *);

const struct cmd_entry cmd_list_buffers_entry = {
"list-buffers", "lsb",
"", 0, 0,
"",
"F:", 0, 0,
"[-F format]",
0,
NULL,
NULL,
Expand All @@ -42,16 +42,27 @@ const struct cmd_entry cmd_list_buffers_entry = {
int
cmd_list_buffers_exec(unused struct cmd *self, struct cmd_ctx *ctx)
{
struct args *args = self->args;
struct paste_buffer *pb;
struct format_tree *ft;
u_int idx;
char *tmp;
char *line;
const char *template;

if ((template = args_get(args, 'F')) == NULL)
template = DEFAULT_BUFFER_LIST_TEMPLATE;

idx = 0;
while ((pb = paste_walk_stack(&global_buffers, &idx)) != NULL) {
tmp = paste_print(pb, 50);
ctx->print(ctx,
"%u: %zu bytes: \"%s\"", idx - 1, pb->size, tmp);
xfree(tmp);
ft = format_create();
format_add(ft, "line", "%u", idx - 1);
format_paste_buffer(ft, pb);

line = format_expand(ft, template);
ctx->print(ctx, "%s", line);
xfree(line);

format_free(ft);
}

return (0);
Expand Down
9 changes: 2 additions & 7 deletions cmd-list-clients.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,8 @@ cmd_list_clients_exec(struct cmd *self, struct cmd_ctx *ctx)
} else
s = NULL;

template = args_get(args, 'F');
if (template == NULL) {
template = "#{client_tty}: #{session_name} "
"[#{client_width}x#{client_height} #{client_termname}]"
"#{?client_utf8, (utf8),}"
"#{?client_readonly, (ro),}";
}
if ((template = args_get(args, 'F')) == NULL)
template = DEFAULT_CLIENT_TEMPLATE;

for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
c = ARRAY_ITEM(&clients, i);
Expand Down
Loading

0 comments on commit ebf94bc

Please sign in to comment.