From 13931aee6059cc70b87e9d9143dd1c9d635c395f Mon Sep 17 00:00:00 2001 From: nick black Date: Sat, 23 Nov 2019 20:45:53 -0500 Subject: [PATCH] implement ncplane_getc() #9 --- include/notcurses.h | 13 +++++++++---- src/lib/notcurses.c | 13 +++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/include/notcurses.h b/include/notcurses.h index 1595e2847..3617e5472 100644 --- a/include/notcurses.h +++ b/include/notcurses.h @@ -113,6 +113,11 @@ struct ncplane* notcurses_newplane(struct notcurses* nc, int rows, int cols, // the standard plane. int ncplane_destroy(struct ncplane* n); +// Retrieve the topmost cell at this location on the screen, returning it in +// 'c'. If there is more than a byte of gcluster, it will be returned as a heap +// allocation in '*gclust', and '*c' will be 0x80. +void notcurses_getc(const struct notcurses* n, cell* c, char** gclust); + // Manipulate the opaque user pointer associated with this plane. void ncplane_set_userptr(struct ncplane* n, void* opaque); void* ncplane_userptr(struct ncplane* n); @@ -156,10 +161,10 @@ void ncplane_move_bottom(struct ncplane* n); // will only be used, if 'c->gcluster' has a value >= 0x80. int ncplane_putc(struct ncplane* n, const cell* c, const char* gclust); -// Retrieve the cell under the cursor, returning it in 'c'. If there is more -// than a byte of gcluster, it will be returned as a heap allocation in -// '*gclust', and '*c' will be 0x80. -void ncplane_getc(const struct ncplane* n, cell* c, char** gclust); +// Retrieve the cell under this plane's cursor, returning it in 'c'. If there +// is more than a byte of gcluster, it will be returned as a heap allocation in +// '*gclust', and '*c' will be 0x80. Returns -1 on error, 0 on success. +int ncplane_getc(const struct ncplane* n, cell* c, char** gclust); // Write a series of cells to the current location, using the current style. // They will be interpreted as a series of columns (according to the definition diff --git a/src/lib/notcurses.c b/src/lib/notcurses.c index 49bafa23a..434417d53 100644 --- a/src/lib/notcurses.c +++ b/src/lib/notcurses.c @@ -627,6 +627,19 @@ int ncplane_putc(ncplane* n, const cell* c, const char* gclust){ return ret; } +int ncplane_getc(const ncplane* n, cell* c, char** gclust){ + const cell* src = &n->fb[fbcellidx(n, n->y, n->x)]; + memcpy(c, src, sizeof(*src)); + *gclust = NULL; + if(!simple_cell_p(src)){ + *gclust = strdup(extended_gcluster(n, src)); + if(*gclust == NULL){ + return -1; + } + } + return 0; +} + int cell_load(ncplane* n, cell* c, const char* gcluster){ if(simple_gcluster_p(gcluster)){ c->gcluster = *gcluster;