File: tmate-msg.c

package info (click to toggle)
tmate 2.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,560 kB
  • sloc: ansic: 38,233; awk: 339; makefile: 252; sh: 44; perl: 41
file content (96 lines) | stat: -rw-r--r-- 2,137 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <time.h>
#include "tmate.h"

void status_message_callback(int, short, void *);

/* Very similar to status.c:status_message_set */

static void tmate_status_message_client(struct client *c, const char *message)
{
	struct timeval		 tv;
	struct message_entry	*msg, *msg1;
	int			 delay;
	u_int			 limit;

	limit = options_get_number(global_options, "message-limit");
	delay = options_get_number(c->session ? c->session->options : global_s_options,
				   "tmate-display-time");

	status_prompt_clear(c);
	status_message_clear(c);

	xasprintf(&c->message_string, "[tmate] %s", message);

	msg = xcalloc(1, sizeof *msg);
	msg->msg_time = time(NULL);
	msg->msg_num = c->message_next++;
	msg->msg = xstrdup(c->message_string);
	TAILQ_INSERT_TAIL(&c->message_log, msg, entry);

	TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
		if (msg->msg_num + limit >= c->message_next)
			break;
		free(msg->msg);
		TAILQ_REMOVE(&c->message_log, msg, entry);
		free(msg);
	}

	if (delay > 0) {
		tv.tv_sec = delay / 1000;
		tv.tv_usec = (delay % 1000) * 1000L;

		if (event_initialized(&c->message_timer))
			evtimer_del(&c->message_timer);
		evtimer_set(&c->message_timer, status_message_callback, c);
		evtimer_add(&c->message_timer, &tv);
	}

	c->flags |= CLIENT_STATUS | CLIENT_FORCE_STATUS;

	recalculate_sizes();
}

static void tmate_status_message_session(const char *message)
{
	if (tmate_foreground)
		return;

	struct session *s;
	s = RB_MIN(sessions, &sessions);
	if (!s) {
		cfg_add_cause("%s", message);
		return;
	}

	struct window_pane *wp;
	wp = s->curw->window->active;
	if (wp->mode == &window_copy_mode)
		window_copy_add(wp, "%s", message);
}

void __tmate_status_message(const char *fmt, va_list ap)
{
	struct client *c;
	char *message;

	xvasprintf(&message, fmt, ap);
	tmate_info("%s", message);

	TAILQ_FOREACH(c, &clients, entry) {
		if (c && !(c->flags & CLIENT_READONLY))
			tmate_status_message_client(c, message);
	}

	tmate_status_message_session(message);

	free(message);
}

void tmate_status_message(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	__tmate_status_message(fmt, ap);
	va_end(ap);
}