2009-07-17 18:45:08 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
#include <stdlib.h>
|
2009-07-17 18:45:08 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Displays a message in the status line.
|
|
|
|
*/
|
|
|
|
|
2014-10-20 23:35:28 +00:00
|
|
|
#define DISPLAY_MESSAGE_TEMPLATE \
|
|
|
|
"[#{session_name}] #{window_index}:" \
|
|
|
|
"#{window_name}, current pane #{pane_index} " \
|
|
|
|
"- (%H:%M %d-%b-%y)"
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_display_message_exec(struct cmd *,
|
|
|
|
struct cmdq_item *);
|
2009-07-17 18:45:08 +00:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_display_message_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "display-message",
|
|
|
|
.alias = "display",
|
|
|
|
|
2020-07-27 08:03:10 +00:00
|
|
|
.args = { "acd:Ipt:F:v", 0, 1 },
|
|
|
|
.usage = "[-aIpv] [-c target-client] [-d delay] [-F format] "
|
2015-12-13 21:53:57 +00:00
|
|
|
CMD_TARGET_PANE_USAGE " [message]",
|
|
|
|
|
2017-04-22 10:22:39 +00:00
|
|
|
.target = { 't', CMD_FIND_PANE, 0 },
|
2015-12-14 00:31:54 +00:00
|
|
|
|
2020-04-13 20:51:57 +00:00
|
|
|
.flags = CMD_AFTERHOOK|CMD_CLIENT_CFLAG|CMD_CLIENT_CANFAIL,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_display_message_exec
|
2009-07-17 18:45:08 +00:00
|
|
|
};
|
|
|
|
|
2019-03-18 14:10:25 +00:00
|
|
|
static void
|
|
|
|
cmd_display_message_each(const char *key, const char *value, void *arg)
|
|
|
|
{
|
|
|
|
struct cmdq_item *item = arg;
|
|
|
|
|
|
|
|
cmdq_print(item, "%s=%s", key, value);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_display_message_exec(struct cmd *self, struct cmdq_item *item)
|
2009-07-17 18:45:08 +00:00
|
|
|
{
|
2020-04-13 08:26:27 +00:00
|
|
|
struct args *args = cmd_get_args(self);
|
2020-04-13 10:59:58 +00:00
|
|
|
struct cmd_find_state *target = cmdq_get_target(item);
|
2020-04-13 20:51:57 +00:00
|
|
|
struct client *tc = cmdq_get_target_client(item), *c;
|
2020-04-13 10:59:58 +00:00
|
|
|
struct session *s = target->s;
|
|
|
|
struct winlink *wl = target->wl;
|
|
|
|
struct window_pane *wp = target->wp;
|
2009-07-17 18:45:08 +00:00
|
|
|
const char *template;
|
2019-05-03 20:44:24 +00:00
|
|
|
char *msg, *cause;
|
2020-07-27 08:03:10 +00:00
|
|
|
int delay = -1;
|
2012-02-23 22:40:58 +00:00
|
|
|
struct format_tree *ft;
|
2019-03-15 10:04:13 +00:00
|
|
|
int flags;
|
2009-07-17 18:45:08 +00:00
|
|
|
|
2019-05-03 20:44:24 +00:00
|
|
|
if (args_has(args, 'I')) {
|
|
|
|
if (window_pane_start_input(wp, item, &cause) != 0) {
|
|
|
|
cmdq_error(item, "%s", cause);
|
|
|
|
free(cause);
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
return (CMD_RETURN_WAIT);
|
|
|
|
}
|
|
|
|
|
2012-02-23 22:40:58 +00:00
|
|
|
if (args_has(args, 'F') && args->argc != 0) {
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "only one of -F or argument must be given");
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
2012-02-23 22:40:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 08:03:10 +00:00
|
|
|
if (args_has(args, 'd')) {
|
|
|
|
delay = args_strtonum(args, 'd', 0, UINT_MAX, &cause);
|
|
|
|
if (cause != NULL) {
|
|
|
|
cmdq_error(item, "delay %s", cause);
|
|
|
|
free(cause);
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-23 22:40:58 +00:00
|
|
|
template = args_get(args, 'F');
|
|
|
|
if (args->argc != 0)
|
2011-01-04 00:42:46 +00:00
|
|
|
template = args->argv[0];
|
2012-02-23 22:40:58 +00:00
|
|
|
if (template == NULL)
|
2012-08-14 08:51:53 +00:00
|
|
|
template = DISPLAY_MESSAGE_TEMPLATE;
|
2012-02-23 22:40:58 +00:00
|
|
|
|
2018-04-18 14:35:37 +00:00
|
|
|
/*
|
|
|
|
* -c is intended to be the client where the message should be
|
|
|
|
* displayed if -p is not given. But it makes sense to use it for the
|
|
|
|
* formats too, assuming it matches the session. If it doesn't, use the
|
|
|
|
* best client for the session.
|
|
|
|
*/
|
2020-04-13 20:51:57 +00:00
|
|
|
if (tc != NULL && tc->session == s)
|
|
|
|
c = tc;
|
2018-04-18 14:35:37 +00:00
|
|
|
else
|
2020-04-13 20:51:57 +00:00
|
|
|
c = cmd_find_best_client(s);
|
2020-04-13 08:26:27 +00:00
|
|
|
if (args_has(args, 'v'))
|
2019-03-15 10:04:13 +00:00
|
|
|
flags = FORMAT_VERBOSE;
|
|
|
|
else
|
|
|
|
flags = 0;
|
2020-04-13 10:59:58 +00:00
|
|
|
ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, flags);
|
2020-04-13 20:51:57 +00:00
|
|
|
format_defaults(ft, c, s, wl, wp);
|
2012-02-23 22:40:58 +00:00
|
|
|
|
2019-03-18 14:10:25 +00:00
|
|
|
if (args_has(args, 'a')) {
|
2019-05-30 20:54:03 +00:00
|
|
|
format_each(ft, cmd_display_message_each, item);
|
2019-03-18 14:10:25 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
|
|
|
|
2019-03-14 23:14:27 +00:00
|
|
|
msg = format_expand_time(ft, template);
|
2020-04-13 08:26:27 +00:00
|
|
|
if (args_has(args, 'p'))
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_print(item, "%s", msg);
|
2020-04-13 20:51:57 +00:00
|
|
|
else if (tc != NULL)
|
2020-07-27 08:03:10 +00:00
|
|
|
status_message_set(tc, delay, 0, "%s", msg);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(msg);
|
2016-08-25 09:33:14 +00:00
|
|
|
|
2012-03-03 09:45:41 +00:00
|
|
|
format_free(ft);
|
2013-03-24 09:54:10 +00:00
|
|
|
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2009-07-17 18:45:08 +00:00
|
|
|
}
|