Home | History | Annotate | Line # | Download | only in dist
cmd-list-panes.c revision 1.1.1.9
      1  1.1.1.5  christos /* $OpenBSD$ */
      2      1.1      jmmv 
      3      1.1      jmmv /*
      4  1.1.1.6  christos  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott (at) gmail.com>
      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.7  christos static enum cmd_retval	cmd_list_panes_exec(struct cmd *, struct cmdq_item *);
     30      1.1      jmmv 
     31  1.1.1.7  christos static void	cmd_list_panes_server(struct cmd *, struct cmdq_item *);
     32  1.1.1.7  christos static void	cmd_list_panes_session(struct cmd *, struct session *,
     33  1.1.1.7  christos 		    struct cmdq_item *, int);
     34  1.1.1.7  christos static void	cmd_list_panes_window(struct cmd *, struct session *,
     35  1.1.1.7  christos 		    struct winlink *, struct cmdq_item *, int);
     36  1.1.1.2      jmmv 
     37      1.1      jmmv const struct cmd_entry cmd_list_panes_entry = {
     38  1.1.1.6  christos 	.name = "list-panes",
     39  1.1.1.6  christos 	.alias = "lsp",
     40  1.1.1.6  christos 
     41  1.1.1.9  christos 	.args = { "asF:f:t:", 0, 0 },
     42  1.1.1.9  christos 	.usage = "[-as] [-F format] [-f filter] " CMD_TARGET_WINDOW_USAGE,
     43  1.1.1.6  christos 
     44  1.1.1.8  christos 	.target = { 't', CMD_FIND_WINDOW, 0 },
     45  1.1.1.6  christos 
     46  1.1.1.7  christos 	.flags = CMD_AFTERHOOK,
     47  1.1.1.6  christos 	.exec = cmd_list_panes_exec
     48      1.1      jmmv };
     49      1.1      jmmv 
     50  1.1.1.7  christos static enum cmd_retval
     51  1.1.1.7  christos cmd_list_panes_exec(struct cmd *self, struct cmdq_item *item)
     52      1.1      jmmv {
     53  1.1.1.9  christos 	struct args		*args = cmd_get_args(self);
     54  1.1.1.9  christos 	struct cmd_find_state	*target = cmdq_get_target(item);
     55  1.1.1.9  christos 	struct session		*s = target->s;
     56  1.1.1.9  christos 	struct winlink		*wl = target->wl;
     57  1.1.1.2      jmmv 
     58  1.1.1.2      jmmv 	if (args_has(args, 'a'))
     59  1.1.1.7  christos 		cmd_list_panes_server(self, item);
     60  1.1.1.6  christos 	else if (args_has(args, 's'))
     61  1.1.1.7  christos 		cmd_list_panes_session(self, s, item, 1);
     62  1.1.1.6  christos 	else
     63  1.1.1.7  christos 		cmd_list_panes_window(self, s, wl, item, 0);
     64  1.1.1.2      jmmv 
     65  1.1.1.3  christos 	return (CMD_RETURN_NORMAL);
     66  1.1.1.2      jmmv }
     67  1.1.1.2      jmmv 
     68  1.1.1.7  christos static void
     69  1.1.1.7  christos cmd_list_panes_server(struct cmd *self, struct cmdq_item *item)
     70  1.1.1.2      jmmv {
     71  1.1.1.2      jmmv 	struct session	*s;
     72  1.1.1.2      jmmv 
     73  1.1.1.2      jmmv 	RB_FOREACH(s, sessions, &sessions)
     74  1.1.1.7  christos 		cmd_list_panes_session(self, s, item, 2);
     75  1.1.1.2      jmmv }
     76  1.1.1.2      jmmv 
     77  1.1.1.7  christos static void
     78  1.1.1.7  christos cmd_list_panes_session(struct cmd *self, struct session *s,
     79  1.1.1.7  christos     struct cmdq_item *item, int type)
     80  1.1.1.2      jmmv {
     81  1.1.1.2      jmmv 	struct winlink	*wl;
     82  1.1.1.2      jmmv 
     83  1.1.1.2      jmmv 	RB_FOREACH(wl, winlinks, &s->windows)
     84  1.1.1.7  christos 		cmd_list_panes_window(self, s, wl, item, type);
     85  1.1.1.2      jmmv }
     86  1.1.1.2      jmmv 
     87  1.1.1.7  christos static void
     88  1.1.1.5  christos cmd_list_panes_window(struct cmd *self, struct session *s, struct winlink *wl,
     89  1.1.1.7  christos     struct cmdq_item *item, int type)
     90  1.1.1.2      jmmv {
     91  1.1.1.9  christos 	struct args		*args = cmd_get_args(self);
     92      1.1      jmmv 	struct window_pane	*wp;
     93  1.1.1.3  christos 	u_int			 n;
     94  1.1.1.3  christos 	struct format_tree	*ft;
     95  1.1.1.9  christos 	const char		*template, *filter;
     96  1.1.1.9  christos 	char			*line, *expanded;
     97  1.1.1.9  christos 	int			 flag;
     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.9  christos 	filter = args_get(args, 'f');
    126  1.1.1.3  christos 
    127  1.1.1.3  christos 	n = 0;
    128  1.1.1.3  christos 	TAILQ_FOREACH(wp, &wl->window->panes, entry) {
    129  1.1.1.9  christos 		ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
    130  1.1.1.3  christos 		format_add(ft, "line", "%u", n);
    131  1.1.1.5  christos 		format_defaults(ft, NULL, s, wl, wp);
    132  1.1.1.3  christos 
    133  1.1.1.9  christos 		if (filter != NULL) {
    134  1.1.1.9  christos 			expanded = format_expand(ft, filter);
    135  1.1.1.9  christos 			flag = format_true(expanded);
    136  1.1.1.9  christos 			free(expanded);
    137  1.1.1.9  christos 		} else
    138  1.1.1.9  christos 			flag = 1;
    139  1.1.1.9  christos 		if (flag) {
    140  1.1.1.9  christos 			line = format_expand(ft, template);
    141  1.1.1.9  christos 			cmdq_print(item, "%s", line);
    142  1.1.1.9  christos 			free(line);
    143  1.1.1.9  christos 		}
    144  1.1.1.3  christos 
    145  1.1.1.3  christos 		format_free(ft);
    146      1.1      jmmv 		n++;
    147      1.1      jmmv 	}
    148      1.1      jmmv }
    149