From c1664170fdb9a259954ff3e8883451a3a36534dc Mon Sep 17 00:00:00 2001 From: nick black Date: Fri, 31 Jan 2020 21:25:21 -0500 Subject: [PATCH] selector: bgchannels only inside the body #302 --- README.md | 3 +-- doc/man/man3/notcurses_selector.3.md | 3 +-- include/notcurses.h | 3 +-- src/lib/internal.h | 1 + src/lib/selector.c | 36 +++++++++++++++++++++------- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index cb30f828d..d1d512401 100644 --- a/README.md +++ b/README.md @@ -2134,8 +2134,7 @@ typedef struct selector_options { uint64_t titlechannels;// title channels uint64_t footchannels; // secondary and footer channels uint64_t boxchannels; // border channels - uint64_t bgchannels; // base cell channels - const char* base_egc; // base EGC, NULL is interpreted as "" for convenience + uint64_t bgchannels; // background channels, used only in body } selector_options; struct ncselector; diff --git a/doc/man/man3/notcurses_selector.3.md b/doc/man/man3/notcurses_selector.3.md index 88aa4554b..268837218 100644 --- a/doc/man/man3/notcurses_selector.3.md +++ b/doc/man/man3/notcurses_selector.3.md @@ -32,8 +32,7 @@ typedef struct selector_options { uint64_t titlechannels;// title channels uint64_t footchannels; // secondary and footer channels uint64_t boxchannels; // border channels - uint64_t bgchannels; // base cell channels - const char* base_egc; // base EGC or NULL + uint64_t bgchannels; // background channels for body } selector_options; ``` diff --git a/include/notcurses.h b/include/notcurses.h index 4a6178081..952cc8211 100644 --- a/include/notcurses.h +++ b/include/notcurses.h @@ -2157,8 +2157,7 @@ typedef struct selector_options { uint64_t titlechannels;// title channels uint64_t footchannels; // secondary and footer channels uint64_t boxchannels; // border channels - uint64_t bgchannels; // base cell channels - const char* base_egc; // base EGC, NULL is interpreted as "" for convenience + uint64_t bgchannels; // background channels, used only in body } selector_options; struct ncselector; diff --git a/src/lib/internal.h b/src/lib/internal.h index 8dd7e59f3..08e822ee0 100644 --- a/src/lib/internal.h +++ b/src/lib/internal.h @@ -138,6 +138,7 @@ typedef struct ncselector { int secondarycols; // columns occupied by secondary char* footer; // can be NULL int footercols; // columns occupied by footer + cell background; // background, used in body only uint64_t opchannels; // option channels uint64_t descchannels; // description channels uint64_t titlechannels; // title channels diff --git a/src/lib/selector.c b/src/lib/selector.c index 1cb9590b6..c1f23c59b 100644 --- a/src/lib/selector.c +++ b/src/lib/selector.c @@ -58,8 +58,14 @@ ncselector_draw(ncselector* n){ n->ncp->channels = n->footchannels; ncplane_putstr_yx(n->ncp, dimy - 1, xloc, n->footer); } + // Top line of body (background and possibly up arrow) + ++yoff; + ncplane_cursor_move_yx(n->ncp, yoff, xoff + 1); + for(int i = xoff + 1 ; i < dimx - 1 ; ++i){ + ncplane_putc(n->ncp, &n->background); + } n->ncp->channels = n->descchannels; - ncplane_putegc_yx(n->ncp, ++yoff, bodywidth - (n->longdesc + 3) + xoff, "↑", NULL); + ncplane_putegc_yx(n->ncp, yoff, bodywidth - (n->longdesc + 3) + xoff, "↑", NULL); unsigned printidx = n->startdisp; int bodyoffset = dimx - bodywidth + 2; unsigned printed = 0; @@ -67,12 +73,15 @@ ncselector_draw(ncselector* n){ if(n->maxdisplay && printed == n->maxdisplay){ break; } + ncplane_cursor_move_yx(n->ncp, yoff, xoff + 1); + for(int i = xoff + 1 ; i < dimx - 1 ; ++i){ + ncplane_putc(n->ncp, &n->background); + } n->ncp->channels = n->opchannels; if(printidx == n->selected){ n->ncp->channels = (uint64_t)channels_bchannel(n->opchannels) << 32u | channels_fchannel(n->opchannels); } - ncplane_printf_yx(n->ncp, yoff, bodyoffset, "%*.*s", (int)n->longop, - (int)n->longop, n->items[printidx].option); + ncplane_printf_yx(n->ncp, yoff, bodyoffset + (n->longop - n->items[printidx].opcolumns), "%s", n->items[printidx].option); n->ncp->channels = n->descchannels; if(printidx == n->selected){ n->ncp->channels = (uint64_t)channels_bchannel(n->descchannels) << 32u | channels_fchannel(n->descchannels); @@ -83,6 +92,11 @@ ncselector_draw(ncselector* n){ } ++printed; } + // Bottom line of body (background and possibly down arrow) + ncplane_cursor_move_yx(n->ncp, yoff, xoff + 1); + for(int i = xoff + 1 ; i < dimx - 1 ; ++i){ + ncplane_putc(n->ncp, &n->background); + } n->ncp->channels = n->descchannels; ncplane_putegc_yx(n->ncp, yoff, bodywidth - (n->longdesc + 3) + xoff, "↓", NULL); return notcurses_render(n->ncp->nc); @@ -154,11 +168,15 @@ ncselector* ncselector_create(ncplane* n, int y, int x, const selector_options* } for(ns->itemcount = 0 ; ns->itemcount < opts->itemcount ; ++ns->itemcount){ const struct selector_item* src = &opts->items[ns->itemcount]; - if(mbswidth(src->option) > ns->longop){ - ns->longop = mbswidth(src->option); + int cols = mbswidth(src->option); + ns->items[ns->itemcount].opcolumns = cols; + if(cols > ns->longop){ + ns->longop = cols; } - if(mbswidth(src->desc) > ns->longdesc){ - ns->longdesc = mbswidth(src->desc); + cols = mbswidth(src->desc); + ns->items[ns->itemcount].desccolumns = cols; + if(cols > ns->longdesc){ + ns->longdesc = cols; } ns->items[ns->itemcount].option = strdup(src->option); ns->items[ns->itemcount].desc = strdup(src->desc); @@ -175,8 +193,7 @@ ncselector* ncselector_create(ncplane* n, int y, int x, const selector_options* if(!(ns->ncp = ncplane_new(n->nc, dimy, dimx, y, x, NULL))){ goto freeitems; } - if(ncplane_set_base(ns->ncp, opts->bgchannels, 0, - opts->base_egc ? opts->base_egc : "") < 0){ + if(cell_prime(ns->ncp, &ns->background, " ", 0, opts->bgchannels) < 0){ ncplane_destroy(ns->ncp); goto freeitems; } @@ -291,6 +308,7 @@ void ncselector_destroy(ncselector* n, char** item){ free(n->items[n->itemcount].option); free(n->items[n->itemcount].desc); } + cell_release(n->ncp, &n->background); ncplane_destroy(n->ncp); free(n->items); free(n->title);