AmendHub

jcs

/

subtext

/

amendments

/

155

session: Don't run all session_printf formats through expand_template

session_expand_template is only needed for actual template views and
is pretty expensive. We only really need support for bolding and
resetting in session_printf.
This had the side effect of converting all bare \r's in session_printf
to \r\n, which screwed up ANSI output that was trying to just jump
back to the beginning of the line, like chat output.

jcs made amendment 155 7 months ago
--- ansi.c Mon Jun 13 15:52:50 2022 +++ ansi.c Fri Jun 17 17:13:28 2022 @@ -29,6 +29,24 @@ static char ansi_buf[10][32]; static short last_ansi_buf = 0; +const char * +ansi_bold(struct session *s) +{ + static const char bold[] = "\33[1m"; + if (s->vt100) + return bold; + return ""; +} + +const char * +ansi_reset(struct session *s) +{ + static const char reset[] = "\33[0m"; + if (s->vt100) + return reset; + return ""; +} + char * ansi(struct session *s, ...) { @@ -44,7 +62,7 @@ ansi(struct session *s, ...) if (++last_ansi_buf >= nitems(ansi_buf)) last_ansi_buf = 0; - *ansi_out = 0; + *ansi_out = '\0'; va_start(ap, s); while ((attr = va_arg(ap, short)) != ANSI_END) { --- ansi.h Sun May 15 22:04:03 2022 +++ ansi.h Fri Jun 17 17:13:58 2022 @@ -47,7 +47,9 @@ enum { ANSI_END = -1 }; -char *ansi(struct session *s, ...); +char * ansi(struct session *s, ...); +const char * ansi_bold(struct session *s); +const char * ansi_reset(struct session *s); size_t ansi_strip(char *inp, char **outp); void ansi_probe_screen_size(struct session *s); --- session.c Thu Jun 16 11:01:19 2022 +++ session.c Fri Jun 17 21:18:04 2022 @@ -318,10 +318,8 @@ session_close(struct session *session) void session_flush(struct session *session) { - if (session->ending) + if (session->ending || session->obuflen == 0) return; - if (session->obuflen == 0) - return; while (session->obuflen != 0) { session->node_funcs->output(session); @@ -376,18 +374,53 @@ session_output(struct session *session, const char *st size_t session_printf(struct session *session, const char *format, ...) { - static char session_printf_tbuf[192]; + static char session_printf_ebuf[160], session_printf_tbuf[160]; va_list ap; - size_t len; - char *expanded; + size_t len, n, en; + bool stop = false; - len = session_expand_template(session, format, &expanded); - + /* avoid a full session_expand_template just for {{B}} and {{/B}} */ + session_printf_ebuf[0] = '\0'; + for (n = 0, en = 0; format[n] != '\0'; n++) { + if (!stop) { + if (format[n] == '{' && format[n + 1] == '{' && + format[n + 2] == 'B' && format[n + 3] == '}' && + format[n + 4] == '}') { + en = strlcat(session_printf_ebuf, ansi_bold(session), + sizeof(session_printf_ebuf)); + n += 4; + continue; + } + if (format[n] == '{' && + format[n + 1] == '{' && format[n + 2] == '/' && + format[n + 3] == 'B' && format[n + 4] == '}' && + format[n + 5] == '}') { + en = strlcat(session_printf_ebuf, ansi_reset(session), + sizeof(session_printf_ebuf)); + n += 5; + continue; + } + if (format[n] == '{' && + format[n + 1] == '{' && format[n + 2] == '#' && + format[n + 3] == '}' && format[n + 4] == '}') { + stop = true; + n += 4; + continue; + } + } + + session_printf_ebuf[en++] = format[n]; + + if (en >= sizeof(session_printf_ebuf)) + panic("session_printf_ebuf overflow!"); + session_printf_ebuf[en] = '\0'; + } + va_start(ap, format); len = vsnprintf(session_printf_tbuf, sizeof(session_printf_tbuf), - expanded, ap); + session_printf_ebuf, ap); va_end(ap); - free(expanded); + if (len > sizeof(session_printf_tbuf)) panic("session_printf overflow! (%ld > %ld)", len, sizeof(session_printf_tbuf)); @@ -461,10 +494,8 @@ session_wait_for_chars(struct session *session, unsign while (session->ibuflen < num_chars) { session->node_funcs->input(session); - if (session->ending) + if (session->ending || session->abort_input) return false; - if (session->abort_input) - return false; if (session->obuflen != 0 && session->chatting) return false; if (expire && Ticks > expire) @@ -548,7 +579,6 @@ done_consuming: } session->last_input = ret; - return ret; idled_out: @@ -886,7 +916,7 @@ session_bar(struct session *s, char *left_str, char *r ansi(s, ANSI_RESET, ANSI_REVERSE, ANSI_END), sizeof(sides[0])); else - sidelen = strlcat(side, ansi(s, ANSI_BOLD, ANSI_END), + sidelen = strlcat(side, ansi_bold(s), sizeof(sides[0])); bold = !bold; @@ -1066,11 +1096,9 @@ session_expand_var(struct session *session, char *ivar } if (strcmp(var, "B") == 0) { - retlen = strlcpy(retval, - ansi(session, ANSI_BOLD, ANSI_END), retsize); + retlen = strlcpy(retval, ansi_bold(session), retsize); } else if (strcmp(var, "/B") == 0) { - retlen = strlcpy(retval, - ansi(session, ANSI_RESET, ANSI_END), retsize); + retlen = strlcpy(retval, ansi_reset(session), retsize); } else if (strcmp(var, "#") == 0) { *end_expansion = true; retlen = 0;