From 4e6c59bef8461bd835d1b47f7d3d76348930b209 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Wed, 25 Jul 2007 14:17:58 -0700 Subject: [PATCH] Handle plural correctly in countdown. --- README.menu | 4 +++- com32/modules/menumain.c | 50 ++++++++++++++++++++++++++++++++++++++++++++-- com32/modules/readconfig.c | 2 +- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/README.menu b/README.menu index dec5410..1d7f52d 100644 --- a/README.menu +++ b/README.menu @@ -149,8 +149,10 @@ MENU INCLUDE filename MENU AUTOBOOT message - Replaces the message "Automatic boot in # seconds". The + Replaces the message "Automatic boot in # second{,s}...". The symbol # is replaced with the number of seconds remaining. + The syntax "{singular,[dual,]plural}" can be used to conjugate + appropriately. MENU TABMSG message diff --git a/com32/modules/menumain.c b/com32/modules/menumain.c index 5bd0eb2..b28371e 100644 --- a/com32/modules/menumain.c +++ b/com32/modules/menumain.c @@ -756,19 +756,65 @@ print_timeout_message(int tol, int row, const char *msg) char *tq = buf; while ((size_t)(tq-buf) < (sizeof buf-16) && (tc = *tp)) { + tp++; if (tc == '#') { nnc = sprintf(tq, "\2#15%d\2#14", tol); tq += nnc; nc += nnc-8; /* 8 formatting characters */ + } else if (tc == '{') { + /* Deal with {singular[,dual],plural} constructs */ + struct { + const char *s, *e; + } tx[3]; + const char *tpp; + int n = 0; + + memset(tx, 0, sizeof tx); + + tx[0].s = tp; + + while (*tp && *tp != '}') { + if (*tp == ',' && n < 2) { + tx[n].e = tp; + n++; + tx[n].s = tp+1; + } + tp++; + } + tx[n].e = tp; + + if (*tp) + tp++; /* Skip final bracket */ + + if (!tx[1].s) + tx[1] = tx[0]; + if (!tx[2].s) + tx[2] = tx[1]; + + /* Now [0] is singular, [1] is dual, and [2] is plural, + even if the user only specified some of them. */ + + switch (tol) { + case 1: n = 0; break; + case 2: n = 1; break; + default: n = 2; break; + } + + for (tpp = tx[n].s; tpp < tx[n].e; tpp++) { + if ((size_t)(tq-buf) < (sizeof buf)) { + *tq++ = *tpp; + nc++; + } + } } else { *tq++ = tc; nc++; } - tp++; } *tq = '\0'; - printf("\033[%d;%dH\2#14 %s ", row, HSHIFT+1+((WIDTH-nc-2)>>1), buf); + /* Let's hope 4 spaces on each side is enough... */ + printf("\033[%d;%dH\2#14 %s ", row, HSHIFT+1+((WIDTH-nc-8)>>1), buf); } static const char * diff --git a/com32/modules/readconfig.c b/com32/modules/readconfig.c index 844ef8a..89c0962 100644 --- a/com32/modules/readconfig.c +++ b/com32/modules/readconfig.c @@ -49,7 +49,7 @@ struct messages messages[MSG_COUNT] = { [MSG_TITLE] = { "title", "", NULL }, [MSG_AUTOBOOT] = - { "autoboot", "Automatic boot in # seconds", NULL }, + { "autoboot", "Automatic boot in # second{,s}...", NULL }, [MSG_TAB] = { "tabmsg", "Press [Tab] to edit options", NULL }, [MSG_NOTAB] = -- 2.7.4