AmendHub

Download:

jcs

/

subtext

/

amendments

/

570

session: Add support for {{center_XX}} template variable

This is useful for centering menu lines on wide terminals without
having to wrap on narrow terminals.

jcs made amendment 570 5 months ago
--- GUIDE Tue Nov 28 13:02:46 2023 +++ GUIDE Tue Nov 28 13:11:57 2023 @@ -89,6 +89,12 @@ characters, such as "You are connected to {{node}}." - {{#}} Stops varible expansion for the rest of the template. Useful when printing untrusted data (mostly used internally). + - {{center_XX}} + Print spaces of length equal to the user's terminal width minus XX, divided + by two. For example, if the user's terminal is 80 characters wide and the + variable {{center_50}} is used before a 50-character-wide menu line, 15 + space characters will be printed. This is useful for centering a menu on + wide terminals but preventing empty space on narrower terminals. - {{logged_in_time}} The amount of time the user has been logged in (still usable in signoff template), such as "7m20s". --- session.c Mon Nov 27 21:19:34 2023 +++ session.c Tue Nov 28 12:39:34 2023 @@ -1438,7 +1438,7 @@ session_expand_var(struct session *session, char *ivar size_t retsize, retlen, varlen, unread_count; long elapsed, telapsed; struct tm *now; - short count; + short count, n; bool pad = false; *ret = (char *)&retval; @@ -1471,6 +1471,16 @@ session_expand_var(struct session *session, char *ivar } else if (strcmp(var, "#") == 0) { *end_expansion = true; retlen = 0; + } else if (strncmp(var, "center_", 7) == 0 && + sscanf(var, "center_%d", &count) == 1) { + if (session->terminal_columns <= count) + retlen = 0; + else { + retlen = MIN((session->terminal_columns - count) / 2, retsize); + for (n = 0; n < retlen; n++) + retval[n] = ' '; + retval[retlen] = '\0'; + } } else if (strcmp(var, "logged_in_time") == 0) { memset(retval, 0, retsize); retlen = 0;