xref: /openbsd-src/usr.bin/tmux/cmd-list-panes.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /* $OpenBSD: cmd-list-panes.c,v 1.33 2017/05/01 12:20:55 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@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 static enum cmd_retval	cmd_list_panes_exec(struct cmd *, struct cmdq_item *);
30 
31 static void	cmd_list_panes_server(struct cmd *, struct cmdq_item *);
32 static void	cmd_list_panes_session(struct cmd *, struct session *,
33 		    struct cmdq_item *, int);
34 static void	cmd_list_panes_window(struct cmd *, struct session *,
35 		    struct winlink *, struct cmdq_item *, 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 	.target = { 't', CMD_FIND_WINDOW, 0 },
45 
46 	.flags = CMD_AFTERHOOK,
47 	.exec = cmd_list_panes_exec
48 };
49 
50 static enum cmd_retval
51 cmd_list_panes_exec(struct cmd *self, struct cmdq_item *item)
52 {
53 	struct args	*args = self->args;
54 	struct session	*s = item->target.s;
55 	struct winlink	*wl = item->target.wl;
56 
57 	if (args_has(args, 'a'))
58 		cmd_list_panes_server(self, item);
59 	else if (args_has(args, 's'))
60 		cmd_list_panes_session(self, s, item, 1);
61 	else
62 		cmd_list_panes_window(self, s, wl, item, 0);
63 
64 	return (CMD_RETURN_NORMAL);
65 }
66 
67 static void
68 cmd_list_panes_server(struct cmd *self, struct cmdq_item *item)
69 {
70 	struct session	*s;
71 
72 	RB_FOREACH(s, sessions, &sessions)
73 		cmd_list_panes_session(self, s, item, 2);
74 }
75 
76 static void
77 cmd_list_panes_session(struct cmd *self, struct session *s,
78     struct cmdq_item *item, int type)
79 {
80 	struct winlink	*wl;
81 
82 	RB_FOREACH(wl, winlinks, &s->windows)
83 		cmd_list_panes_window(self, s, wl, item, type);
84 }
85 
86 static void
87 cmd_list_panes_window(struct cmd *self, struct session *s, struct winlink *wl,
88     struct cmdq_item *item, 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(item->client, item, FORMAT_NONE, 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(item, "%s", line);
132 		free(line);
133 
134 		format_free(ft);
135 		n++;
136 	}
137 }
138