Constify strings in selector/mselector_items

Use internal types to track items within the
selector/mselector widgets, rather than pressing the
user-provided item structs into double-duty. With this
change, we can constify the strings within those
user-provided items. Do so, also removing the internal-side
elements. Update documentation. Closes #2267.
This commit is contained in:
nick black 2021-10-13 19:19:43 -04:00
parent 1152e2c318
commit 2015e8ffc9
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC
7 changed files with 43 additions and 32 deletions

View File

@ -16,9 +16,8 @@ struct notcurses;
struct ncmultiselector;
struct ncmselector_item {
char* option;
char* desc;
bool selected;
const char* option;
const char* desc;
};
typedef struct ncmultiselector_options {

View File

@ -16,8 +16,8 @@ struct notcurses;
struct ncselector;
struct ncselector_item {
char* option;
char* desc;
const char* option;
const char* desc;
};
typedef struct ncselector_options {

View File

@ -3325,10 +3325,8 @@ API void ncplane_greyscale(struct ncplane* n);
//
// At all times, exactly one item is selected.
struct ncselector_item {
char* option;
char* desc;
size_t opcolumns; // filled in by library
size_t desccolumns; // filled in by library
const char* option;
const char* desc;
};
typedef struct ncselector_options {
@ -3384,8 +3382,8 @@ API bool ncselector_offer_input(struct ncselector* n, const ncinput* nc)
API void ncselector_destroy(struct ncselector* n, char** item);
struct ncmselector_item {
char* option;
char* desc;
const char* option;
const char* desc;
bool selected;
};

View File

@ -27,7 +27,7 @@ draw_background(struct notcurses* nc){
// we list all distributions on which notcurses is known to exist
static struct ncselector_item select_items[] = {
#define SITEM(short, long) { short, long, 0, 0, }
#define SITEM(s, l) { s, l, }
SITEM("fbsd", "FreeBSD"),
SITEM("deb", "Debian Unstable Linux"),
SITEM("rpm", "Fedora Rawhide Linux"),

View File

@ -1,5 +1,19 @@
#include "internal.h"
// internal ncselector item
struct ncselector_int {
char* option;
char* desc;
size_t opcolumns; // filled in by library
size_t desccolumns; // filled in by library
};
struct ncmselector_int {
char* option;
char* desc;
bool selected;
};
typedef struct ncselector {
ncplane* ncp; // backing ncplane
unsigned selected; // index of selection
@ -7,7 +21,7 @@ typedef struct ncselector {
unsigned maxdisplay; // max number of items to display, 0 -> no limit
int longop; // columns occupied by longest option
int longdesc; // columns occupied by longest description
struct ncselector_item* items; // list of items and descriptions, heap-copied
struct ncselector_int* items; // list of items and descriptions, heap-copied
unsigned itemcount; // number of pairs in 'items'
char* title; // can be NULL, in which case there's no riser
int titlecols; // columns occupied by title
@ -29,7 +43,7 @@ typedef struct ncmultiselector {
unsigned startdisp; // index of first option displayed
unsigned maxdisplay; // max number of items to display, 0 -> no limit
int longitem; // columns occupied by longest item
struct ncmselector_item* items; // items, descriptions, and statuses, heap-copied
struct ncmselector_int* items; // items, descriptions, and statuses, heap-copied
unsigned itemcount; // number of pairs in 'items'
char* title; // can be NULL, in which case there's no riser
int titlecols; // columns occupied by title
@ -332,7 +346,7 @@ int ncselector_additem(ncselector* n, const struct ncselector_item* item){
int origdimy, origdimx;
ncselector_dim_yx(n, &origdimy, &origdimx);
size_t newsize = sizeof(*n->items) * (n->itemcount + 1);
struct ncselector_item* items = realloc(n->items, newsize);
struct ncselector_int* items = realloc(n->items, newsize);
if(!items){
return -1;
}

View File

@ -6,7 +6,7 @@
#include "version.h"
static struct ncselector_item items[] = {
#define SITEM(short, long) { short, long, 0, 0, }
#define SITEM(s, l) { s, l, }
SITEM("Afrikaans", "Ek kan glas eet, dit maak my nie seer nie."),
SITEM("AngloSax", "ᛁᚳ᛫ᛗᚨᚷ᛫ᚷᛚᚨᛋ᛫ᛖᚩᛏᚪᚾ᛫ᚩᚾᛞ᛫ᚻᛁᛏ᛫ᚾᛖ᛫ᚻᛖᚪᚱᛗᛁᚪᚧ᛫ᛗᛖ᛬"),
SITEM("Japanese", "私はガラスを食べられます。それは私を傷つけません。"),

View File

@ -109,10 +109,10 @@ TEST_CASE("Selectors") {
SUBCASE("PopulatedSelector") {
ncselector_item items[] = {
{ strdup("op1"), strdup("this is option 1"), 0, 0, },
{ strdup("2ndop"), strdup("this is option #2"), 0, 0, },
{ strdup("tres"), strdup("option the third"), 0, 0, },
{ NULL, NULL, 0, 0, },
{ strdup("op1"), strdup("this is option 1"), },
{ strdup("2ndop"), strdup("this is option #2"), },
{ strdup("tres"), strdup("option the third"), },
{ NULL, NULL, },
};
struct ncselector_options opts{};
opts.items = items;
@ -164,10 +164,10 @@ TEST_CASE("Selectors") {
SUBCASE("SelectorMovement") {
ncselector_item items[] = {
{ strdup("op1"), strdup("this is option 1"), 0, 0, },
{ strdup("2ndop"), strdup("this is option #2"), 0, 0, },
{ strdup("tres"), strdup("option the third"), 0, 0, },
{ NULL, NULL, 0, 0, },
{ strdup("op1"), strdup("this is option 1"), },
{ strdup("2ndop"), strdup("this is option #2"), },
{ strdup("tres"), strdup("option the third"), },
{ NULL, NULL, },
};
struct ncselector_options opts{};
opts.items = items;
@ -210,10 +210,10 @@ TEST_CASE("Selectors") {
// Provide three items, limited to 1 shown at a time
SUBCASE("ScrollingSelectorOne") {
ncselector_item items[] = {
{ strdup("op1"), strdup("this is option 1"), 0, 0, },
{ strdup("2ndop"), strdup("this is option #2"), 0, 0, },
{ strdup("tres"), strdup("option the third"), 0, 0, },
{ NULL, NULL, 0, 0, },
{ strdup("op1"), strdup("this is option 1"), },
{ strdup("2ndop"), strdup("this is option #2"), },
{ strdup("tres"), strdup("option the third"), },
{ NULL, NULL, },
};
struct ncselector_options opts{};
opts.maxdisplay = 1;
@ -262,10 +262,10 @@ TEST_CASE("Selectors") {
// Provide three items, limited to 2 shown at a time
SUBCASE("ScrollingSelectorTwo") {
ncselector_item items[] = {
{ strdup("op1"), strdup("this is option 1"), 0, 0, },
{ strdup("2ndop"), strdup("this is option #2"), 0, 0, },
{ strdup("tres"), strdup("option the third"), 0, 0, },
{ NULL, NULL, 0, 0, },
{ strdup("op1"), strdup("this is option 1"), },
{ strdup("2ndop"), strdup("this is option #2"), },
{ strdup("tres"), strdup("option the third"), },
{ NULL, NULL, },
};
struct ncselector_options opts{};
opts.maxdisplay = 2;