Coverage Report

Created: 2025-07-11 06:20

/src/tmux/cmd-list-panes.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD$ */
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:f:t:", 0, 0, NULL },
42
  .usage = "[-as] [-F format] [-f filter] " 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
0
{
53
0
  struct args   *args = cmd_get_args(self);
54
0
  struct cmd_find_state *target = cmdq_get_target(item);
55
0
  struct session    *s = target->s;
56
0
  struct winlink    *wl = target->wl;
57
58
0
  if (args_has(args, 'a'))
59
0
    cmd_list_panes_server(self, item);
60
0
  else if (args_has(args, 's'))
61
0
    cmd_list_panes_session(self, s, item, 1);
62
0
  else
63
0
    cmd_list_panes_window(self, s, wl, item, 0);
64
65
0
  return (CMD_RETURN_NORMAL);
66
0
}
67
68
static void
69
cmd_list_panes_server(struct cmd *self, struct cmdq_item *item)
70
0
{
71
0
  struct session  *s;
72
73
0
  RB_FOREACH(s, sessions, &sessions)
74
0
    cmd_list_panes_session(self, s, item, 2);
75
0
}
76
77
static void
78
cmd_list_panes_session(struct cmd *self, struct session *s,
79
    struct cmdq_item *item, int type)
80
0
{
81
0
  struct winlink  *wl;
82
83
0
  RB_FOREACH(wl, winlinks, &s->windows)
84
0
    cmd_list_panes_window(self, s, wl, item, type);
85
0
}
86
87
static void
88
cmd_list_panes_window(struct cmd *self, struct session *s, struct winlink *wl,
89
    struct cmdq_item *item, int type)
90
0
{
91
0
  struct args   *args = cmd_get_args(self);
92
0
  struct window_pane  *wp;
93
0
  u_int      n;
94
0
  struct format_tree  *ft;
95
0
  const char    *template, *filter;
96
0
  char      *line, *expanded;
97
0
  int      flag;
98
99
0
  template = args_get(args, 'F');
100
0
  if (template == NULL) {
101
0
    switch (type) {
102
0
    case 0:
103
0
      template = "#{pane_index}: "
104
0
          "[#{pane_width}x#{pane_height}] [history "
105
0
          "#{history_size}/#{history_limit}, "
106
0
          "#{history_bytes} bytes] #{pane_id}"
107
0
          "#{?pane_active, (active),}#{?pane_dead, (dead),}";
108
0
      break;
109
0
    case 1:
110
0
      template = "#{window_index}.#{pane_index}: "
111
0
          "[#{pane_width}x#{pane_height}] [history "
112
0
          "#{history_size}/#{history_limit}, "
113
0
          "#{history_bytes} bytes] #{pane_id}"
114
0
          "#{?pane_active, (active),}#{?pane_dead, (dead),}";
115
0
      break;
116
0
    case 2:
117
0
      template = "#{session_name}:#{window_index}."
118
0
          "#{pane_index}: [#{pane_width}x#{pane_height}] "
119
0
          "[history #{history_size}/#{history_limit}, "
120
0
          "#{history_bytes} bytes] #{pane_id}"
121
0
          "#{?pane_active, (active),}#{?pane_dead, (dead),}";
122
0
      break;
123
0
    }
124
0
  }
125
0
  filter = args_get(args, 'f');
126
127
0
  n = 0;
128
0
  TAILQ_FOREACH(wp, &wl->window->panes, entry) {
129
0
    ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
130
0
    format_add(ft, "line", "%u", n);
131
0
    format_defaults(ft, NULL, s, wl, wp);
132
133
0
    if (filter != NULL) {
134
0
      expanded = format_expand(ft, filter);
135
0
      flag = format_true(expanded);
136
0
      free(expanded);
137
0
    } else
138
0
      flag = 1;
139
0
    if (flag) {
140
0
      line = format_expand(ft, template);
141
0
      cmdq_print(item, "%s", line);
142
0
      free(line);
143
0
    }
144
145
0
    format_free(ft);
146
0
    n++;
147
0
  }
148
0
}