Home | History | Annotate | Line # | Download | only in dist
cmd-list-panes.c revision 1.1.1.6
      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.3  christos enum cmd_retval	 cmd_list_panes_exec(struct cmd *, struct cmd_q *);
     30      1.1      jmmv 
     31  1.1.1.3  christos void	cmd_list_panes_server(struct cmd *, struct cmd_q *);
     32  1.1.1.5  christos void	cmd_list_panes_session(struct cmd *, struct session *, struct cmd_q *,
     33  1.1.1.5  christos 	    int);
     34  1.1.1.5  christos void	cmd_list_panes_window(struct cmd *, struct session *, struct winlink *,
     35  1.1.1.5  christos 	    struct cmd_q *, 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.6  christos 	.args = { "asF:t:", 0, 0 },
     42  1.1.1.6  christos 	.usage = "[-as] [-F format] " CMD_TARGET_WINDOW_USAGE,
     43  1.1.1.6  christos 
     44  1.1.1.6  christos 	.tflag = CMD_WINDOW,
     45  1.1.1.6  christos 
     46  1.1.1.6  christos 	.flags = 0,
     47  1.1.1.6  christos 	.exec = cmd_list_panes_exec
     48      1.1      jmmv };
     49      1.1      jmmv 
     50  1.1.1.3  christos enum cmd_retval
     51  1.1.1.3  christos cmd_list_panes_exec(struct cmd *self, struct cmd_q *cmdq)
     52      1.1      jmmv {
     53  1.1.1.2      jmmv 	struct args	*args = self->args;
     54  1.1.1.6  christos 	struct session	*s = cmdq->state.tflag.s;
     55  1.1.1.6  christos 	struct winlink	*wl = cmdq->state.tflag.wl;
     56  1.1.1.2      jmmv 
     57  1.1.1.2      jmmv 	if (args_has(args, 'a'))
     58  1.1.1.3  christos 		cmd_list_panes_server(self, cmdq);
     59  1.1.1.6  christos 	else if (args_has(args, 's'))
     60  1.1.1.3  christos 		cmd_list_panes_session(self, s, cmdq, 1);
     61  1.1.1.6  christos 	else
     62  1.1.1.3  christos 		cmd_list_panes_window(self, s, wl, cmdq, 0);
     63  1.1.1.2      jmmv 
     64  1.1.1.3  christos 	return (CMD_RETURN_NORMAL);
     65  1.1.1.2      jmmv }
     66  1.1.1.2      jmmv 
     67  1.1.1.2      jmmv void
     68  1.1.1.3  christos cmd_list_panes_server(struct cmd *self, struct cmd_q *cmdq)
     69  1.1.1.2      jmmv {
     70  1.1.1.2      jmmv 	struct session	*s;
     71  1.1.1.2      jmmv 
     72  1.1.1.2      jmmv 	RB_FOREACH(s, sessions, &sessions)
     73  1.1.1.3  christos 		cmd_list_panes_session(self, s, cmdq, 2);
     74  1.1.1.2      jmmv }
     75  1.1.1.2      jmmv 
     76  1.1.1.2      jmmv void
     77  1.1.1.5  christos cmd_list_panes_session(struct cmd *self, struct session *s, struct cmd_q *cmdq,
     78  1.1.1.5  christos     int type)
     79  1.1.1.2      jmmv {
     80  1.1.1.2      jmmv 	struct winlink	*wl;
     81  1.1.1.2      jmmv 
     82  1.1.1.2      jmmv 	RB_FOREACH(wl, winlinks, &s->windows)
     83  1.1.1.3  christos 		cmd_list_panes_window(self, s, wl, cmdq, type);
     84  1.1.1.2      jmmv }
     85  1.1.1.2      jmmv 
     86  1.1.1.2      jmmv void
     87  1.1.1.5  christos cmd_list_panes_window(struct cmd *self, struct session *s, struct winlink *wl,
     88  1.1.1.5  christos     struct cmd_q *cmdq, int type)
     89  1.1.1.2      jmmv {
     90  1.1.1.3  christos 	struct args		*args = self->args;
     91      1.1      jmmv 	struct window_pane	*wp;
     92  1.1.1.3  christos 	u_int			 n;
     93  1.1.1.3  christos 	struct format_tree	*ft;
     94  1.1.1.3  christos 	const char		*template;
     95  1.1.1.3  christos 	char			*line;
     96      1.1      jmmv 
     97  1.1.1.3  christos 	template = args_get(args, 'F');
     98  1.1.1.3  christos 	if (template == NULL) {
     99  1.1.1.2      jmmv 		switch (type) {
    100  1.1.1.2      jmmv 		case 0:
    101  1.1.1.3  christos 			template = "#{pane_index}: "
    102  1.1.1.3  christos 			    "[#{pane_width}x#{pane_height}] [history "
    103  1.1.1.3  christos 			    "#{history_size}/#{history_limit}, "
    104  1.1.1.3  christos 			    "#{history_bytes} bytes] #{pane_id}"
    105  1.1.1.3  christos 			    "#{?pane_active, (active),}#{?pane_dead, (dead),}";
    106  1.1.1.2      jmmv 			break;
    107  1.1.1.2      jmmv 		case 1:
    108  1.1.1.3  christos 			template = "#{window_index}.#{pane_index}: "
    109  1.1.1.3  christos 			    "[#{pane_width}x#{pane_height}] [history "
    110  1.1.1.3  christos 			    "#{history_size}/#{history_limit}, "
    111  1.1.1.3  christos 			    "#{history_bytes} bytes] #{pane_id}"
    112  1.1.1.3  christos 			    "#{?pane_active, (active),}#{?pane_dead, (dead),}";
    113  1.1.1.2      jmmv 			break;
    114  1.1.1.2      jmmv 		case 2:
    115  1.1.1.5  christos 			template = "#{session_name}:#{window_index}."
    116  1.1.1.5  christos 			    "#{pane_index}: [#{pane_width}x#{pane_height}] "
    117  1.1.1.5  christos 			    "[history #{history_size}/#{history_limit}, "
    118  1.1.1.3  christos 			    "#{history_bytes} bytes] #{pane_id}"
    119  1.1.1.3  christos 			    "#{?pane_active, (active),}#{?pane_dead, (dead),}";
    120  1.1.1.2      jmmv 			break;
    121  1.1.1.2      jmmv 		}
    122  1.1.1.3  christos 	}
    123  1.1.1.3  christos 
    124  1.1.1.3  christos 	n = 0;
    125  1.1.1.3  christos 	TAILQ_FOREACH(wp, &wl->window->panes, entry) {
    126  1.1.1.6  christos 		ft = format_create(cmdq, 0);
    127  1.1.1.3  christos 		format_add(ft, "line", "%u", n);
    128  1.1.1.5  christos 		format_defaults(ft, NULL, s, wl, wp);
    129  1.1.1.3  christos 
    130  1.1.1.3  christos 		line = format_expand(ft, template);
    131  1.1.1.3  christos 		cmdq_print(cmdq, "%s", line);
    132  1.1.1.3  christos 		free(line);
    133  1.1.1.3  christos 
    134  1.1.1.3  christos 		format_free(ft);
    135      1.1      jmmv 		n++;
    136      1.1      jmmv 	}
    137      1.1      jmmv }
    138