cmd-list-panes.c revision 1.1.1.5 1 1.1.1.5 christos /* $OpenBSD$ */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.1 jmmv * Copyright (c) 2009 Nicholas Marriott <nicm (at) users.sourceforge.net>
5 1.1 jmmv *
6 1.1 jmmv * Permission to use, copy, modify, and distribute this software for any
7 1.1 jmmv * purpose with or without fee is hereby granted, provided that the above
8 1.1 jmmv * copyright notice and this permission notice appear in all copies.
9 1.1 jmmv *
10 1.1 jmmv * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 jmmv * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 jmmv * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 jmmv * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 jmmv * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 1.1 jmmv * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 1.1 jmmv * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 jmmv */
18 1.1 jmmv
19 1.1 jmmv #include <sys/types.h>
20 1.1 jmmv
21 1.1.1.3 christos #include <stdlib.h>
22 1.1 jmmv
23 1.1 jmmv #include "tmux.h"
24 1.1 jmmv
25 1.1 jmmv /*
26 1.1 jmmv * List panes on given window.
27 1.1 jmmv */
28 1.1 jmmv
29 1.1.1.3 christos enum cmd_retval cmd_list_panes_exec(struct cmd *, struct cmd_q *);
30 1.1 jmmv
31 1.1.1.3 christos void cmd_list_panes_server(struct cmd *, struct cmd_q *);
32 1.1.1.5 christos void cmd_list_panes_session(struct cmd *, struct session *, struct cmd_q *,
33 1.1.1.5 christos int);
34 1.1.1.5 christos void cmd_list_panes_window(struct cmd *, struct session *, struct winlink *,
35 1.1.1.5 christos struct cmd_q *, int);
36 1.1.1.2 jmmv
37 1.1 jmmv const struct cmd_entry cmd_list_panes_entry = {
38 1.1 jmmv "list-panes", "lsp",
39 1.1.1.3 christos "asF:t:", 0, 0,
40 1.1.1.3 christos "[-as] [-F format] " CMD_TARGET_WINDOW_USAGE,
41 1.1.1.2 jmmv 0,
42 1.1.1.2 jmmv cmd_list_panes_exec
43 1.1 jmmv };
44 1.1 jmmv
45 1.1.1.3 christos enum cmd_retval
46 1.1.1.3 christos cmd_list_panes_exec(struct cmd *self, struct cmd_q *cmdq)
47 1.1 jmmv {
48 1.1.1.2 jmmv struct args *args = self->args;
49 1.1.1.2 jmmv struct session *s;
50 1.1.1.2 jmmv struct winlink *wl;
51 1.1.1.2 jmmv
52 1.1.1.2 jmmv if (args_has(args, 'a'))
53 1.1.1.3 christos cmd_list_panes_server(self, cmdq);
54 1.1.1.2 jmmv else if (args_has(args, 's')) {
55 1.1.1.3 christos s = cmd_find_session(cmdq, args_get(args, 't'), 0);
56 1.1.1.2 jmmv if (s == NULL)
57 1.1.1.3 christos return (CMD_RETURN_ERROR);
58 1.1.1.3 christos cmd_list_panes_session(self, s, cmdq, 1);
59 1.1.1.2 jmmv } else {
60 1.1.1.3 christos wl = cmd_find_window(cmdq, args_get(args, 't'), &s);
61 1.1.1.2 jmmv if (wl == NULL)
62 1.1.1.3 christos return (CMD_RETURN_ERROR);
63 1.1.1.3 christos cmd_list_panes_window(self, s, wl, cmdq, 0);
64 1.1.1.2 jmmv }
65 1.1.1.2 jmmv
66 1.1.1.3 christos return (CMD_RETURN_NORMAL);
67 1.1.1.2 jmmv }
68 1.1.1.2 jmmv
69 1.1.1.2 jmmv void
70 1.1.1.3 christos cmd_list_panes_server(struct cmd *self, struct cmd_q *cmdq)
71 1.1.1.2 jmmv {
72 1.1.1.2 jmmv struct session *s;
73 1.1.1.2 jmmv
74 1.1.1.2 jmmv RB_FOREACH(s, sessions, &sessions)
75 1.1.1.3 christos cmd_list_panes_session(self, s, cmdq, 2);
76 1.1.1.2 jmmv }
77 1.1.1.2 jmmv
78 1.1.1.2 jmmv void
79 1.1.1.5 christos cmd_list_panes_session(struct cmd *self, struct session *s, struct cmd_q *cmdq,
80 1.1.1.5 christos int type)
81 1.1.1.2 jmmv {
82 1.1.1.2 jmmv struct winlink *wl;
83 1.1.1.2 jmmv
84 1.1.1.2 jmmv RB_FOREACH(wl, winlinks, &s->windows)
85 1.1.1.3 christos cmd_list_panes_window(self, s, wl, cmdq, type);
86 1.1.1.2 jmmv }
87 1.1.1.2 jmmv
88 1.1.1.2 jmmv void
89 1.1.1.5 christos cmd_list_panes_window(struct cmd *self, struct session *s, struct winlink *wl,
90 1.1.1.5 christos struct cmd_q *cmdq, int type)
91 1.1.1.2 jmmv {
92 1.1.1.3 christos struct args *args = self->args;
93 1.1 jmmv struct window_pane *wp;
94 1.1.1.3 christos u_int n;
95 1.1.1.3 christos struct format_tree *ft;
96 1.1.1.3 christos const char *template;
97 1.1.1.3 christos char *line;
98 1.1 jmmv
99 1.1.1.3 christos template = args_get(args, 'F');
100 1.1.1.3 christos if (template == NULL) {
101 1.1.1.2 jmmv switch (type) {
102 1.1.1.2 jmmv case 0:
103 1.1.1.3 christos template = "#{pane_index}: "
104 1.1.1.3 christos "[#{pane_width}x#{pane_height}] [history "
105 1.1.1.3 christos "#{history_size}/#{history_limit}, "
106 1.1.1.3 christos "#{history_bytes} bytes] #{pane_id}"
107 1.1.1.3 christos "#{?pane_active, (active),}#{?pane_dead, (dead),}";
108 1.1.1.2 jmmv break;
109 1.1.1.2 jmmv case 1:
110 1.1.1.3 christos template = "#{window_index}.#{pane_index}: "
111 1.1.1.3 christos "[#{pane_width}x#{pane_height}] [history "
112 1.1.1.3 christos "#{history_size}/#{history_limit}, "
113 1.1.1.3 christos "#{history_bytes} bytes] #{pane_id}"
114 1.1.1.3 christos "#{?pane_active, (active),}#{?pane_dead, (dead),}";
115 1.1.1.2 jmmv break;
116 1.1.1.2 jmmv case 2:
117 1.1.1.5 christos template = "#{session_name}:#{window_index}."
118 1.1.1.5 christos "#{pane_index}: [#{pane_width}x#{pane_height}] "
119 1.1.1.5 christos "[history #{history_size}/#{history_limit}, "
120 1.1.1.3 christos "#{history_bytes} bytes] #{pane_id}"
121 1.1.1.3 christos "#{?pane_active, (active),}#{?pane_dead, (dead),}";
122 1.1.1.2 jmmv break;
123 1.1.1.2 jmmv }
124 1.1.1.3 christos }
125 1.1.1.3 christos
126 1.1.1.3 christos n = 0;
127 1.1.1.3 christos TAILQ_FOREACH(wp, &wl->window->panes, entry) {
128 1.1.1.3 christos ft = format_create();
129 1.1.1.3 christos format_add(ft, "line", "%u", n);
130 1.1.1.5 christos format_defaults(ft, NULL, s, wl, wp);
131 1.1.1.3 christos
132 1.1.1.3 christos line = format_expand(ft, template);
133 1.1.1.3 christos cmdq_print(cmdq, "%s", line);
134 1.1.1.3 christos free(line);
135 1.1.1.3 christos
136 1.1.1.3 christos format_free(ft);
137 1.1 jmmv n++;
138 1.1 jmmv }
139 1.1 jmmv }
140