selector: support non-zero default idx #302

pull/312/head
nick black 5 years ago
parent 09d2e35372
commit d30df23f43
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC

@ -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

@ -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;

@ -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);

Loading…
Cancel
Save