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