ncmetric: adapt PREFIXFMT to multibyte #630

This commit is contained in:
nick black 2020-05-22 06:46:23 -04:00
parent 4e9f46e063
commit cce9384ab8
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC
2 changed files with 15 additions and 18 deletions

View File

@ -2339,23 +2339,22 @@ API void* nctablet_userptr(struct nctablet* t);
// Access the ncplane associated with this nctablet, if one exists.
API struct ncplane* nctablet_ncplane(struct nctablet* t);
// The number of columns is one fewer, as the expressions here must
// leave an extra byte open in case “µ” (U+00B5, 0xC2 0xB5) shows up.
// The number of columns is one fewer, as the STRLEN expressions must
// leave an extra byte open in case 'µ' (U+00B5, 0xC2 0xB5) shows up.
#define PREFIXCOLUMNS 7
#define IPREFIXCOLUMNS 8
#define BPREFIXCOLUMNS 9
#define PREFIXSTRLEN (PREFIXCOLUMNS + 1) // Does not include a '\0' (xxx.xxU)
#define IPREFIXSTRLEN (IPREFIXCOLUMNS + 1) // Does not include a '\0' (xxxx.xxU)
#define BPREFIXSTRLEN (BPREFIXCOLUMNS + 1) // Does not include a '\0' (xxxx.xxUi), i == prefix
// A bit of the nasties here to stringize our preprocessor tokens just now
// #defined, making them usable as printf(3) specifiers.
#define STRHACK1(x) #x
#define STRHACK2(x) STRHACK1(x)
#define PREFIXFMT "%" STRHACK2(PREFIXCOLUMNS) "s"
#define IPREFIXFMT "%" STRHACK2(IPREFIXCOLUMNS) "s"
#define BPREFIXFMT "%" STRHACK2(BPREFIXCOLUMNS) "s"
// Used as arguments to a variable field width (i.e. "%*s" -- these are the *).
// We need this convoluted grotesquery to properly handle 'µ'.
#define PREFIXFWIDTH(x) ((int)(strlen(x) - mbswidth(x) + PREFIXCOLUMNS))
#define IPREFIXFWIDTH(x) ((int)(strlen(x) - mbswidth(x) + IPREFIXCOLUMNS))
#define BPREFIXFWIDTH(x) ((int)(strlen(x) - mbswidth(x) + BPREFIXCOLUMNS))
#define PREFIXFMT(x) PREFIXFWIDTH(x), x
#define IPREFIXFMT(x) IPREFIXFWIDTH(x), x
#define BPREFIXFMT(x) BPREFIXFWIDTH(x), x
// Takes an arbitrarily large number, and prints it into a fixed-size buffer by
// adding the necessary SI suffix. Usually, pass a |[IB]PREFIXSTRLEN+1|-sized

View File

@ -369,10 +369,8 @@ summary_table(struct ncdirect* nc, const char* spec){
printf("%9s", demos[results[i].selector - 'a'].name);
ncdirect_fg_rgb8(nc, 178, 102, 255);
printf("│%*ss│%7ju│%*s│ %*ss│%3jd│%7.1f│%7.1f║",
PREFIXCOLUMNS, timebuf,
(uintmax_t)(results[i].stats.renders),
BPREFIXCOLUMNS, totalbuf,
PREFIXCOLUMNS, rtimebuf,
PREFIXFMT(timebuf), (uintmax_t)(results[i].stats.renders),
BPREFIXFMT(totalbuf), PREFIXFMT(rtimebuf),
(uintmax_t)(results[i].timens ?
results[i].stats.render_ns * 100 / results[i].timens : 0),
results[i].timens ?
@ -395,10 +393,10 @@ summary_table(struct ncdirect* nc, const char* spec){
qprefix(totalrenderns, GIG, rtimebuf, 0);
table_segment(nc, "", "══╧═════════╪════════╪═══════╪═════════╪═════════╪═══╪═══════╪═══════╝\n");
printf("");
table_printf(nc, "", "%*ss", PREFIXCOLUMNS, timebuf);
table_printf(nc, "", "%*ss", PREFIXFMT(timebuf));
table_printf(nc, "", "%7lu", totalframes);
table_printf(nc, "", "%*s", BPREFIXCOLUMNS, totalbuf);
table_printf(nc, "", " %*ss", PREFIXCOLUMNS, rtimebuf);
table_printf(nc, "", "%*s", BPREFIXFMT(totalbuf));
table_printf(nc, "", " %*ss", PREFIXFMT(rtimebuf));
table_printf(nc, "", "%3ld", nsdelta ? totalrenderns * 100 / nsdelta : 0);
table_printf(nc, "", "%7.1f", nsdelta ? totalframes / ((double)nsdelta / GIG) : 0);
printf("\n");