diff options
Diffstat (limited to 'src/tui.c')
-rw-r--r-- | src/tui.c | 97 |
1 files changed, 83 insertions, 14 deletions
@@ -15,11 +15,13 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <curses.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> #include "disp.h" #include "calc_data.h" +#include "input.h" static struct calc_data calcs[CALC_COUNT] = {0}; static int current_idx = 0; @@ -29,38 +31,105 @@ void init(void) { win = initscr(); + echo(); + cbreak(); clear(); refresh(); } +static void +draw_res(void) +{ + int j = current_idx; + + while (calcs[j].ms != 0) + { + printw("%d + %d = %d\t%s\t%dms\n", + calcs[j].x, calcs[j].y, + calcs[j].input, + calcs[j].right ? "RIGHT" : "WRONG", + calcs[j].ms); + + j = loop_backwards(j); + } +} + +static void +draw_calc(void) +{ + printw("%d + %d = %d", + calcs[current_idx].x, calcs[current_idx].y, + calcs[current_idx].input); +} + +static void +redraw(void) +{ + clear(); + move(0, 0); + draw_res(); + draw_calc(); + refresh(); +} + void display_calc(int x, int y) { + current_idx = next_calc(current_idx); calcs[current_idx].x = x; calcs[current_idx].y = y; - clear(); - printw("%d + %d = ", x, y); - refresh(); + calcs[current_idx].input = 0; + calcs[current_idx].ms = 0; + redraw(); } +#define case_digit(digit) \ + case digit: \ + calcs[current_idx].input = \ + add_input_digit(calcs[current_idx].input, digit - '0'); \ + break; + int read_input(void) { - char c = getchar(); - - switch(c) + int c = getch(); + while (c != ERR) { + switch(c) + { #ifndef NDEBUG - case 'g': - case 'r': - return DISP_RELOAD; + case 'g': + case 'r': + return DISP_RELOAD; #endif - case 'q': - return DISP_QUIT; - default: - return DISP_ERR; + case 'q': + return DISP_QUIT; + case '\n': + case KEY_ENTER: + printf("ENTER PRESSED\n\r"); + return calcs[current_idx].input; + case KEY_BACKSPACE: + calcs[current_idx].input = + rm_input_digit(calcs[current_idx].input); + break; + case_digit ('0') + case_digit ('1') + case_digit ('2') + case_digit ('3') + case_digit ('4') + case_digit ('5') + case_digit ('6') + case_digit ('7') + case_digit ('8') + case_digit ('9') + default: + return DISP_ERR; + } + + redraw(); + c = getchar(); } - + return DISP_ERR; } |