/src/tmux/cmd-list-panes.c
Line | Count | Source |
1 | | /* $OpenBSD: cmd-list-panes.c,v 1.41 2026/09/08 10:20:08 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 = { "aF:f:O:rst:", 0, 0, NULL }, |
42 | | .usage = "[-asr] [-F format] [-f filter] [-O order]" |
43 | | CMD_TARGET_WINDOW_USAGE, |
44 | | |
45 | | .target = { 't', CMD_FIND_WINDOW, 0 }, |
46 | | |
47 | | .flags = CMD_AFTERHOOK, |
48 | | .exec = cmd_list_panes_exec |
49 | | }; |
50 | | |
51 | | static enum cmd_retval |
52 | | cmd_list_panes_exec(struct cmd *self, struct cmdq_item *item) |
53 | 0 | { |
54 | 0 | struct args *args = cmd_get_args(self); |
55 | 0 | struct cmd_find_state *target = cmdq_get_target(item); |
56 | 0 | struct session *s = target->s; |
57 | 0 | struct winlink *wl = target->wl; |
58 | 0 | enum sort_order order; |
59 | |
|
60 | 0 | order = sort_order_from_string(args_get(args, 'O')); |
61 | 0 | if (order == SORT_END && args_has(args, 'O')) { |
62 | 0 | cmdq_error(item, "invalid sort order"); |
63 | 0 | return (CMD_RETURN_ERROR); |
64 | 0 | } |
65 | | |
66 | 0 | if (args_has(args, 'a')) |
67 | 0 | cmd_list_panes_server(self, item); |
68 | 0 | else if (args_has(args, 's')) |
69 | 0 | cmd_list_panes_session(self, s, item, 1); |
70 | 0 | else |
71 | 0 | cmd_list_panes_window(self, s, wl, item, 0); |
72 | |
|
73 | 0 | return (CMD_RETURN_NORMAL); |
74 | 0 | } |
75 | | |
76 | | static void |
77 | | cmd_list_panes_server(struct cmd *self, struct cmdq_item *item) |
78 | 0 | { |
79 | 0 | struct session *s; |
80 | |
|
81 | 0 | RB_FOREACH(s, sessions, &sessions) |
82 | 0 | cmd_list_panes_session(self, s, item, 2); |
83 | 0 | } |
84 | | |
85 | | static void |
86 | | cmd_list_panes_session(struct cmd *self, struct session *s, |
87 | | struct cmdq_item *item, int type) |
88 | 0 | { |
89 | 0 | struct winlink *wl; |
90 | |
|
91 | 0 | RB_FOREACH(wl, winlinks, &s->windows) |
92 | 0 | cmd_list_panes_window(self, s, wl, item, type); |
93 | 0 | } |
94 | | |
95 | | static void |
96 | | cmd_list_panes_window(struct cmd *self, struct session *s, struct winlink *wl, |
97 | | struct cmdq_item *item, int type) |
98 | 0 | { |
99 | 0 | struct args *args = cmd_get_args(self); |
100 | 0 | struct client *c = cmdq_get_client(item); |
101 | 0 | struct window_pane *wp, **l; |
102 | 0 | u_int i, n; |
103 | 0 | struct format_tree *ft; |
104 | 0 | const char *template, *filter; |
105 | 0 | char *line, *expanded; |
106 | 0 | int flag; |
107 | 0 | struct sort_criteria sort_crit; |
108 | |
|
109 | 0 | template = args_get(args, 'F'); |
110 | 0 | if (template == NULL) { |
111 | 0 | switch (type) { |
112 | 0 | case 0: |
113 | 0 | template = "#{pane_index}: " |
114 | 0 | "[#{pane_width}x#{pane_height}" |
115 | 0 | "#{?pane_floating_flag, " |
116 | 0 | "#{pane_x}#,#{pane_y}#,#{pane_z}}] [history " |
117 | 0 | "#{history_size}/#{history_limit}, " |
118 | 0 | "#{history_bytes} bytes] #{pane_id}" |
119 | 0 | "#{?pane_active, (active),}#{?pane_dead, (dead),}"; |
120 | 0 | break; |
121 | 0 | case 1: |
122 | 0 | template = "#{window_index}.#{pane_index}: " |
123 | 0 | "[#{pane_width}x#{pane_height}" |
124 | 0 | "#{?pane_floating_flag, " |
125 | 0 | "#{pane_x}#,#{pane_y}#,#{pane_z}}] [history " |
126 | 0 | "#{history_size}/#{history_limit}, " |
127 | 0 | "#{history_bytes} bytes] #{pane_id}" |
128 | 0 | "#{?pane_active, (active),}#{?pane_dead, (dead),}"; |
129 | 0 | break; |
130 | 0 | case 2: |
131 | 0 | template = "#{session_name}:#{window_index}." |
132 | 0 | "#{pane_index}: [#{pane_width}x#{pane_height}" |
133 | 0 | "#{?pane_floating_flag, " |
134 | 0 | "#{pane_x}#,#{pane_y}#,#{pane_z}}] [history " |
135 | 0 | "#{history_size}/#{history_limit}, " |
136 | 0 | "#{history_bytes} bytes] #{pane_id}" |
137 | 0 | "#{?pane_active, (active),}#{?pane_dead, (dead),}"; |
138 | 0 | break; |
139 | 0 | } |
140 | 0 | } |
141 | 0 | filter = args_get(args, 'f'); |
142 | |
|
143 | 0 | sort_crit.order = sort_order_from_string(args_get(args, 'O')); |
144 | 0 | sort_crit.reversed = args_has(args, 'r'); |
145 | |
|
146 | 0 | l = sort_get_panes_window(wl->window, &n, &sort_crit); |
147 | 0 | for (i = 0; i < n; i++) { |
148 | 0 | wp = l[i]; |
149 | 0 | ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0); |
150 | 0 | format_add(ft, "line", "%u", n); |
151 | 0 | format_defaults(ft, c, s, wl, wp); |
152 | |
|
153 | 0 | if (filter != NULL) { |
154 | 0 | expanded = format_expand(ft, filter); |
155 | 0 | flag = format_true(expanded); |
156 | 0 | free(expanded); |
157 | 0 | } else |
158 | 0 | flag = 1; |
159 | 0 | if (flag) { |
160 | 0 | line = format_expand(ft, template); |
161 | 0 | cmdq_print(item, "%s", line); |
162 | 0 | free(line); |
163 | 0 | } |
164 | |
|
165 | 0 | format_free(ft); |
166 | 0 | } |
167 | 0 | } |