From 1c2f92b3bc52e5b81ed689506fee779338de71e7 Mon Sep 17 00:00:00 2001 From: nick black Date: Wed, 13 May 2020 10:50:42 -0400 Subject: [PATCH] =?UTF-8?q?ncmetric:=20replace=20u=20with=20=C2=B5,=20defi?= =?UTF-8?q?ne=20*COLUMNS=20#540?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/notcurses/notcurses.h | 30 ++++++++++++++++++------------ src/demo/demo.c | 12 ++++++------ src/lib/cpp.h | 8 ++++---- src/lib/metric.c | 5 ++--- tests/main.cpp | 5 +++++ tests/metric.cpp | 3 ++- 6 files changed, 37 insertions(+), 26 deletions(-) diff --git a/include/notcurses/notcurses.h b/include/notcurses/notcurses.h index 55f3188cf..6c859581d 100644 --- a/include/notcurses/notcurses.h +++ b/include/notcurses/notcurses.h @@ -2341,30 +2341,36 @@ 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); -#define PREFIXSTRLEN 7 // Does not include a '\0' (xxx.xxU) -#define IPREFIXSTRLEN 8 // Does not include a '\0' (xxxx.xxU) -#define BPREFIXSTRLEN 9 // Does not include a '\0' (xxxx.xxUi), i == prefix +// 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. +#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(PREFIXSTRLEN) "s" -#define IPREFIXFMT "%" STRHACK2(IPREFIXSTRLEN) "s" -#define BPREFIXFMT "%" STRHACK2(BPREFIXSTRLEN) "s" +#define PREFIXFMT "%" STRHACK2(PREFIXCOLUMNS) "s" +#define IPREFIXFMT "%" STRHACK2(IPREFIXCOLUMNS) "s" +#define BPREFIXFMT "%" STRHACK2(BPREFIXCOLUMNS) "s" // Takes an arbitrarily large number, and prints it into a fixed-size buffer by -// adding the necessary SI suffix. Usually, pass a |[B]PREFIXSTRLEN+1|-sized -// buffer to generate up to [B]PREFIXSTRLEN characters. The characteristic can -// occupy up through |mult-1| characters (3 for 1000, 4 for 1024). The mantissa -// can occupy either zero or two characters. +// adding the necessary SI suffix. Usually, pass a |[IB]PREFIXSTRLEN+1|-sized +// buffer to generate up to |[IB]PREFIXCOLUMNS| columns' worth of EGCs. The +// characteristic can occupy up through |mult-1| characters (3 for 1000, 4 for +// 1024). The mantissa can occupy either zero or two characters. // // Floating-point is never used, because an IEEE758 double can only losslessly // represent integers through 2^53-1. // // 2^64-1 is 18446744073709551615, 18.45E(xa). KMGTPEZY thus suffice to handle // an 89-bit uintmax_t. Beyond Z(etta) and Y(otta) lie lands unspecified by SI. -// +// 2^-63 is 0.000000000000000000108, 1.08a(tto). // val: value to print // decimal: scaling. '1' if none has taken place. // buf: buffer in which string will be generated @@ -2372,7 +2378,7 @@ API struct ncplane* nctablet_ncplane(struct nctablet* t); // mult: base of suffix system (almost always 1000 or 1024) // uprefix: character to print following suffix ('i' for kibibytes basically). // only printed if suffix is actually printed (input >= mult). -API const char* ncmetric(uintmax_t val, unsigned decimal, char* buf, +API const char* ncmetric(uintmax_t val, uintmax_t decimal, char* buf, int omitdec, unsigned mult, int uprefix); // Mega, kilo, gigafoo. Use PREFIXSTRLEN + 1. diff --git a/src/demo/demo.c b/src/demo/demo.c index 6e84843ba..880f1580c 100644 --- a/src/demo/demo.c +++ b/src/demo/demo.c @@ -368,10 +368,10 @@ 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│%7lu│%*s│ %*ss│%3ld│%7.1f│%7.1f║", - PREFIXSTRLEN, timebuf, + PREFIXCOLUMNS, timebuf, results[i].stats.renders, - BPREFIXSTRLEN, totalbuf, - PREFIXSTRLEN, rtimebuf, + BPREFIXCOLUMNS, totalbuf, + PREFIXCOLUMNS, rtimebuf, results[i].timens ? results[i].stats.render_ns * 100 / results[i].timens : 0, results[i].timens ? @@ -394,10 +394,10 @@ summary_table(struct ncdirect* nc, const char* spec){ qprefix(totalrenderns, GIG, rtimebuf, 0); table_segment(nc, "", "══╧═════════╪════════╪═══════╪═════════╪═════════╪═══╪═══════╪═══════╝\n"); printf(" │"); - table_printf(nc, "│", "%*ss", PREFIXSTRLEN, timebuf); + table_printf(nc, "│", "%*ss", PREFIXCOLUMNS, timebuf); table_printf(nc, "│", "%7lu", totalframes); - table_printf(nc, "│", "%*s", BPREFIXSTRLEN, totalbuf); - table_printf(nc, "│", " %*ss", PREFIXSTRLEN, rtimebuf); + table_printf(nc, "│", "%*s", BPREFIXCOLUMNS, totalbuf); + table_printf(nc, "│", " %*ss", PREFIXCOLUMNS, rtimebuf); table_printf(nc, "│", "%3ld", nsdelta ? totalrenderns * 100 / nsdelta : 0); table_printf(nc, "│", "%7.1f", nsdelta ? totalframes / ((double)nsdelta / GIG) : 0); printf("\n"); diff --git a/src/lib/cpp.h b/src/lib/cpp.h index 9853e60c6..35275d20c 100644 --- a/src/lib/cpp.h +++ b/src/lib/cpp.h @@ -51,7 +51,7 @@ class ncppplot { // if we're sizing the plot based off the plane dimensions, scale it by the // plot geometry's width for all calculations const int scaleddim = dimx * geomdata[opts->gridtype].width; - const int scaledprefixlen = PREFIXSTRLEN * geomdata[opts->gridtype].width; + const int scaledprefixlen = PREFIXCOLUMNS * geomdata[opts->gridtype].width; if((ncpp->slotcount = ncpp->rangex) == 0){ ncpp->slotcount = scaleddim; } @@ -141,7 +141,7 @@ class ncppplot { }else{ interval = maxy < miny ? 0 : (maxy - miny) / ((double)dimy * states); } - const int startx = labelaxisd ? PREFIXSTRLEN : 0; // plot cols begin here + const int startx = labelaxisd ? PREFIXCOLUMNS : 0; // plot cols begin here // if we want fewer slots than there are available columns, our final column // will be other than the plane's final column. most recent x goes here. const int finalx = (slotcount < scaleddim - 1 - (startx * scale) ? startx + (slotcount / scale) - 1 : dimx - 1); @@ -154,7 +154,7 @@ class ncppplot { }else{ ncmetric(interval * states * (y + 1) * 100, 100, buf, 0, 1000, '\0'); } - ncplane_printf_yx(ncp, dimy - y - 1, 0, "%*s", PREFIXSTRLEN, buf); + ncplane_printf_yx(ncp, dimy - y - 1, 0, "%*s", PREFIXCOLUMNS, buf); } } if(finalx < startx){ // exit on pathologically narrow planes @@ -321,7 +321,7 @@ class ncppplot { int slotcount; int slotstart; // index of most recently-written slot int64_t slotx; // x value corresponding to slots[slotstart] (newest x) - bool labelaxisd; // label dependent axis (consumes PREFIXSTRLEN columns) + bool labelaxisd; // label dependent axis (consumes PREFIXCOLUMNS columns) bool exponentiali; // exponential independent axis bool detectdomain; // is domain detection in effect (stretch the domain)? diff --git a/src/lib/metric.c b/src/lib/metric.c index 3f26c79cb..7da043797 100644 --- a/src/lib/metric.c +++ b/src/lib/metric.c @@ -4,12 +4,11 @@ #include #include "notcurses/notcurses.h" -const char *ncmetric(uintmax_t val, unsigned decimal, char *buf, int omitdec, +const char *ncmetric(uintmax_t val, uintmax_t decimal, char *buf, int omitdec, unsigned mul, int uprefix){ const unsigned mult = mul; // FIXME kill const char prefixes[] = "KMGTPEZY"; // 10^21-1 encompasses 2^64-1 - // FIXME can't use multibyte μ unless we enlarge the target buffer - const char subprefixes[] = "munpfazy"; // 10^24-1 + const char subprefixes[] = "mµnpfazy"; // 10^24-1 unsigned consumed = 0; uintmax_t dv; diff --git a/tests/main.cpp b/tests/main.cpp index 506bbea0e..2b8fcb179 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -62,6 +62,11 @@ reset_terminal(){ printf("%s", str); } fflush(stdout); + str = tigetstr("oc"); + if(str){ + printf("%s", str); + } + fflush(stdout); close(fd); } } diff --git a/tests/metric.cpp b/tests/metric.cpp index 8d41f2a7b..3180d426b 100644 --- a/tests/metric.cpp +++ b/tests/metric.cpp @@ -90,7 +90,7 @@ TEST_CASE("Metric") { ncmetric(INTMAX_MAX - 1, 1, buf, 0, 1024, 'i'); sprintf(gold, "%.2fEi", ((double)(INTMAX_MAX - (1ull << 53))) / (1ull << 60)); CHECK(!strcmp(gold, buf)); - ncmetric(INTMAX_MAX + 1ull, 1, buf, 0, 1024, 'i'); + REQUIRE(ncmetric(INTMAX_MAX + 1ull, 1, buf, 0, 1024, 'i')); sprintf(gold, "%.2fEi", ((double)(INTMAX_MAX + 1ull)) / (1ull << 60)); CHECK(!strcmp(gold, buf)); impericize_ncmetric(UINTMAX_MAX - 1, 1, buf, 0, 1024, 'i'); @@ -103,6 +103,7 @@ TEST_CASE("Metric") { } const char suffixes[] = "\0KMGTPE"; + //const char smallsuffixes[] = "\0munpfazy"; SUBCASE("PowersOfTen") { char gold[PREFIXSTRLEN + 1];