cmd-list-windows.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2007 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 <unistd.h>
  20. #include "tmux.h"
  21. /*
  22. * List windows on given session.
  23. */
  24. #define LIST_WINDOWS_TEMPLATE \
  25. "#{window_index}: #{window_name}#{window_flags} " \
  26. "(#{window_panes} panes) " \
  27. "[#{window_width}x#{window_height}] " \
  28. "[layout #{window_layout}] #{window_id}" \
  29. "#{?window_active, (active),}";
  30. #define LIST_WINDOWS_WITH_SESSION_TEMPLATE \
  31. "#{session_name}:" \
  32. "#{window_index}: #{window_name}#{window_flags} " \
  33. "(#{window_panes} panes) " \
  34. "[#{window_width}x#{window_height}] "
  35. enum cmd_retval cmd_list_windows_exec(struct cmd *, struct cmd_q *);
  36. void cmd_list_windows_server(struct cmd *, struct cmd_q *);
  37. void cmd_list_windows_session(struct cmd *, struct session *,
  38. struct cmd_q *, int);
  39. const struct cmd_entry cmd_list_windows_entry = {
  40. .name = "list-windows",
  41. .alias = "lsw",
  42. .args = { "F:at:", 0, 0 },
  43. .usage = "[-a] [-F format] " CMD_TARGET_SESSION_USAGE,
  44. .tflag = CMD_SESSION,
  45. .flags = 0,
  46. .exec = cmd_list_windows_exec
  47. };
  48. enum cmd_retval
  49. cmd_list_windows_exec(struct cmd *self, struct cmd_q *cmdq)
  50. {
  51. struct args *args = self->args;
  52. if (args_has(args, 'a'))
  53. cmd_list_windows_server(self, cmdq);
  54. else
  55. cmd_list_windows_session(self, cmdq->state.tflag.s, cmdq, 0);
  56. return (CMD_RETURN_NORMAL);
  57. }
  58. void
  59. cmd_list_windows_server(struct cmd *self, struct cmd_q *cmdq)
  60. {
  61. struct session *s;
  62. RB_FOREACH(s, sessions, &sessions)
  63. cmd_list_windows_session(self, s, cmdq, 1);
  64. }
  65. void
  66. cmd_list_windows_session(struct cmd *self, struct session *s,
  67. struct cmd_q *cmdq, int type)
  68. {
  69. struct args *args = self->args;
  70. struct winlink *wl;
  71. u_int n;
  72. struct format_tree *ft;
  73. const char *template;
  74. char *line;
  75. template = args_get(args, 'F');
  76. if (template == NULL) {
  77. switch (type) {
  78. case 0:
  79. template = LIST_WINDOWS_TEMPLATE;
  80. break;
  81. case 1:
  82. template = LIST_WINDOWS_WITH_SESSION_TEMPLATE;
  83. break;
  84. }
  85. }
  86. n = 0;
  87. RB_FOREACH(wl, winlinks, &s->windows) {
  88. ft = format_create(cmdq, 0);
  89. format_add(ft, "line", "%u", n);
  90. format_defaults(ft, NULL, s, wl, NULL);
  91. line = format_expand(ft, template);
  92. cmdq_print(cmdq, "%s", line);
  93. free(line);
  94. format_free(ft);
  95. n++;
  96. }
  97. }