ncreader_contents(): wrap ncplane_contents() #403

pull/592/head
nick black 4 years ago committed by Nick Black
parent f0357ef6cb
commit c4bf483d8b

@ -149,7 +149,8 @@ typedef struct ncsubproc {
typedef struct ncreader {
ncplane* ncp; // always owned by ncreader
char* contents; // UTF-8 encoded, never NULL
uint64_t tchannels; // channels for input text
uint32_t tattrs; // attributes for input text
} ncreader;
typedef struct ncmenu {

@ -2025,3 +2025,28 @@ uint32_t* ncplane_rgba(const ncplane* nc, int begy, int begx, int leny, int lenx
}
return ret;
}
char* ncplane_contents(const ncplane* nc, int begy, int begx, int leny, int lenx){
if(begy < 0 || begx < 0){
return NULL;
}
if(begx >= nc->lenx || begy >= nc->leny){
return NULL;
}
if(lenx == -1){ // -1 means "to the end"; use all space available
lenx = nc->lenx - begx;
}
if(leny == -1){
leny = nc->leny - begy;
}
if(lenx < 0 || leny < 0){ // no need to draw zero-size object, exit
return NULL;
}
if(begx + lenx > nc->lenx || begy + leny > nc->leny){
return NULL;
}
char* ret = malloc(lenx * leny + 1); // allow a bit of oversize
// FIXME traverse that fucker
ret[lenx * leny] = '\0';
return ret;
}

@ -4,6 +4,9 @@ ncreader* ncreader_create(notcurses* nc, int y, int x, const ncreader_options* o
if(opts->physrows <= 0 || opts->physcols <= 0){
return NULL;
}
if(opts->scroll){
return NULL; // FIXME not yet handled
}
ncreader* nr = malloc(sizeof(*nr));
if(nr){
nr->ncp = ncplane_new(nc, opts->physrows, opts->physcols, y, x, NULL);
@ -16,15 +19,14 @@ ncreader* ncreader_create(notcurses* nc, int y, int x, const ncreader_options* o
ncreader_destroy(nr, NULL);
return NULL;
}
nr->contents = strdup("");
// FIXME
nr->tchannels = opts->tchannels;
nr->tattrs = opts->tattrword;
}
return nr;
}
// empty the ncreader of any user input, and home the cursor.
int ncreader_clear(ncreader* n){
free(n->contents);
ncplane_erase(n->ncp);
return ncplane_cursor_move_yx(n->ncp, 0, 0);
}
@ -37,20 +39,28 @@ bool ncreader_offer_input(ncreader* n, const ncinput* ni){
if(nckey_supppuab_p(ni->id)){
return false;
}
// FIXME add ni to n->content
// FIXME handle backspace
// FIXME need to collect full EGCs
char wbuf[WCHAR_MAX_UTF8BYTES + 1];
// FIXME breaks for wchar_t < 32bits
if(snprintf(wbuf, sizeof(wbuf), "%lc", (wchar_t)ni->id) >= (int)sizeof(wbuf)){
return true;
}
if(ncplane_putegc(n->ncp, wbuf, NULL) < 0){
return true;
}
return true;
}
char* ncreader_contents(const ncreader* n){
return strdup(n->contents);
return ncplane_contents(n->ncp, 0, 0, -1, -1);
}
void ncreader_destroy(ncreader* n, char** contents){
if(n){
if(contents){
*contents = strdup(n->contents);
*contents = ncreader_contents(n);
}
free(n->contents);
ncplane_destroy(n->ncp);
free(n);
}

Loading…
Cancel
Save