This teaches the list/choose buffers command to accept -F, and in doing so,
creates new buffer-specific format options.
---
 trunk/cmd-choose-buffer.c |   24 +++++++++++++++++-------
 trunk/cmd-list-buffers.c  |   25 ++++++++++++++++++-------
 trunk/format.c            |   11 +++++++++++
 trunk/tmux.h              |    1 +
 4 files changed, 47 insertions(+), 14 deletions(-)

diff --git a/trunk/cmd-choose-buffer.c b/trunk/cmd-choose-buffer.c
index 0b2edb3..ebb55bd 100644
--- a/trunk/cmd-choose-buffer.c
+++ b/trunk/cmd-choose-buffer.c
@@ -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]",
+       "t:F:", 0, 1,
+       CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
        0,
        NULL,
        NULL,
@@ -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_TEMPLATE;
+
        if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
                return (-1);
 
@@ -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);
diff --git a/trunk/cmd-list-buffers.c b/trunk/cmd-list-buffers.c
index 0a59317..acb6082 100644
--- a/trunk/cmd-list-buffers.c
+++ b/trunk/cmd-list-buffers.c
@@ -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,
@@ -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_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);
diff --git a/trunk/format.c b/trunk/format.c
index 2dea8e8..eb32be0 100644
--- a/trunk/format.c
+++ b/trunk/format.c
@@ -393,3 +393,14 @@ format_window_pane(struct format_tree *ft, struct 
window_pane *wp)
        format_add(ft, "pane_pid", "%ld", (long) wp->pid);
        format_add(ft, "pane_tty", "%s", wp->tty);
 }
+
+void
+format_paste_buffer(struct format_tree *ft, struct paste_buffer *pb)
+{
+       char    *pb_print = paste_print(pb, 50);
+
+       format_add(ft, "buffer_size", "%zu", pb->size);
+       format_add(ft, "buffer_sample", "%s", pb_print);
+
+       xfree(pb_print);
+}
diff --git a/trunk/tmux.h b/trunk/tmux.h
index 8c9207b..ae9c6c3 100644
--- a/trunk/tmux.h
+++ b/trunk/tmux.h
@@ -1410,6 +1410,7 @@ void               format_client(struct format_tree *, 
struct client *);
 void            format_winlink(
                     struct format_tree *, struct session *, struct winlink *);
 void            format_window_pane(struct format_tree *, struct window_pane *);
+void            format_paste_buffer(struct format_tree *, struct paste_buffer 
*);
 
 /* mode-key.c */
 extern const struct mode_key_table mode_key_tables[];
-- 
1.7.10


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
tmux-users mailing list
tmux-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/tmux-users

Reply via email to