cmd-list-panes.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <stdlib.h>
  19. #include "tmux.h"
  20. /*
  21. * List panes on given window.
  22. */
  23. enum cmd_retval cmd_list_panes_exec(struct cmd *, struct cmd_q *);
  24. void cmd_list_panes_server(struct cmd *, struct cmd_q *);
  25. void cmd_list_panes_session(struct cmd *, struct session *, struct cmd_q *,
  26. int);
  27. void cmd_list_panes_window(struct cmd *, struct session *, struct winlink *,
  28. struct cmd_q *, int);
  29. const struct cmd_entry cmd_list_panes_entry = {
  30. .name = "list-panes",
  31. .alias = "lsp",
  32. .args = { "asF:t:", 0, 0 },
  33. .usage = "[-as] [-F format] " CMD_TARGET_WINDOW_USAGE,
  34. .tflag = CMD_WINDOW,
  35. .flags = 0,
  36. .exec = cmd_list_panes_exec
  37. };
  38. enum cmd_retval
  39. cmd_list_panes_exec(struct cmd *self, struct cmd_q *cmdq)
  40. {
  41. struct args *args = self->args;
  42. struct session *s = cmdq->state.tflag.s;
  43. struct winlink *wl = cmdq->state.tflag.wl;
  44. if (args_has(args, 'a'))
  45. cmd_list_panes_server(self, cmdq);
  46. else if (args_has(args, 's'))
  47. cmd_list_panes_session(self, s, cmdq, 1);
  48. else
  49. cmd_list_panes_window(self, s, wl, cmdq, 0);
  50. return (CMD_RETURN_NORMAL);
  51. }
  52. void
  53. cmd_list_panes_server(struct cmd *self, struct cmd_q *cmdq)
  54. {
  55. struct session *s;
  56. RB_FOREACH(s, sessions, &sessions)
  57. cmd_list_panes_session(self, s, cmdq, 2);
  58. }
  59. void
  60. cmd_list_panes_session(struct cmd *self, struct session *s, struct cmd_q *cmdq,
  61. int type)
  62. {
  63. struct winlink *wl;
  64. RB_FOREACH(wl, winlinks, &s->windows)
  65. cmd_list_panes_window(self, s, wl, cmdq, type);
  66. }
  67. void
  68. cmd_list_panes_window(struct cmd *self, struct session *s, struct winlink *wl,
  69. struct cmd_q *cmdq, int type)
  70. {
  71. struct args *args = self->args;
  72. struct window_pane *wp;
  73. u_int n;
  74. struct format_tree *ft;
  75. const char *template;
  76. char *line;
  77. template = args_get(args, 'F');
  78. if (template == NULL) {
  79. switch (type) {
  80. case 0:
  81. template = "#{pane_index}: "
  82. "[#{pane_width}x#{pane_height}] [history "
  83. "#{history_size}/#{history_limit}, "
  84. "#{history_bytes} bytes] #{pane_id}"
  85. "#{?pane_active, (active),}#{?pane_dead, (dead),}";
  86. break;
  87. case 1:
  88. template = "#{window_index}.#{pane_index}: "
  89. "[#{pane_width}x#{pane_height}] [history "
  90. "#{history_size}/#{history_limit}, "
  91. "#{history_bytes} bytes] #{pane_id}"
  92. "#{?pane_active, (active),}#{?pane_dead, (dead),}";
  93. break;
  94. case 2:
  95. template = "#{session_name}:#{window_index}."
  96. "#{pane_index}: [#{pane_width}x#{pane_height}] "
  97. "[history #{history_size}/#{history_limit}, "
  98. "#{history_bytes} bytes] #{pane_id}"
  99. "#{?pane_active, (active),}#{?pane_dead, (dead),}";
  100. break;
  101. }
  102. }
  103. n = 0;
  104. TAILQ_FOREACH(wp, &wl->window->panes, entry) {
  105. ft = format_create(cmdq, 0);
  106. format_add(ft, "line", "%u", n);
  107. format_defaults(ft, NULL, s, wl, wp);
  108. line = format_expand(ft, template);
  109. cmdq_print(cmdq, "%s", line);
  110. free(line);
  111. format_free(ft);
  112. n++;
  113. }
  114. }