From 0b44ad99b51606a8cab662e04cf043a8c4a3ca92 Mon Sep 17 00:00:00 2001 From: nicm Date: Thu, 20 Apr 2017 09:20:22 +0000 Subject: If a #() command doesn't exit, use its most recent line of output (it must be a full line). Don't let it redraw the status line more than once a second. Requested by someone about 10 years ago... --- format.c | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) (limited to 'format.c') diff --git a/format.c b/format.c index 269d506a..5c85f1ff 100644 --- a/format.c +++ b/format.c @@ -39,7 +39,6 @@ struct format_entry; typedef void (*format_cb)(struct format_tree *, struct format_entry *); -static void format_job_callback(struct job *); static char *format_job_get(struct format_tree *, const char *); static void format_job_timer(int, short, void *); @@ -83,6 +82,7 @@ struct format_job { time_t last; char *out; + int updated; struct job *job; int status; @@ -203,9 +203,35 @@ static const char *format_lower[] = { NULL /* z */ }; -/* Format job callback. */ +/* Format job update callback. */ static void -format_job_callback(struct job *job) +format_job_update(struct job *job) +{ + struct format_job *fj = job->data; + char *line; + time_t t; + struct client *c; + + if ((line = evbuffer_readline(job->event->input)) == NULL) + return; + fj->updated = 1; + + free(fj->out); + fj->out = line; + + log_debug("%s: %s: %s", __func__, fj->cmd, fj->out); + + t = time (NULL); + if (fj->status && fj->last != t) { + TAILQ_FOREACH(c, &clients, entry) + server_status_client(c); + fj->last = t; + } +} + +/* Format job complete callback. */ +static void +format_job_complete(struct job *job) { struct format_job *fj = job->data; char *line, *buf; @@ -213,7 +239,6 @@ format_job_callback(struct job *job) struct client *c; fj->job = NULL; - free(fj->out); buf = NULL; if ((line = evbuffer_readline(job->event->input)) == NULL) { @@ -224,15 +249,19 @@ format_job_callback(struct job *job) buf[len] = '\0'; } else buf = line; - fj->out = buf; + + if (*buf != '\0' || !fj->updated) { + free(fj->out); + fj->out = buf; + log_debug("%s: %s: %s", __func__, fj->cmd, fj->out); + } else + free(buf); if (fj->status) { TAILQ_FOREACH(c, &clients, entry) server_status_client(c); fj->status = 0; } - - log_debug("%s: %s: %s", __func__, fj->cmd, fj->out); } /* Find a job. */ @@ -267,8 +296,8 @@ format_job_get(struct format_tree *ft, const char *cmd) t = time(NULL); if (fj->job == NULL && (force || fj->last != t)) { - fj->job = job_run(expanded, NULL, NULL, format_job_callback, - NULL, fj); + fj->job = job_run(expanded, NULL, NULL, format_job_update, + format_job_complete, NULL, fj); if (fj->job == NULL) { free(fj->out); xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd); -- cgit From 0f25ad3ca32d329500ac830f3c13e60ead4cb3db Mon Sep 17 00:00:00 2001 From: nicm Date: Thu, 20 Apr 2017 09:39:07 +0000 Subject: There is no real need for window_printable_flags to allocate, make it return a buffer from the stack. --- format.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'format.c') diff --git a/format.c b/format.c index 5c85f1ff..151c3c48 100644 --- a/format.c +++ b/format.c @@ -1237,17 +1237,14 @@ format_defaults_winlink(struct format_tree *ft, struct session *s, struct winlink *wl) { struct window *w = wl->window; - char *flags; if (ft->w == NULL) ft->w = wl->window; - flags = window_printable_flags(s, wl); - format_defaults_window(ft, w); format_add(ft, "window_index", "%d", wl->idx); - format_add(ft, "window_flags", "%s", flags); + format_add(ft, "window_flags", "%s", window_printable_flags(s, wl)); format_add(ft, "window_active", "%d", wl == s->curw); format_add(ft, "window_bell_flag", "%d", @@ -1259,8 +1256,6 @@ format_defaults_winlink(struct format_tree *ft, struct session *s, format_add(ft, "window_last_flag", "%d", !!(wl == TAILQ_FIRST(&s->lastw))); format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window)); - - free(flags); } /* Set default format keys for a window pane. */ -- cgit From 21993105e53da0f5aae583b494c83f1cbbf48b1b Mon Sep 17 00:00:00 2001 From: nicm Date: Thu, 20 Apr 2017 09:43:45 +0000 Subject: Now that struct winlink has a session pointer, can remove some arguments. --- format.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'format.c') diff --git a/format.c b/format.c index 151c3c48..b173df4c 100644 --- a/format.c +++ b/format.c @@ -71,7 +71,7 @@ static int format_replace(struct format_tree *, const char *, size_t, static void format_defaults_session(struct format_tree *, struct session *); static void format_defaults_client(struct format_tree *, struct client *); -static void format_defaults_winlink(struct format_tree *, struct session *, +static void format_defaults_winlink(struct format_tree *, struct winlink *); /* Entry in format job tree. */ @@ -1121,8 +1121,8 @@ format_defaults(struct format_tree *ft, struct client *c, struct session *s, format_defaults_client(ft, c); if (s != NULL) format_defaults_session(ft, s); - if (s != NULL && wl != NULL) - format_defaults_winlink(ft, s, wl); + if (wl != NULL) + format_defaults_winlink(ft, wl); if (wp != NULL) format_defaults_pane(ft, wp); } @@ -1233,9 +1233,9 @@ format_defaults_window(struct format_tree *ft, struct window *w) /* Set default format keys for a winlink. */ static void -format_defaults_winlink(struct format_tree *ft, struct session *s, - struct winlink *wl) +format_defaults_winlink(struct format_tree *ft, struct winlink *wl) { + struct session *s = wl->session; struct window *w = wl->window; if (ft->w == NULL) @@ -1244,7 +1244,7 @@ format_defaults_winlink(struct format_tree *ft, struct session *s, format_defaults_window(ft, w); format_add(ft, "window_index", "%d", wl->idx); - format_add(ft, "window_flags", "%s", window_printable_flags(s, wl)); + format_add(ft, "window_flags", "%s", window_printable_flags(wl)); format_add(ft, "window_active", "%d", wl == s->curw); format_add(ft, "window_bell_flag", "%d", -- cgit