From dfff1e3cc46eab91c67d9190324beda1786f5601 Mon Sep 17 00:00:00 2001 From: Nominate Date: Tue, 6 Aug 2013 11:16:17 +0100 Subject: [PATCH] Corrected wrap-around This should allow wrap-around and allow proper execution. --- prompt.c | 80 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 30 deletions(-) diff --git a/prompt.c b/prompt.c index c832db3..742b35f 100644 --- a/prompt.c +++ b/prompt.c @@ -1,6 +1,6 @@ /* - * Toxic -- Tox Curses Client - */ +* Toxic -- Tox Curses Client +*/ #include #include @@ -41,7 +41,17 @@ unsigned char * hex_string_to_bin(char hex_string[]) static char prompt_buf[256] = {0}; static int prompt_buf_pos=0; -static void execute(ToxWindow* self, char* cmd) { +static void execute(ToxWindow* self, char* u_cmd) { + int i; + int newlines = 0; + char cmd[256] = {0}; + for(i = 0; i < strlen(prompt_buf); i++) + { + if (u_cmd[i] == '\n') + ++newlines; + else + cmd[i - newlines] = u_cmd[i]; + } if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit") || !strcmp(cmd, "q")) { endwin(); @@ -158,7 +168,7 @@ static void execute(ToxWindow* self, char* cmd) { break; } } - else if(!strcmp(cmd, "clear")) { + else if(!strcmp(cmd, "clear")) { wclear(self->window); } else if(!strcmp(cmd, "help")) { @@ -197,7 +207,7 @@ static void execute(ToxWindow* self, char* cmd) { for(i=0; i<32; i++) { char xx[3]; - snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff); + snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff); strcat(id, xx); } @@ -266,8 +276,16 @@ static void execute(ToxWindow* self, char* cmd) { static void prompt_onKey(ToxWindow* self, int key) { // PRINTABLE characters: Add to line. if(isprint(key)) { - if(prompt_buf_pos == (COLS - 3)) { - return; + if (prompt_buf_pos == 255){ + wprintw(self->window, "\nToo Long.\n"); + prompt_buf_pos = 0; + prompt_buf[0] = 0; + } + else if(!(prompt_buf_pos == 0) && (prompt_buf_pos < COLS) && (prompt_buf_pos % (COLS - 3) == 0)) { + prompt_buf[prompt_buf_pos++] = '\n'; + } + else if(!(prompt_buf_pos == 0) && (prompt_buf_pos > COLS) && ((prompt_buf_pos - (COLS - 3)) % (COLS) == 0)) { + prompt_buf[prompt_buf_pos++] = '\n'; } prompt_buf[prompt_buf_pos++] = key; prompt_buf[prompt_buf_pos] = 0; @@ -290,20 +308,22 @@ static void prompt_onKey(ToxWindow* self, int key) { } static void prompt_onDraw(ToxWindow* self) { - curs_set(1); - int x, y; - - getyx(self->window, y, x); - (void) x; - - wattron(self->window, COLOR_PAIR(1)); - mvwprintw(self->window, y, 0, "# "); - wattroff(self->window, COLOR_PAIR(1)); - - mvwprintw(self->window, y, 2, "%s", prompt_buf); - wclrtoeol(self->window); - - wrefresh(self->window); + curs_set(1); + int x, y; + getyx(self->window, y, x); + (void) x; + int i; + for (i = 0; i < (strlen(prompt_buf)); i++) + { + if ((prompt_buf[i] == '\n') && (y != 0)) + --y; + } + wattron(self->window, COLOR_PAIR(1)); + mvwprintw(self->window, y, 0, "# "); + wattroff(self->window, COLOR_PAIR(1)); + mvwprintw(self->window, y, 2, "%s", prompt_buf); + wclrtoeol(self->window); + wrefresh(self->window); } static void print_usage(ToxWindow* self) { @@ -311,15 +331,15 @@ static void print_usage(ToxWindow* self) { wprintw(self->window, "Commands:\n"); wattroff(self->window, A_BOLD); - wprintw(self->window, " connect : Connect to DHT server\n"); - wprintw(self->window, " add : Add friend\n"); - wprintw(self->window, " status : Set your status\n"); - wprintw(self->window, " nick : Set your nickname\n"); - wprintw(self->window, " accept : Accept friend request\n"); - wprintw(self->window, " myid : Print your ID\n"); - wprintw(self->window, " quit/exit : Exit program\n"); - wprintw(self->window, " help : Print this message again\n"); - wprintw(self->window, " clear : Clear this window\n"); + wprintw(self->window, " connect : Connect to DHT server\n"); + wprintw(self->window, " add : Add friend\n"); + wprintw(self->window, " status : Set your status\n"); + wprintw(self->window, " nick : Set your nickname\n"); + wprintw(self->window, " accept : Accept friend request\n"); + wprintw(self->window, " myid : Print your ID\n"); + wprintw(self->window, " quit/exit : Exit program\n"); + wprintw(self->window, " help : Print this message again\n"); + wprintw(self->window, " clear : Clear this window\n"); wattron(self->window, A_BOLD); wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n");