cmd-display-message.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
  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 <time.h>
  20. #include "tmux.h"
  21. /*
  22. * Displays a message in the status line.
  23. */
  24. #define DISPLAY_MESSAGE_TEMPLATE \
  25. "[#{session_name}] #{window_index}:" \
  26. "#{window_name}, current pane #{pane_index} " \
  27. "- (%H:%M %d-%b-%y)"
  28. enum cmd_retval cmd_display_message_exec(struct cmd *, struct cmd_q *);
  29. const struct cmd_entry cmd_display_message_entry = {
  30. .name = "display-message",
  31. .alias = "display",
  32. .args = { "c:pt:F:", 0, 1 },
  33. .usage = "[-p] [-c target-client] [-F format] "
  34. CMD_TARGET_PANE_USAGE " [message]",
  35. .cflag = CMD_CLIENT_CANFAIL,
  36. .tflag = CMD_PANE,
  37. .flags = 0,
  38. .exec = cmd_display_message_exec
  39. };
  40. enum cmd_retval
  41. cmd_display_message_exec(struct cmd *self, struct cmd_q *cmdq)
  42. {
  43. struct args *args = self->args;
  44. struct client *c = cmdq->state.c;
  45. struct session *s = cmdq->state.tflag.s;
  46. struct winlink *wl = cmdq->state.tflag.wl;
  47. struct window_pane *wp = cmdq->state.tflag.wp;
  48. const char *template;
  49. char *msg;
  50. struct format_tree *ft;
  51. if (args_has(args, 'F') && args->argc != 0) {
  52. cmdq_error(cmdq, "only one of -F or argument must be given");
  53. return (CMD_RETURN_ERROR);
  54. }
  55. template = args_get(args, 'F');
  56. if (args->argc != 0)
  57. template = args->argv[0];
  58. if (template == NULL)
  59. template = DISPLAY_MESSAGE_TEMPLATE;
  60. ft = format_create(cmdq, 0);
  61. format_defaults(ft, c, s, wl, wp);
  62. msg = format_expand_time(ft, template, time(NULL));
  63. if (args_has(self->args, 'p'))
  64. cmdq_print(cmdq, "%s", msg);
  65. else
  66. status_message_set(c, "%s", msg);
  67. free(msg);
  68. format_free(ft);
  69. return (CMD_RETURN_NORMAL);
  70. }