prefix qprefix(), bprefix(), iprefix() with nc-

This commit is contained in:
nick black 2021-11-07 05:52:31 -05:00 committed by nick black
parent 83ff2cfe5a
commit 0231768406
15 changed files with 74 additions and 68 deletions

View File

@ -12,6 +12,8 @@ rearrangements of Notcurses.
deprecated functionality, ABI3 ought require small changes, if any.
* 2.4.9 (not yet released)
* `qprefix()`, `bprefix()`, and `iprefix()` have been renamed
`ncqprefix()`, `ncbprefix()`, and `nciprefix()`, respectively.
* `notcurses_mice_enable()` and `notcurses_mouse_disable()` replace
`notcurses_mouse_enable()` and `notcurses_mouse_disable()`, which
have been deprecated, and will be removed in ABI3.

View File

@ -25,11 +25,11 @@ notcurses_metric - fixed-width numeric output with metric suffixes
**const char* ncmetric(uintmax_t ***val***, uintmax_t ***decimal***, char* ***buf***, int ***omitdec***, unsigned ***mult***, int ***uprefix***);**
**static inline const char* qprefix(uintmax_t ***val***, uintmax_t ***decimal***, char* ***buf***, int ***omitdec***);**
**static inline const char* ncqprefix(uintmax_t ***val***, uintmax_t ***decimal***, char* ***buf***, int ***omitdec***);**
**static inline const char* iprefix(uintmax_t ***val***, uintmax_t ***decimal***, char* ***buf***, int ***omitdec***);**
**static inline const char* nciprefix(uintmax_t ***val***, uintmax_t ***decimal***, char* ***buf***, int ***omitdec***);**
**static inline const char* bprefix(uintmax_t ***val***, uintmax_t ***decimal***, char* ***buf***, int ***omitdec***);**
**static inline const char* ncbprefix(uintmax_t ***val***, uintmax_t ***decimal***, char* ***buf***, int ***omitdec***);**
# DESCRIPTION
@ -58,19 +58,19 @@ Three helper functions are provided to simplify these common cases:
```
// Mega, kilo, gigafoo. Use PREFIXSTRLEN + 1 and PREFIXCOLUMNS.
static inline const char*
qprefix(uintmax_t val, uintmax_t decimal, char* buf, int omitdec){
ncqprefix(uintmax_t val, uintmax_t decimal, char* buf, int omitdec){
return ncmetric(val, decimal, buf, omitdec, 1000, '\0');
}
// Mibi, kebi, gibibytes sans 'i' suffix. Use IPREFIXSTRLEN + 1.
static inline const char*
iprefix(uintmax_t val, uintmax_t decimal, char* buf, int omitdec){
nciprefix(uintmax_t val, uintmax_t decimal, char* buf, int omitdec){
return ncmetric(val, decimal, buf, omitdec, 1024, '\0');
}
// Mibi, kebi, gibibytes. Use BPREFIXSTRLEN + 1 and BPREFIXCOLUMNS.
static inline const char*
bprefix(uintmax_t val, uintmax_t decimal, char* buf, int omitdec){
ncbprefix(uintmax_t val, uintmax_t decimal, char* buf, int omitdec){
return ncmetric(val, decimal, buf, omitdec, 1024, 'i');
}
```

View File

@ -71,17 +71,17 @@ namespace ncpp
static const char* qprefix (uintmax_t val, uintmax_t decimal, char *buf, int omitdec) noexcept
{
return ::qprefix (val, decimal, buf, omitdec);
return ::ncqprefix (val, decimal, buf, omitdec);
}
static const char* iprefix (uintmax_t val, uintmax_t decimal, char *buf, int omitdec) noexcept
{
return ::iprefix (val, decimal, buf, omitdec);
return ::nciprefix (val, decimal, buf, omitdec);
}
static const char* bprefix (uintmax_t val, uintmax_t decimal, char *buf, int omitdec) noexcept
{
return ::bprefix (val, decimal, buf, omitdec);
return ::ncbprefix (val, decimal, buf, omitdec);
}
static const char* version () noexcept

