Add a -f filter argument to the list commands like choose-tree.

This commit is contained in:
nicm
2020-04-12 08:36:18 +00:00
parent 70534cfde6
commit 756591b4ca
5 changed files with 95 additions and 37 deletions

View File

@ -42,8 +42,8 @@ const struct cmd_entry cmd_list_sessions_entry = {
.name = "list-sessions",
.alias = "ls",
.args = { "F:", 0, 0 },
.usage = "[-F format]",
.args = { "F:f:", 0, 0 },
.usage = "[-F format] [-f filter]",
.flags = CMD_AFTERHOOK,
.exec = cmd_list_sessions_exec
@ -56,11 +56,13 @@ cmd_list_sessions_exec(struct cmd *self, struct cmdq_item *item)
struct session *s;
u_int n;
struct format_tree *ft;
const char *template;
char *line;
const char *template, *filter;
char *line, *expanded;
int flag;
if ((template = args_get(args, 'F')) == NULL)
template = LIST_SESSIONS_TEMPLATE;
filter = args_get(args, 'f');
n = 0;
RB_FOREACH(s, sessions, &sessions) {
@ -68,9 +70,17 @@ cmd_list_sessions_exec(struct cmd *self, struct cmdq_item *item)
format_add(ft, "line", "%u", n);
format_defaults(ft, NULL, s, NULL, NULL);
line = format_expand(ft, template);
cmdq_print(item, "%s", line);
free(line);
if (filter != NULL) {
expanded = format_expand(ft, filter);
flag = format_true(expanded);
free(expanded);
} else
flag = 1;
if (flag) {
line = format_expand(ft, template);
cmdq_print(item, "%s", line);
free(line);
}
format_free(ft);
n++;