src/input.c

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

mercurial