selector/multiselect: kill itemcount options field #831

This commit is contained in:
nick black 2020-07-28 22:42:49 -04:00
parent 83c0a6a33d
commit bac02bccd0
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC
9 changed files with 28 additions and 23 deletions

View File

@ -27,7 +27,6 @@ typedef struct ncmultiselector_options {
char* secondary; // secondary may be NULL
char* footer; // footer may be NULL
struct ncmselector_item* items; // initial items, statuses
unsigned itemcount; // number of initial items
// default item (selected at start)
unsigned defidx;
// maximum number of options to display at once

View File

@ -26,7 +26,6 @@ typedef struct ncselector_options {
char* secondary; // secondary may be NULL
char* footer; // footer may be NULL
struct ncselector_item* items; // initial items and descriptions
unsigned itemcount; // number of initial items and descriptions
// default item (selected at start)
unsigned defidx;
// maximum number of options to display at once

View File

@ -2664,9 +2664,8 @@ typedef struct ncselector_options {
char* secondary; // secondary may be NULL
char* footer; // footer may be NULL
struct ncselector_item* items; // initial items and descriptions
unsigned itemcount; // number of initial items and descriptions
// default item (selected at start), must be < itemcount unless 'itemcount'
// is 0, in which case 'defidx' must also be 0
// default item (selected at start), must be < itemcount unless itemcount is
// 0, in which case 'defidx' must also be 0
unsigned defidx;
// maximum number of options to display at once, 0 to use all available space
unsigned maxdisplay;
@ -2742,7 +2741,6 @@ typedef struct ncmultiselector_options {
char* secondary; // secondary may be NULL
char* footer; // footer may be NULL
struct ncmselector_item* items; // initial items, descriptions, and statuses
unsigned itemcount; // number of items and descriptions, can't be 0
// maximum number of options to display at once, 0 to use all available space
unsigned maxdisplay;
// exhaustive styling options

View File

@ -178,7 +178,13 @@ ncselector_dim_yx(notcurses* nc, const ncselector* n, int* ncdimy, int* ncdimx){
}
ncselector* ncselector_create(ncplane* nc, int y, int x, const ncselector_options* opts){
if(opts->defidx && opts->defidx >= opts->itemcount){
unsigned itemcount = 0;
if(opts->items){
for(const struct ncselector_item* i = opts->items ; i->option ; ++i){
++itemcount;
}
}
if(opts->defidx && opts->defidx >= itemcount){
return NULL;
}
ncselector* ns = malloc(sizeof(*ns));
@ -200,8 +206,8 @@ ncselector* ncselector_create(ncplane* nc, int y, int x, const ncselector_option
ns->footchannels = opts->footchannels;
ns->boxchannels = opts->boxchannels;
ns->darrowy = ns->uarrowy = ns->arrowx = -1;
if(opts->itemcount){
if(!(ns->items = malloc(sizeof(*ns->items) * opts->itemcount))){
if(itemcount){
if(!(ns->items = malloc(sizeof(*ns->items) * itemcount))){
free(ns->title); free(ns->secondary); free(ns->footer);
free(ns);
return NULL;
@ -209,7 +215,7 @@ ncselector* ncselector_create(ncplane* nc, int y, int x, const ncselector_option
}else{
ns->items = NULL;
}
for(ns->itemcount = 0 ; ns->itemcount < opts->itemcount ; ++ns->itemcount){
for(ns->itemcount = 0 ; ns->itemcount < itemcount ; ++ns->itemcount){
const struct ncselector_item* src = &opts->items[ns->itemcount];
int cols = mbswidth(src->option);
ns->items[ns->itemcount].opcolumns = cols;
@ -675,6 +681,12 @@ ncmultiselector_dim_yx(notcurses* nc, const ncmultiselector* n, int* ncdimy, int
ncmultiselector* ncmultiselector_create(ncplane* nc, int y, int x,
const ncmultiselector_options* opts){
unsigned itemcount = 0;
if(opts->items){
for(const struct ncmselector_item* i = opts->items ; i->option ; ++i){
++itemcount;
}
}
ncmultiselector* ns = malloc(sizeof(*ns));
ns->title = opts->title ? strdup(opts->title) : NULL;
ns->titlecols = opts->title ? mbswidth(opts->title) : 0;
@ -693,8 +705,8 @@ ncmultiselector* ncmultiselector_create(ncplane* nc, int y, int x,
ns->footchannels = opts->footchannels;
ns->boxchannels = opts->boxchannels;
ns->darrowy = ns->uarrowy = ns->arrowx = -1;
if(opts->itemcount){
if(!(ns->items = malloc(sizeof(*ns->items) * opts->itemcount))){
if(itemcount){
if(!(ns->items = malloc(sizeof(*ns->items) * itemcount))){
free(ns->title); free(ns->secondary); free(ns->footer);
free(ns);
return NULL;
@ -702,7 +714,7 @@ ncmultiselector* ncmultiselector_create(ncplane* nc, int y, int x,
}else{
ns->items = NULL;
}
for(ns->itemcount = 0 ; ns->itemcount < opts->itemcount ; ++ns->itemcount){
for(ns->itemcount = 0 ; ns->itemcount < itemcount ; ++ns->itemcount){
const struct ncmselector_item* src = &opts->items[ns->itemcount];
int cols = mbswidth(src->option);
if(cols > ns->longitem){

View File

@ -8,7 +8,6 @@ ncmultiselector_options MultiSelector::default_options = {
/* secondary */ nullptr,
/* footer */ nullptr,
/* items */ nullptr,
/* itemcount */ 0,
/* maxdisplay */ 0,
/* opchannels */ 0,
/* descchannels */ 0,

View File

@ -8,7 +8,6 @@ ncselector_options Selector::default_options = {
/* secondary */ nullptr,
/* footer */ nullptr,
/* items */ nullptr,
/* itemcount */ 0,
/* defidx */ 0,
/* maxdisplay */ 0,
/* opchannels */ 0,

View File

@ -18,6 +18,7 @@ static struct ncmselector_item items[] = {
{ "9", "And, eventually noticing the rest of the world was there,", .selected = false, },
{ "10", "Decided to rule it.", .selected = false, },
{ "11", "This is their story.", .selected = false, },
{ NULL, NULL, .selected = false, },
};
static void
@ -66,17 +67,19 @@ int main(void){
memset(&sopts, 0, sizeof(sopts));
sopts.maxdisplay = 10;
sopts.items = items;
sopts.itemcount = sizeof(items) / sizeof(*items);
sopts.title = "this is truly an awfully long example of a MULTISELECTOR title";
sopts.secondary = "pick one (you will die regardless)";
sopts.footer = "press q to exit (there is sartrev(\"no exit\"))";
channels_set_fg(&sopts.boxchannels, 0x20e0e0);
channels_set_bg(&sopts.boxchannels, 0x200000);
channels_set_fg(&sopts.opchannels, 0xe08040);
channels_set_fg(&sopts.descchannels, 0xe0e040);
channels_set_bg(&sopts.opchannels, 0);
channels_set_fg(&sopts.descchannels, 0xe0e040);
channels_set_bg(&sopts.descchannels, 0);
channels_set_fg(&sopts.footchannels, 0xe00040);
channels_set_fg(&sopts.titlechannels, 0x80ffff);
channels_set_bg(&sopts.footchannels, 0x202000);
channels_set_fg(&sopts.titlechannels, 0x20ffff);
channels_set_bg(&sopts.titlechannels, 0x000020);
channels_set_fg(&sopts.bgchannels, 0x002000);
channels_set_bg(&sopts.bgchannels, 0x002000);
channels_set_fg_alpha(&sopts.bgchannels, CELL_ALPHA_BLEND);

View File

@ -17,6 +17,7 @@ static struct ncselector_item items[] = {
SITEM("8 8 8", "the chinese 平仮名平平仮名仮名love me, i'm told"),
SITEM("nine", "nine, nine, nine 'cause you left me"),
SITEM("ten", "stunning and brave"),
SITEM(NULL, NULL),
#undef SITEM
};
@ -66,7 +67,6 @@ int main(void){
memset(&sopts, 0, sizeof(sopts));
sopts.maxdisplay = 4;
sopts.items = items;
sopts.itemcount = sizeof(items) / sizeof(*items);
sopts.title = "this is truly, absolutely an awfully long example of a selector title";
sopts.secondary = "pick one (you will die regardless)";
sopts.footer = "press q to exit (there is no exit)";

View File

@ -79,7 +79,6 @@ TEST_CASE("Selectors") {
};
struct ncselector_options opts{};
opts.items = items;
opts.itemcount = sizeof(items) / sizeof(*items);
struct ncselector* ncs = ncselector_create(n_, 0, 0, &opts);
REQUIRE(nullptr != ncs);
CHECK(0 == notcurses_render(nc_));
@ -116,7 +115,6 @@ TEST_CASE("Selectors") {
};
struct ncselector_options opts{};
opts.items = items;
opts.itemcount = sizeof(items) / sizeof(*items);
struct ncselector* ncs = ncselector_create(n_, 0, 0, &opts);
REQUIRE(nullptr != ncs);
auto sel = ncselector_selected(ncs);
@ -154,7 +152,6 @@ TEST_CASE("Selectors") {
struct ncselector_options opts{};
opts.maxdisplay = 1;
opts.items = items;
opts.itemcount = sizeof(items) / sizeof(*items);
struct ncselector* ncs = ncselector_create(n_, 0, 0, &opts);
REQUIRE(nullptr != ncs);
CHECK(0 == notcurses_render(nc_));
@ -197,7 +194,6 @@ TEST_CASE("Selectors") {
struct ncselector_options opts{};
opts.maxdisplay = 2;
opts.items = items;
opts.itemcount = sizeof(items) / sizeof(*items);
struct ncselector* ncs = ncselector_create(n_, 0, 0, &opts);
REQUIRE(nullptr != ncs);
CHECK(0 == notcurses_render(nc_));