src/input.c

changeset 30
a285ee393860
parent 7
41468077b5bb
child 31
ed440bcd9740
     1.1 --- a/src/input.c	Mon Apr 07 14:08:57 2014 +0200
     1.2 +++ b/src/input.c	Mon Apr 07 17:39:46 2014 +0200
     1.3 @@ -28,7 +28,6 @@
     1.4   */
     1.5  
     1.6  #include "input.h"
     1.7 -#include <ncurses.h>
     1.8  
     1.9  void init_colorpairs() {
    1.10      init_pair(COL_YB, COLOR_YELLOW, COLOR_BLUE);
    1.11 @@ -48,3 +47,52 @@
    1.12      
    1.13      return ch == 'y';
    1.14  }
    1.15 +
    1.16 +/**
    1.17 + * Asynchronous variant of getnstr().
    1.18 + * 
    1.19 + * Needs halfdelay mode enabled!
    1.20 + * 
    1.21 + * Warning: you must not call this function for reading into two separate
    1.22 + * buffers at the same time.
    1.23 + * 
    1.24 + * Attention: the first byte of the buffer must be zero at the first call, so
    1.25 + * the buffer pointer is initialized correctly.
    1.26 + * 
    1.27 + * @param w the window
    1.28 + * @param y the window y position
    1.29 + * @param x the window x position
    1.30 + * @param str the buffer for the read string
    1.31 + * @param len the length of the buffer
    1.32 + * @return 0 if reading is in progress and 1 when a complete line is read
    1.33 + */
    1.34 +int mvwasyncgetnstr(WINDOW* w, int y, int x, char *str, size_t len) {
    1.35 +    static size_t pos = 0;
    1.36 +    
    1.37 +    if (*str == '\0') {
    1.38 +        pos = 0;
    1.39 +    }
    1.40 +    
    1.41 +    mvwaddstr(w,y, x, str);
    1.42 +    wrefresh(w);
    1.43 +    int c = wgetch(w);
    1.44 +
    1.45 +    if (c != ERR) {
    1.46 +        switch (c) {
    1.47 +        case '\n':
    1.48 +            str[pos] = '\0';
    1.49 +            pos = 0;
    1.50 +            return 1;
    1.51 +        case KEY_BACKSPACE:
    1.52 +        case KEY_LEFT:
    1.53 +            str[--pos] = '\0';
    1.54 +            break;
    1.55 +        default:
    1.56 +            if (c < 255 && pos < len-1) {
    1.57 +                str[pos++] = (char) c;
    1.58 +            }
    1.59 +        }
    1.60 +    }
    1.61 +    
    1.62 +    return 0;
    1.63 +}

mercurial