blob: 11a5fddfc6ba614640ed8db36a3f4383d193fb6b [file] [log] [blame] [raw]
Nicholas Marriott35876ea2009-06-01 22:58:49 +00001/* $OpenBSD$ */
2
3/*
nicm995af0e2016-01-19 15:59:12 +00004 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
Nicholas Marriott35876ea2009-06-01 22:58:49 +00005 *
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
Nicholas Marriottdf912e32012-07-10 11:53:01 +000021#include <stdlib.h>
Nicholas Marriott35876ea2009-06-01 22:58:49 +000022#include <unistd.h>
23
24#include "tmux.h"
25
26/*
27 * List windows on given session.
28 */
29
nicm4c423812014-10-20 23:35:28 +000030#define LIST_WINDOWS_TEMPLATE \
31 "#{window_index}: #{window_name}#{window_flags} " \
32 "(#{window_panes} panes) " \
33 "[#{window_width}x#{window_height}] " \
34 "[layout #{window_layout}] #{window_id}" \
35 "#{?window_active, (active),}";
36#define LIST_WINDOWS_WITH_SESSION_TEMPLATE \
37 "#{session_name}:" \
38 "#{window_index}: #{window_name}#{window_flags} " \
39 "(#{window_panes} panes) " \
40 "[#{window_width}x#{window_height}] "
41
Nicholas Marriott20636d92013-03-24 09:54:10 +000042enum cmd_retval cmd_list_windows_exec(struct cmd *, struct cmd_q *);
Nicholas Marriott35876ea2009-06-01 22:58:49 +000043
Nicholas Marriott20636d92013-03-24 09:54:10 +000044void cmd_list_windows_server(struct cmd *, struct cmd_q *);
nicm4c423812014-10-20 23:35:28 +000045void cmd_list_windows_session(struct cmd *, struct session *,
46 struct cmd_q *, int);
Nicholas Marriottf19a4bf2011-03-28 23:13:00 +000047
Nicholas Marriott35876ea2009-06-01 22:58:49 +000048const struct cmd_entry cmd_list_windows_entry = {
nicmecfeee22015-12-13 21:53:57 +000049 .name = "list-windows",
50 .alias = "lsw",
51
52 .args = { "F:at:", 0, 0 },
53 .usage = "[-a] [-F format] " CMD_TARGET_SESSION_USAGE,
54
nicma3129fd2015-12-14 00:31:54 +000055 .tflag = CMD_SESSION,
56
57 .flags = 0,
nicmecfeee22015-12-13 21:53:57 +000058 .exec = cmd_list_windows_exec
Nicholas Marriott35876ea2009-06-01 22:58:49 +000059};
60
Nicholas Marriottede83122012-07-11 07:10:15 +000061enum cmd_retval
Nicholas Marriott20636d92013-03-24 09:54:10 +000062cmd_list_windows_exec(struct cmd *self, struct cmd_q *cmdq)
Nicholas Marriott35876ea2009-06-01 22:58:49 +000063{
Nicholas Marriott7502cb32011-01-04 00:42:46 +000064 struct args *args = self->args;
Nicholas Marriottf19a4bf2011-03-28 23:13:00 +000065
66 if (args_has(args, 'a'))
Nicholas Marriott20636d92013-03-24 09:54:10 +000067 cmd_list_windows_server(self, cmdq);
nicm4a4daf12015-12-13 14:32:38 +000068 else
69 cmd_list_windows_session(self, cmdq->state.tflag.s, cmdq, 0);
Nicholas Marriottf19a4bf2011-03-28 23:13:00 +000070
Nicholas Marriottede83122012-07-11 07:10:15 +000071 return (CMD_RETURN_NORMAL);
Nicholas Marriottf19a4bf2011-03-28 23:13:00 +000072}
73
74void
Nicholas Marriott20636d92013-03-24 09:54:10 +000075cmd_list_windows_server(struct cmd *self, struct cmd_q *cmdq)
Nicholas Marriottf19a4bf2011-03-28 23:13:00 +000076{
77 struct session *s;
78
79 RB_FOREACH(s, sessions, &sessions)
Nicholas Marriott20636d92013-03-24 09:54:10 +000080 cmd_list_windows_session(self, s, cmdq, 1);
Nicholas Marriottf19a4bf2011-03-28 23:13:00 +000081}
82
83void
nicm88bc8f32015-12-11 16:37:21 +000084cmd_list_windows_session(struct cmd *self, struct session *s,
85 struct cmd_q *cmdq, int type)
Nicholas Marriottf19a4bf2011-03-28 23:13:00 +000086{
Nicholas Marriott4a5dff32011-08-26 10:53:16 +000087 struct args *args = self->args;
88 struct winlink *wl;
89 u_int n;
90 struct format_tree *ft;
91 const char *template;
92 char *line;
Nicholas Marriott35876ea2009-06-01 22:58:49 +000093
Nicholas Marriott4a5dff32011-08-26 10:53:16 +000094 template = args_get(args, 'F');
95 if (template == NULL) {
96 switch (type) {
97 case 0:
Nicholas Marriott73c67852012-08-14 08:51:53 +000098 template = LIST_WINDOWS_TEMPLATE;
Nicholas Marriott4a5dff32011-08-26 10:53:16 +000099 break;
100 case 1:
Nicholas Marriott73c67852012-08-14 08:51:53 +0000101 template = LIST_WINDOWS_WITH_SESSION_TEMPLATE;
Nicholas Marriott4a5dff32011-08-26 10:53:16 +0000102 break;
Nicholas Marriott26aa0682011-07-04 14:04:40 +0000103 }
Nicholas Marriott4a5dff32011-08-26 10:53:16 +0000104 }
105
106 n = 0;
107 RB_FOREACH(wl, winlinks, &s->windows) {
nicm01831da2015-12-11 12:27:36 +0000108 ft = format_create(cmdq, 0);
Nicholas Marriott4a5dff32011-08-26 10:53:16 +0000109 format_add(ft, "line", "%u", n);
nicm4946f742015-02-05 10:29:43 +0000110 format_defaults(ft, NULL, s, wl, NULL);
Nicholas Marriott4a5dff32011-08-26 10:53:16 +0000111
112 line = format_expand(ft, template);
Nicholas Marriott20636d92013-03-24 09:54:10 +0000113 cmdq_print(cmdq, "%s", line);
Nicholas Marriottdf912e32012-07-10 11:53:01 +0000114 free(line);
Nicholas Marriott4a5dff32011-08-26 10:53:16 +0000115
116 format_free(ft);
117 n++;
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000118 }
Nicholas Marriott35876ea2009-06-01 22:58:49 +0000119}