1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 01:53:02 +01:00

fixed prompt input

This commit is contained in:
Jfreegman 2013-10-23 03:24:08 -04:00
parent 3d7fb13847
commit b9b3487581
4 changed files with 32 additions and 26 deletions

View File

@ -360,6 +360,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
int x, y, y2, x2; int x, y, y2, x2;
getyx(self->window, y, x); getyx(self->window, y, x);
getmaxyx(self->window, y2, x2); getmaxyx(self->window, y2, x2);
/* BACKSPACE key: Remove one character from line */ /* BACKSPACE key: Remove one character from line */
if (key == 0x107 || key == 0x8 || key == 0x7f) { if (key == 0x107 || key == 0x8 || key == 0x7f) {
if (ctx->pos > 0) { if (ctx->pos > 0) {
@ -377,7 +378,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
#else #else
if (isprint(key)) { if (isprint(key)) {
#endif #endif
if (ctx->pos < (MAX_STR_SIZE-1)) { if (ctx->pos <= MAX_STR_SIZE) {
mvwaddstr(self->window, y, x, wc_to_char(key)); mvwaddstr(self->window, y, x, wc_to_char(key));
ctx->line[ctx->pos++] = key; ctx->line[ctx->pos++] = key;
ctx->line[ctx->pos] = L'\0'; ctx->line[ctx->pos] = L'\0';
@ -390,6 +391,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
wmove(self->window, y2 - CURS_Y_OFFSET, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0);
wclrtobot(self->window); wclrtobot(self->window);
bool close_win = false; bool close_win = false;
if (line[0] == '/') { if (line[0] == '/') {
if (close_win = !strncmp(line, "/close", strlen("/close"))) { if (close_win = !strncmp(line, "/close", strlen("/close"))) {
int f_num = self->num; int f_num = self->num;

View File

@ -367,7 +367,6 @@ void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *cmd)
char args[MAX_NUM_ARGS][MAX_STR_SIZE] = {0}; char args[MAX_NUM_ARGS][MAX_STR_SIZE] = {0};
int num_args = 0; int num_args = 0;
int i = 0;
bool cmd_end = false; // flags when we get to the end of cmd bool cmd_end = false; // flags when we get to the end of cmd
char *end; // points to the end of the current arg char *end; // points to the end of the current arg
@ -377,7 +376,7 @@ void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *cmd)
end = strchr(cmd+1, '\"'); end = strchr(cmd+1, '\"');
if (end++ == NULL) { /* Increment past the end quote */ if (end++ == NULL) { /* Increment past the end quote */
wprintw(window, "Invalid command. Did you forget a closing \"?\n"); wprintw(window, "Invalid argument. Did you forget a closing \"?\n");
return; return;
} }
@ -395,6 +394,8 @@ void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *cmd)
cmd = end; cmd = end;
} }
int i;
/* match input to command list */ /* match input to command list */
for (i = 0; i < NUM_COMMANDS; ++i) { for (i = 0; i < NUM_COMMANDS; ++i) {
if (strcmp(args[0], commands[i].name) == 0) { if (strcmp(args[0], commands[i].name) == 0) {

View File

@ -60,7 +60,7 @@ int string_is_empty(char *string)
uint8_t *wcs_to_char(wchar_t *string) uint8_t *wcs_to_char(wchar_t *string)
{ {
size_t len = 0; size_t len = 0;
char *ret = NULL; uint8_t *ret = NULL;
len = wcstombs(NULL, string, 0); len = wcstombs(NULL, string, 0);
if (len != (size_t) -1) { if (len != (size_t) -1) {

View File

@ -118,22 +118,29 @@ static void print_prompt_help(ToxWindow *self)
static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key)
{ {
/* Add printable characters to line */ int x, y, y2, x2;
if (isprint(key)) { getyx(self->window, y, x);
if (prompt_buf_pos == (sizeof(prompt_buf) - 1)) { getmaxyx(self->window, y2, x2);
return;
} else if (!(prompt_buf_pos == 0) && (prompt_buf_pos < COLS) /* BACKSPACE key: Remove one character from line */
&& (prompt_buf_pos % (COLS - 3) == 0)) { if (key == 0x107 || key == 0x8 || key == 0x7f) {
wprintw(self->window, "\n"); if (prompt_buf_pos != 0) {
prompt_buf[prompt_buf_pos++] = '\n'; prompt_buf[--prompt_buf_pos] = '\0';
} else if (!(prompt_buf_pos == 0) && (prompt_buf_pos > COLS)
&& ((prompt_buf_pos - (COLS - 3)) % (COLS) == 0)) { if (x == 0)
wprintw(self->window, "\n"); mvwdelch(self->window, y - 1, x2 - 1);
prompt_buf[prompt_buf_pos++] = '\n'; else
mvwdelch(self->window, y, x - 1);
}
} }
/* Add printable characters to line */
else if (isprint(key)) {
if (prompt_buf_pos < (MAX_STR_SIZE-1)) {
mvwaddch(self->window, y, x, key);
prompt_buf[prompt_buf_pos++] = key; prompt_buf[prompt_buf_pos++] = key;
prompt_buf[prompt_buf_pos] = 0; prompt_buf[prompt_buf_pos] = '\0';
}
} }
/* RETURN key: execute command */ /* RETURN key: execute command */
@ -146,14 +153,9 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key)
execute(self->window, self, m, prompt_buf); execute(self->window, self, m, prompt_buf);
prompt_buf_pos = 0; prompt_buf_pos = 0;
prompt_buf[0] = 0; prompt_buf[0] = '\0';
} }
/* BACKSPACE key: Remove one character from line */
else if (key == 0x107 || key == 0x8 || key == 0x7f) {
if (prompt_buf_pos != 0)
prompt_buf[--prompt_buf_pos] = 0;
}
} }
static void prompt_onDraw(ToxWindow *self, Tox *m) static void prompt_onDraw(ToxWindow *self, Tox *m)
@ -164,10 +166,11 @@ static void prompt_onDraw(ToxWindow *self, Tox *m)
getyx(self->window, y, x); getyx(self->window, y, x);
getmaxyx(self->window, y2, x2); getmaxyx(self->window, y2, x2);
/* Someone please fix this disgusting hack */
size_t i; size_t i;
for (i = 0; i < prompt_buf_pos; ++i) { for (i = 0; i < prompt_buf_pos; ++i) {
if ((prompt_buf[i] == '\n') && (y != 0)) if ((prompt_buf_pos + 3) >= x2)
--y; --y;
} }