diff -r c6a1ad6cf749 -r a285ee393860 src/input.c --- a/src/input.c Mon Apr 07 14:08:57 2014 +0200 +++ b/src/input.c Mon Apr 07 17:39:46 2014 +0200 @@ -28,7 +28,6 @@ */ #include "input.h" -#include void init_colorpairs() { init_pair(COL_YB, COLOR_YELLOW, COLOR_BLUE); @@ -48,3 +47,52 @@ return ch == 'y'; } + +/** + * Asynchronous variant of getnstr(). + * + * Needs halfdelay mode enabled! + * + * Warning: you must not call this function for reading into two separate + * buffers at the same time. + * + * Attention: the first byte of the buffer must be zero at the first call, so + * the buffer pointer is initialized correctly. + * + * @param w the window + * @param y the window y position + * @param x the window x position + * @param str the buffer for the read string + * @param len the length of the buffer + * @return 0 if reading is in progress and 1 when a complete line is read + */ +int mvwasyncgetnstr(WINDOW* w, int y, int x, char *str, size_t len) { + static size_t pos = 0; + + if (*str == '\0') { + pos = 0; + } + + mvwaddstr(w,y, x, str); + wrefresh(w); + int c = wgetch(w); + + if (c != ERR) { + switch (c) { + case '\n': + str[pos] = '\0'; + pos = 0; + return 1; + case KEY_BACKSPACE: + case KEY_LEFT: + str[--pos] = '\0'; + break; + default: + if (c < 255 && pos < len-1) { + str[pos++] = (char) c; + } + } + } + + return 0; +}