View File

@ -3389,19 +3389,19 @@ API const char* ncmetric(uintmax_t val, uintmax_t decimal, char* buf,
// Mega, kilo, gigafoo. Use PREFIXSTRLEN + 1 and PREFIXCOLUMNS.
static inline const char*
qprefix(uintmax_t val, uintmax_t decimal, char* buf, int omitdec){
ncqprefix(uintmax_t val, uintmax_t decimal, char* buf, int omitdec){
return ncmetric(val, decimal, buf, omitdec, 1000, '\0');
}
// Mibi, kebi, gibibytes sans 'i' suffix. Use IPREFIXSTRLEN + 1.
static inline const char*
iprefix(uintmax_t val, uintmax_t decimal, char* buf, int omitdec){
nciprefix(uintmax_t val, uintmax_t decimal, char* buf, int omitdec){
return ncmetric(val, decimal, buf, omitdec, 1024, '\0');
}
// Mibi, kebi, gibibytes. Use BPREFIXSTRLEN + 1 and BPREFIXCOLUMNS.
static inline const char*
bprefix(uintmax_t val, uintmax_t decimal, char* buf, int omitdec){
ncbprefix(uintmax_t val, uintmax_t decimal, char* buf, int omitdec){
return ncmetric(val, decimal, buf, omitdec, 1024, 'i');
}

View File

@ -410,14 +410,14 @@ summary_table(struct notcurses* nc, const char* spec, bool canimage, bool canvid
uint64_t nsdelta = 0;
for(size_t i = 0 ; i < strlen(spec) ; ++i){
nsdelta += results[i].timens;
qprefix(results[i].timens, NANOSECS_IN_SEC, timebuf, 0);
bprefix(results[i].stats.render_bytes, 1, totalbuf, 0);
ncqprefix(results[i].timens, NANOSECS_IN_SEC, timebuf, 0);
ncbprefix(results[i].stats.render_bytes, 1, totalbuf, 0);
uint64_t divisor = results[i].stats.render_ns + results[i].stats.writeout_ns + results[i].stats.raster_ns;
if(divisor){
qprefix((uintmax_t)results[i].stats.writeouts * NANOSECS_IN_SEC * 1000 / divisor,
ncqprefix((uintmax_t)results[i].stats.writeouts * NANOSECS_IN_SEC * 1000 / divisor,
1000, tfpsbuf, 0);
}else{
qprefix(0, NANOSECS_IN_SEC, tfpsbuf, 0);
ncqprefix(0, NANOSECS_IN_SEC, tfpsbuf, 0);
}
uint32_t rescolor;
if(results[i].result < 0){
@ -459,8 +459,8 @@ summary_table(struct notcurses* nc, const char* spec, bool canimage, bool canvid
totalrenderns += results[i].stats.render_ns;
totalwriteoutns += results[i].stats.writeout_ns;
}
qprefix(nsdelta, NANOSECS_IN_SEC, timebuf, 0);
bprefix(totalbytes, 1, totalbuf, 0);
ncqprefix(nsdelta, NANOSECS_IN_SEC, timebuf, 0);
ncbprefix(totalbytes, 1, totalbuf, 0);
table_segment(n, "", "══╧════════╧════════╪═══════╪═════════╪═══════╧══╧══╧══╧═══════╝\n");
ncplane_putstr(n, " ");
table_printf(n, "", "%*ss", PREFIXFMT(timebuf));

View File

@ -119,7 +119,7 @@ draw_block(struct ncplane* nn, uint32_t blockstart){
}
int unicodeblocks_demo(struct notcurses* nc){
int maxx, maxy;
unsigned maxx, maxy;
struct ncplane* n = notcurses_stddim_yx(nc, &maxy, &maxx);
// some blocks are good for the printing, some less so. some are only
// marginally covered by mainstream fonts, some not at all. we explicitly

View File

@ -14,7 +14,7 @@
static struct ncplane*
mathplane(struct notcurses* nc){
struct ncplane* stdn = notcurses_stdplane(nc);
int dimx, dimy;
unsigned dimx, dimy;
notcurses_term_dim_yx(nc, &dimy, &dimx);
const int HEIGHT = 9;
const int WIDTH = dimx;
@ -468,7 +468,8 @@ int witherworm_demo(struct notcurses* nc){
const int start = starts[i];
int step = steps[i];
nccell_init(&c);
int y, x, maxy, maxx;
int y, x;
unsigned maxy, maxx;
ncplane_dim_yx(n, &maxy, &maxx); // might resize
int rgb = start;
int bytes_out = 0;
@ -488,11 +489,11 @@ int witherworm_demo(struct notcurses* nc){
if(ncplane_set_fg_rgb8(n, ncchannel_r(rgb), ncchannel_g(rgb), ncchannel_b(rgb))){
return -1;
}
if(x >= maxx){
if(x >= (int)maxx){
x = 0;
++y;
}
if(y >= maxy){
if(y >= (int)maxy){
break;
}
wchar_t wcs;
@ -508,14 +509,14 @@ int witherworm_demo(struct notcurses* nc){
}
int ulen = 0;
int r;
if(wcwidth(wcs) <= maxx - x){
//if(wcwidth(wcs) <= maxx - x){
if((r = ncplane_putegc(n, &(*s)[idx], &ulen)) <= 0){
if(ulen < 0){
return -1;
}else if(ulen == 0){
break; // FIXME work around missing unicode
}
}
//}
}else{
if((r = ncplane_putchar(n, '#')) < 1){
return -1;
@ -528,7 +529,7 @@ int witherworm_demo(struct notcurses* nc){
++egcs_out;
}
rgb += step;
}while(y < maxy);
}while(y < (int)maxy);
struct ncplane* math = mathplane(nc);
if(math == NULL){
return -1;

View File

@ -75,7 +75,7 @@ struct marsh {
// make a plane on a new pile suitable for rendering a frame of the video
static int
make_plane(struct notcurses* nc, struct ncplane** t){
int dimy, dimx;
unsigned dimy, dimx;
notcurses_stddim_yx(nc, &dimy, &dimx);
// FIXME want a resizecb
struct ncplane_options opts = {
@ -167,7 +167,7 @@ int xray_demo(struct notcurses* nc){
if(!notcurses_canopen_videos(nc)){
return 0;
}
int dimx, dimy;
unsigned dimx, dimy;
notcurses_term_dim_yx(nc, &dimy, &dimx);
ncplane_erase(notcurses_stdplane(nc));
char* path = find_data("notcursesIII.mkv");

View File

@ -431,8 +431,8 @@ infoplane_notcurses(struct notcurses* nc, const fetched_info* fi, int planeheigh
struct sysinfo sinfo;
sysinfo(&sinfo);
char totalmet[BPREFIXSTRLEN + 1], usedmet[BPREFIXSTRLEN + 1];
bprefix(sinfo.totalram, 1, totalmet, 1);
bprefix(sinfo.totalram - sinfo.freeram, 1, usedmet, 1);
ncbprefix(sinfo.totalram, 1, totalmet, 1);
ncbprefix(sinfo.totalram - sinfo.freeram, 1, usedmet, 1);
ncplane_printf_aligned(infop, 2, NCALIGN_RIGHT, "Processes: %hu ", sinfo.procs);
ncplane_printf_aligned(infop, 2, NCALIGN_LEFT, " RAM: %sB/%sB", usedmet, totalmet);
#elif defined(BSD)

View File

@ -822,9 +822,9 @@ sprixel_state(const sprixel* s, int y, int x){
int localx = x - (s->n->absx - stdn->absx);
//fprintf(stderr, "TAM %d at %d/%d (%d/%d, %d/%d)\n", s->n->tam[localy * s->dimx + localx].state, localy, localx, y, x, s->dimy, s->dimx);
assert(localy >= 0);
assert(localy < s->dimy);
assert(localy < (int)s->dimy);
assert(localx >= 0);
assert(localx < s->dimx);
assert(localx < (int)s->dimx);
return s->n->tam[localy * s->dimx + localx].state;
}

View File

@ -697,34 +697,34 @@ int ncplane_resize_internal(ncplane* n, int keepy, int keepx,
int yoff, int xoff,
unsigned ylen, unsigned xlen){
if(keepy < 0 || keepx < 0){ // can't start at negative origin
logerror("Can't retain negative offset %dx%d\n", keepy, keepx);
logerror("can't retain negative offset %dx%d\n", keepy, keepx);
return -1;
}
if((!keepleny && keeplenx) || (keepleny && !keeplenx)){ // both must be 0
logerror("Can't retain null dimension %dx%d\n", keepleny, keeplenx);
logerror("can't retain null dimension %dx%d\n", keepleny, keeplenx);
return -1;
}
// can't be smaller than keep length
if(ylen < keepleny){
logerror("Can't map in y dimension: %u < %d\n", ylen, keepleny);
logerror("can't map in y dimension: %u < %d\n", ylen, keepleny);
return -1;
}
if(xlen < keeplenx){
logerror("Can't map in x dimension: %u < %d\n", xlen, keeplenx);
logerror("can't map in x dimension: %u < %d\n", xlen, keeplenx);
return -1;
}
if(ylen <= 0 || xlen <= 0){ // can't resize to trivial or negative size
logerror("Can't achieve meaningless size %ux%u\n", ylen, xlen);
logerror("can't achieve meaningless size %ux%u\n", ylen, xlen);
return -1;
}
unsigned rows, cols;
ncplane_dim_yx(n, &rows, &cols);
if(keepleny + keepy > (unsigned)rows){
logerror("Can't keep %d@%d rows from %d\n", keepleny, keepy, rows);
logerror("can't keep %d@%d rows from %d\n", keepleny, keepy, rows);
return -1;
}
if(keeplenx + keepx > (unsigned)cols){
logerror("Can't keep %d@%d cols from %d\n", keeplenx, keepx, cols);
logerror("can't keep %d@%d cols from %d\n", keeplenx, keepx, cols);
return -1;
}
loginfo("%dx%d @ %d/%d → %u/%u @ %d/%d (want %ux%u@%d/%d)\n", rows, cols, n->absy, n->absx, ylen, xlen, n->absy + keepy + yoff, n->absx + keepx + xoff, keepleny, keeplenx, keepy, keepx);

View File

@ -171,39 +171,39 @@ void summarize_stats(notcurses* nc){
char maxbuf[BPREFIXSTRLEN + 1];
char avgbuf[BPREFIXSTRLEN + 1];
if(stats->renders){
qprefix(stats->render_ns, NANOSECS_IN_SEC, totalbuf, 0);
qprefix(stats->render_min_ns, NANOSECS_IN_SEC, minbuf, 0);
qprefix(stats->render_max_ns, NANOSECS_IN_SEC, maxbuf, 0);
qprefix(stats->render_ns / stats->renders, NANOSECS_IN_SEC, avgbuf, 0);
ncqprefix(stats->render_ns, NANOSECS_IN_SEC, totalbuf, 0);
ncqprefix(stats->render_min_ns, NANOSECS_IN_SEC, minbuf, 0);
ncqprefix(stats->render_max_ns, NANOSECS_IN_SEC, maxbuf, 0);
ncqprefix(stats->render_ns / stats->renders, NANOSECS_IN_SEC, avgbuf, 0);
fprintf(stderr, "%s%"PRIu64" render%s, %ss (%ss min, %ss avg, %ss max)" NL,
clreol, stats->renders, stats->renders == 1 ? "" : "s",
totalbuf, minbuf, avgbuf, maxbuf);
}
if(stats->writeouts || stats->failed_writeouts){
qprefix(stats->raster_ns, NANOSECS_IN_SEC, totalbuf, 0);
qprefix(stats->raster_min_ns, NANOSECS_IN_SEC, minbuf, 0);
qprefix(stats->raster_max_ns, NANOSECS_IN_SEC, maxbuf, 0);
qprefix(stats->raster_ns / (stats->writeouts + stats->failed_writeouts),
ncqprefix(stats->raster_ns, NANOSECS_IN_SEC, totalbuf, 0);
ncqprefix(stats->raster_min_ns, NANOSECS_IN_SEC, minbuf, 0);
ncqprefix(stats->raster_max_ns, NANOSECS_IN_SEC, maxbuf, 0);
ncqprefix(stats->raster_ns / (stats->writeouts + stats->failed_writeouts),
NANOSECS_IN_SEC, avgbuf, 0);
fprintf(stderr, "%s%"PRIu64" raster%s, %ss (%ss min, %ss avg, %ss max)" NL,
clreol, stats->writeouts, stats->writeouts == 1 ? "" : "s",
totalbuf, minbuf, avgbuf, maxbuf);
qprefix(stats->writeout_ns, NANOSECS_IN_SEC, totalbuf, 0);
qprefix(stats->writeout_ns ? stats->writeout_min_ns : 0,
ncqprefix(stats->writeout_ns, NANOSECS_IN_SEC, totalbuf, 0);
ncqprefix(stats->writeout_ns ? stats->writeout_min_ns : 0,
NANOSECS_IN_SEC, minbuf, 0);
qprefix(stats->writeout_max_ns, NANOSECS_IN_SEC, maxbuf, 0);
qprefix(stats->writeouts ? stats->writeout_ns / stats->writeouts : 0,
ncqprefix(stats->writeout_max_ns, NANOSECS_IN_SEC, maxbuf, 0);
ncqprefix(stats->writeouts ? stats->writeout_ns / stats->writeouts : 0,
NANOSECS_IN_SEC, avgbuf, 0);
fprintf(stderr, "%s%"PRIu64" write%s, %ss (%ss min, %ss avg, %ss max)" NL,
clreol, stats->writeouts, stats->writeouts == 1 ? "" : "s",
totalbuf, minbuf, avgbuf, maxbuf);
}
if(stats->renders || stats->input_events){
bprefix(stats->render_bytes, 1, totalbuf, 1),
bprefix(stats->render_bytes ? stats->render_min_bytes : 0,
ncbprefix(stats->render_bytes, 1, totalbuf, 1),
ncbprefix(stats->render_bytes ? stats->render_min_bytes : 0,
1, minbuf, 1),
bprefix(stats->renders ? stats->render_bytes / stats->renders : 0, 1, avgbuf, 1);
bprefix(stats->render_max_bytes, 1, maxbuf, 1),
ncbprefix(stats->renders ? stats->render_bytes / stats->renders : 0, 1, avgbuf, 1);
ncbprefix(stats->render_max_bytes, 1, maxbuf, 1),
fprintf(stderr, "%s%sB (%sB min, %sB avg, %sB max) %"PRIu64" input%s Ghpa: %"PRIu64 NL,
clreol, totalbuf, minbuf, avgbuf, maxbuf,
stats->input_events,
@ -234,7 +234,7 @@ void summarize_stats(notcurses* nc){
(stats->fgelisions * 100.0) / (stats->fgemissions + stats->fgelisions),
(stats->bgemissions + stats->bgelisions) == 0 ? 0 :
(stats->bgelisions * 100.0) / (stats->bgemissions + stats->bgelisions));
bprefix(stats->sprixelbytes, 1, totalbuf, 1);
ncbprefix(stats->sprixelbytes, 1, totalbuf, 1);
fprintf(stderr, "%sBitmap emits:elides: %"PRIu64":%"PRIu64" (%.2f%%) %sB (%.2f%%) SuM: %"PRIu64" (%.2f%%)" NL,
clreol, stats->sprixelemissions, stats->sprixelelisions,
(stats->sprixelemissions + stats->sprixelelisions) == 0 ? 0 :

View File

@ -43,9 +43,9 @@ int main(int argc, char** argv){
}
setlocale(LC_ALL, "");
notcurses_options opts = {
.flags = NCOPTION_INHIBIT_SETLOCALE
| NCOPTION_SUPPRESS_BANNERS
| NCOPTION_DRAIN_INPUT,
.flags = NCOPTION_NO_ALTERNATE_SCREEN
| NCOPTION_INHIBIT_SETLOCALE
| NCOPTION_SUPPRESS_BANNERS,
};
struct notcurses* nc = notcurses_core_init(&opts, NULL);
if(nc == NULL){
@ -73,7 +73,10 @@ int main(int argc, char** argv){
ncplane_set_fg_rgb(std, 0x00bcaa);
ncplane_printf_aligned(std, -1, NCALIGN_CENTER, "press any key to continue (%s)", *argv);
notcurses_render(nc);
notcurses_get(nc, NULL, NULL);
ncinput ni;
do{
notcurses_getc_blocking(nc, &ni);
}while(ni.evtype == NCTYPE_RELEASE);
if(notcurses_stop(nc)){
return EXIT_FAILURE;
}

View File

@ -53,7 +53,7 @@ int main(int argc, char** argv){
if(notcurses_render(nc)){
goto err;
}
if(top > bot){
if(top >= bot){
break;
}
// one from bottom

View File

@ -328,7 +328,7 @@ TEST_CASE("Metric") {
char buf[PREFIXSTRLEN + 1];
uintmax_t val = 9027854993;
uintmax_t decimal = GIG;
REQUIRE(qprefix(val, decimal, buf, 0));
REQUIRE(ncqprefix(val, decimal, buf, 0));
CHECK(!strcmp(buf, gold));
}
@ -338,7 +338,7 @@ TEST_CASE("Metric") {
char buf[PREFIXSTRLEN + 1];
uintmax_t decimal = GIG;
uintmax_t val = decimal;
REQUIRE(qprefix(val, decimal, buf, 0));
REQUIRE(ncqprefix(val, decimal, buf, 0));
CHECK(!strcmp(buf, gold));
}
@ -348,7 +348,7 @@ TEST_CASE("Metric") {
char buf[PREFIXSTRLEN + 1];
uintmax_t val = 1000000001;
uintmax_t decimal = GIG;
REQUIRE(qprefix(val, decimal, buf, 0));
REQUIRE(ncqprefix(val, decimal, buf, 0));
CHECK(!strcmp(buf, gold));
}
@ -358,7 +358,7 @@ TEST_CASE("Metric") {
char buf[PREFIXSTRLEN + 1];
uintmax_t val = 999999999;
uintmax_t decimal = GIG;
REQUIRE(qprefix(val, decimal, buf, 0));
REQUIRE(ncqprefix(val, decimal, buf, 0));
CHECK(!strcmp(buf, gold));
}
@ -368,7 +368,7 @@ TEST_CASE("Metric") {
char buf[PREFIXSTRLEN + 1];
uintmax_t val = 27854993;
uintmax_t decimal = GIG;
REQUIRE(qprefix(val, decimal, buf, 0));
REQUIRE(ncqprefix(val, decimal, buf, 0));
CHECK(!strcmp(buf, gold));
}
@ -378,7 +378,7 @@ TEST_CASE("Metric") {
char buf[PREFIXSTRLEN + 1];
uintmax_t val = 7854993;
uintmax_t decimal = GIG;
REQUIRE(qprefix(val, decimal, buf, 0));
REQUIRE(ncqprefix(val, decimal, buf, 0));
CHECK(!strcmp(buf, gold));
}
@ -437,7 +437,7 @@ TEST_CASE("Metric") {
uintmax_t val = 1;
size_t i = 0;
do{
REQUIRE(qprefix(val, 1000000000000000ull, buf, '\0'));
REQUIRE(ncqprefix(val, 1000000000000000ull, buf, '\0'));
const int sidx = i / 3 + 3;
snprintf(gold, sizeof(gold), "%ju%s00%lc", goldval, decisep, smallsuffixes[sidx]);
CHECK(!strcmp(gold, buf));