diff --git a/include/notcurses.h b/include/notcurses.h index 9c0ccb7fd..f8ac8d40a 100644 --- a/include/notcurses.h +++ b/include/notcurses.h @@ -2123,6 +2123,9 @@ typedef struct selector_options { char* footer; // footer may be NULL struct selector_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 + unsigned defidx; // maximum number of options to display at once, 0 to use all available space unsigned maxdisplay; // exhaustive styling options diff --git a/src/lib/selector.c b/src/lib/selector.c index 7ac62d9c1..9679eb9b2 100644 --- a/src/lib/selector.c +++ b/src/lib/selector.c @@ -119,12 +119,15 @@ ncselector_dim_yx(notcurses* nc, const ncselector* n, int* ncdimy, int* ncdimx){ } ncselector* ncselector_create(ncplane* n, int y, int x, const selector_options* opts){ + if(opts->defidx && opts->defidx >= opts->itemcount){ + return NULL; + } ncselector* ns = malloc(sizeof(*ns)); ns->title = opts->title ? strdup(opts->title) : NULL; ns->secondary = opts->secondary ? strdup(opts->secondary) : NULL; ns->footer = opts->footer ? strdup(opts->footer) : NULL; - ns->selected = 0; - ns->startdisp = 0; + ns->selected = opts->defidx; + ns->startdisp = opts->defidx >= opts->maxdisplay ? opts->defidx - opts->maxdisplay + 1 : 0; ns->longop = 0; ns->maxdisplay = opts->maxdisplay; ns->longdesc = 0; diff --git a/src/poc/selector.c b/src/poc/selector.c index 88c504412..19dd991cc 100644 --- a/src/poc/selector.c +++ b/src/poc/selector.c @@ -35,6 +35,7 @@ int main(void){ sopts.title = "this is 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)"; + sopts.defidx = 5; channels_set_fg(&sopts.boxchannels, 0x20e040); channels_set_fg(&sopts.opchannels, 0xe08040); channels_set_fg(&sopts.descchannels, 0x80e040);