Skip to content

Commit

Permalink
ncdirect: track eof #2521
Browse files Browse the repository at this point in the history
  • Loading branch information
dankamongmen committed Jan 6, 2022
1 parent 15eb313 commit 876aa05
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/lib/direct.c
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,11 @@ int ncdirect_stop(ncdirect* nc){
char* ncdirect_readline(ncdirect* n, const char* prompt){
const char* u7 = get_escape(&n->tcache, ESCAPE_U7);
if(!u7){ // we probably *can*, but it would be a pita; screw it
logerror("can't readline without u7\n");
logerror("can't readline without u7");
return NULL;
}
if(n->eof){
logerror("already got EOF");
return NULL;
}
if(fprintf(n->ttyfp, "%s", prompt) < 0){
Expand Down Expand Up @@ -1005,15 +1009,18 @@ char* ncdirect_readline(ncdirect* n, const char* prompt){
if(ni.evtype == NCTYPE_RELEASE){
continue;
}
if(id == NCKEY_EOF || id == NCKEY_ENTER){
if(id == NCKEY_EOF || id == NCKEY_ENTER || (ncinput_ctrl_p(&ni) && id == 'd')){
if(id == NCKEY_ENTER){
if(fputc('\n', n->ttyfp) < 0){
free(str);
return NULL;
}
}else if(wused == 1){ // NCKEY_EOF without input returns NULL
free(str);
return NULL;
}else{
n->eof = 1;
if(wused == 1){ // NCKEY_EOF without input returns NULL
free(str);
return NULL;
}
}
char* ustr = ncwcsrtombs(str);
free(str);
Expand Down
10 changes: 9 additions & 1 deletion src/lib/in.c
Original file line number Diff line number Diff line change
Expand Up @@ -2476,7 +2476,15 @@ int notcurses_getvec(notcurses* n, const struct timespec* absdl,
}

uint32_t ncdirect_get(ncdirect* n, const struct timespec* absdl, ncinput* ni){
return internal_get(n->tcache.ictx, absdl, ni);
if(n->eof){
logerror("already got EOF");
return -1;
}
uint32_t r = internal_get(n->tcache.ictx, absdl, ni);
if(r == NCKEY_EOF){
n->eof = 1;
}
return r;
}

int get_cursor_location(inputctx* ictx, const char* u7, unsigned* y, unsigned* x){
Expand Down
1 change: 1 addition & 0 deletions src/lib/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ typedef struct ncdirect {
uint16_t stylemask; // current styles
uint64_t flags; // copied in ncdirect_init() from param
ncsharedstats stats; // stats! not as broadly used as in notcurses
unsigned eof; // have we seen EOF on stdin?
} ncdirect;

// Extracellular state for a cell during the render process. There is one
Expand Down

0 comments on commit 876aa05

Please sign in to comment.