cmd-list-sessions.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 <string.h>
  20. #include <time.h>
  21. #include "tmux.h"
  22. /*
  23. * List all sessions.
  24. */
  25. #define LIST_SESSIONS_TEMPLATE \
  26. "#{session_name}: #{session_windows} windows " \
  27. "(created #{t:session_created}) " \
  28. "[#{session_width}x#{session_height}]" \
  29. "#{?session_grouped, (group ,}" \
  30. "#{session_group}#{?session_grouped,),}" \
  31. "#{?session_attached, (attached),}"
  32. enum cmd_retval cmd_list_sessions_exec(struct cmd *, struct cmd_q *);
  33. const struct cmd_entry cmd_list_sessions_entry = {
  34. .name = "list-sessions",
  35. .alias = "ls",
  36. .args = { "F:", 0, 0 },
  37. .usage = "[-F format]",
  38. .flags = 0,
  39. .exec = cmd_list_sessions_exec
  40. };
  41. enum cmd_retval
  42. cmd_list_sessions_exec(struct cmd *self, struct cmd_q *cmdq)
  43. {
  44. struct args *args = self->args;
  45. struct session *s;
  46. u_int n;
  47. struct format_tree *ft;
  48. const char *template;
  49. char *line;
  50. if ((template = args_get(args, 'F')) == NULL)
  51. template = LIST_SESSIONS_TEMPLATE;
  52. n = 0;
  53. RB_FOREACH(s, sessions, &sessions) {
  54. ft = format_create(cmdq, 0);
  55. format_add(ft, "line", "%u", n);
  56. format_defaults(ft, NULL, s, NULL, NULL);
  57. line = format_expand(ft, template);
  58. cmdq_print(cmdq, "%s", line);
  59. free(line);
  60. format_free(ft);
  61. n++;
  62. }
  63. return (CMD_RETURN_NORMAL);
  64. }