ncmetric: replace u with µ, define *COLUMNS #540

pull/608/head
nick black 4 years ago
parent e3df2dca16
commit 1c2f92b3bc
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC

@ -2341,30 +2341,36 @@ API void* nctablet_userptr(struct nctablet* t);
// Access the ncplane associated with this nctablet, if one exists. // Access the ncplane associated with this nctablet, if one exists.
API struct ncplane* nctablet_ncplane(struct nctablet* t); API struct ncplane* nctablet_ncplane(struct nctablet* t);
#define PREFIXSTRLEN 7 // Does not include a '\0' (xxx.xxU) // The number of columns is one fewer, as the expressions here must
#define IPREFIXSTRLEN 8 // Does not include a '\0' (xxxx.xxU) // leave an extra byte open in case “µ” (U+00B5, 0xC2 0xB5) shows up.
#define BPREFIXSTRLEN 9 // Does not include a '\0' (xxxx.xxUi), i == prefix #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 // A bit of the nasties here to stringize our preprocessor tokens just now
// #defined, making them usable as printf(3) specifiers. // #defined, making them usable as printf(3) specifiers.
#define STRHACK1(x) #x #define STRHACK1(x) #x
#define STRHACK2(x) STRHACK1(x) #define STRHACK2(x) STRHACK1(x)
#define PREFIXFMT "%" STRHACK2(PREFIXSTRLEN) "s" #define PREFIXFMT "%" STRHACK2(PREFIXCOLUMNS) "s"
#define IPREFIXFMT "%" STRHACK2(IPREFIXSTRLEN) "s" #define IPREFIXFMT "%" STRHACK2(IPREFIXCOLUMNS) "s"
#define BPREFIXFMT "%" STRHACK2(BPREFIXSTRLEN) "s" #define BPREFIXFMT "%" STRHACK2(BPREFIXCOLUMNS) "s"
// Takes an arbitrarily large number, and prints it into a fixed-size buffer by // 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 // adding the necessary SI suffix. Usually, pass a |[IB]PREFIXSTRLEN+1|-sized
// buffer to generate up to [B]PREFIXSTRLEN characters. The characteristic can // buffer to generate up to |[IB]PREFIXCOLUMNS| columns' worth of EGCs. The
// occupy up through |mult-1| characters (3 for 1000, 4 for 1024). The mantissa // characteristic can occupy up through |mult-1| characters (3 for 1000, 4 for
// can occupy either zero or two characters. // 1024). The mantissa can occupy either zero or two characters.
// //
// Floating-point is never used, because an IEEE758 double can only losslessly // Floating-point is never used, because an IEEE758 double can only losslessly
// represent integers through 2^53-1. // represent integers through 2^53-1.
// //
// 2^64-1 is 18446744073709551615, 18.45E(xa). KMGTPEZY thus suffice to handle // 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. // 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 // val: value to print
// decimal: scaling. '1' if none has taken place. // decimal: scaling. '1' if none has taken place.
// buf: buffer in which string will be generated // 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) // mult: base of suffix system (almost always 1000 or 1024)
// uprefix: character to print following suffix ('i' for kibibytes basically). // uprefix: character to print following suffix ('i' for kibibytes basically).
// only printed if suffix is actually printed (input >= mult). // 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); int omitdec, unsigned mult, int uprefix);
// Mega, kilo, gigafoo. Use PREFIXSTRLEN + 1. // Mega, kilo, gigafoo. Use PREFIXSTRLEN + 1.

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

@ -51,7 +51,7 @@ class ncppplot {
// if we're sizing the plot based off the plane dimensions, scale it by the // if we're sizing the plot based off the plane dimensions, scale it by the
// plot geometry's width for all calculations // plot geometry's width for all calculations
const int scaleddim = dimx * geomdata[opts->gridtype].width; 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){ if((ncpp->slotcount = ncpp->rangex) == 0){
ncpp->slotcount = scaleddim; ncpp->slotcount = scaleddim;
} }
@ -141,7 +141,7 @@ class ncppplot {
}else{ }else{
interval = maxy < miny ? 0 : (maxy - miny) / ((double)dimy * states); 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 // 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. // 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); const int finalx = (slotcount < scaleddim - 1 - (startx * scale) ? startx + (slotcount / scale) - 1 : dimx - 1);
@ -154,7 +154,7 @@ class ncppplot {
}else{ }else{
ncmetric(interval * states * (y + 1) * 100, 100, buf, 0, 1000, '\0'); 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 if(finalx < startx){ // exit on pathologically narrow planes
@ -321,7 +321,7 @@ class ncppplot {
int slotcount; int slotcount;
int slotstart; // index of most recently-written slot int slotstart; // index of most recently-written slot
int64_t slotx; // x value corresponding to slots[slotstart] (newest x) 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 exponentiali; // exponential independent axis
bool detectdomain; // is domain detection in effect (stretch the domain)? bool detectdomain; // is domain detection in effect (stretch the domain)?

@ -4,12 +4,11 @@
#include <pthread.h> #include <pthread.h>
#include "notcurses/notcurses.h" #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){ unsigned mul, int uprefix){
const unsigned mult = mul; // FIXME kill const unsigned mult = mul; // FIXME kill
const char prefixes[] = "KMGTPEZY"; // 10^21-1 encompasses 2^64-1 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[] = "mµnpfazy"; // 10^24-1
const char subprefixes[] = "munpfazy"; // 10^24-1
unsigned consumed = 0; unsigned consumed = 0;
uintmax_t dv; uintmax_t dv;

@ -62,6 +62,11 @@ reset_terminal(){
printf("%s", str); printf("%s", str);
} }
fflush(stdout); fflush(stdout);
str = tigetstr("oc");
if(str){
printf("%s", str);
}
fflush(stdout);
close(fd); close(fd);
} }
} }

@ -90,7 +90,7 @@ TEST_CASE("Metric") {
ncmetric(INTMAX_MAX - 1, 1, buf, 0, 1024, 'i'); ncmetric(INTMAX_MAX - 1, 1, buf, 0, 1024, 'i');
sprintf(gold, "%.2fEi", ((double)(INTMAX_MAX - (1ull << 53))) / (1ull << 60)); sprintf(gold, "%.2fEi", ((double)(INTMAX_MAX - (1ull << 53))) / (1ull << 60));
CHECK(!strcmp(gold, buf)); 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)); sprintf(gold, "%.2fEi", ((double)(INTMAX_MAX + 1ull)) / (1ull << 60));
CHECK(!strcmp(gold, buf)); CHECK(!strcmp(gold, buf));
impericize_ncmetric(UINTMAX_MAX - 1, 1, buf, 0, 1024, 'i'); impericize_ncmetric(UINTMAX_MAX - 1, 1, buf, 0, 1024, 'i');
@ -103,6 +103,7 @@ TEST_CASE("Metric") {
} }
const char suffixes[] = "\0KMGTPE"; const char suffixes[] = "\0KMGTPE";
//const char smallsuffixes[] = "\0munpfazy";
SUBCASE("PowersOfTen") { SUBCASE("PowersOfTen") {
char gold[PREFIXSTRLEN + 1]; char gold[PREFIXSTRLEN + 1];

Loading…
Cancel
Save