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

fix prompt user input mechanics

This commit is contained in:
Jfreegman 2013-12-04 03:57:03 -05:00
parent a2af0bc047
commit 424ab7cd10
3 changed files with 43 additions and 42 deletions

View File

@ -476,7 +476,6 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
wprintw(statusbar->topline, "\n"); wprintw(statusbar->topline, "\n");
mvwhline(ctx->linewin, 0, 0, ACS_HLINE, x); mvwhline(ctx->linewin, 0, 0, ACS_HLINE, x);
wrefresh(self->window);
} }
static void chat_onInit(ToxWindow *self, Tox *m) static void chat_onInit(ToxWindow *self, Tox *m)

View File

@ -387,8 +387,6 @@ static void groupchat_onDraw(ToxWindow *self, Tox *m)
groupchats[self->num].peer_names[peer*N+SIDEBAR_WIDTH-2] = '\0'; groupchats[self->num].peer_names[peer*N+SIDEBAR_WIDTH-2] = '\0';
wprintw(ctx->sidebar, "%s\n", &groupchats[self->num].peer_names[peer*N]); wprintw(ctx->sidebar, "%s\n", &groupchats[self->num].peer_names[peer*N]);
} }
wrefresh(self->window);
} }
static void groupchat_onInit(ToxWindow *self, Tox *m) static void groupchat_onInit(ToxWindow *self, Tox *m)

View File

@ -17,9 +17,6 @@
uint8_t pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE] = {0}; uint8_t pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE] = {0};
uint8_t num_frnd_requests = 0; uint8_t num_frnd_requests = 0;
static char prompt_buf[MAX_STR_SIZE] = {'\0'};
static int prompt_buf_pos = 0;
/* Updates own nick in prompt statusbar */ /* Updates own nick in prompt statusbar */
void prompt_update_nick(ToxWindow *prompt, uint8_t *nick, uint16_t len) void prompt_update_nick(ToxWindow *prompt, uint8_t *nick, uint16_t len)
{ {
@ -75,57 +72,59 @@ static int add_friend_request(uint8_t *public_key)
static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key)
{ {
ChatContext *ctx = self->chatwin;
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 (prompt_buf_pos != 0) { if (ctx->pos > 0) {
prompt_buf[--prompt_buf_pos] = '\0'; del_char_buf_bck(ctx->line, &ctx->pos, &ctx->len);
if (x == 0) if (x == 0)
mvwdelch(self->window, y - 1, x2 - 1); wmove(self->window, y - 1, x2 - 1);
else else
mvwdelch(self->window, y, x - 1); wmove(self->window, y, x - 1);
}
} }
} else
#if HAVE_WIDECHAR
if (iswprint(key))
#else
if (isprint(key))
#endif
{
if (ctx->pos < (MAX_STR_SIZE-1)) {
add_char_to_buf(ctx->line, &ctx->pos, &ctx->len, key);
/* Add printable characters to line */ if (x == x2-1) {
else if (isprint(key)) { wprintw(self->window, "\n");
if (prompt_buf_pos < (MAX_STR_SIZE-1)) { wmove(self->window, y, x+2);
mvwaddch(self->window, y, x, key); }
prompt_buf[prompt_buf_pos++] = key; else {
prompt_buf[prompt_buf_pos] = '\0'; wmove(self->window, y, x);
}
} }
} }
/* RETURN key: execute command */ /* RETURN key: execute command */
else if (key == '\n') { else if (key == '\n') {
wprintw(self->window, "\n"); wprintw(self->window, "\n");
execute(self->window, self, m, prompt_buf, GLOBAL_COMMAND_MODE); uint8_t *line = wcs_to_char(ctx->line);
prompt_buf_pos = 0; execute(self->window, self, m, line, GLOBAL_COMMAND_MODE);
prompt_buf[0] = '\0'; reset_buf(ctx->line, &ctx->pos, &ctx->len);
} }
} }
static void prompt_onDraw(ToxWindow *self, Tox *m) static void prompt_onDraw(ToxWindow *self, Tox *m)
{ {
curs_set(1); ChatContext *ctx = self->chatwin;
curs_set(1);
int x, y, x2, y2; int x, y, x2, y2;
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;
for (i = 0; i < prompt_buf_pos; ++i) {
if ((prompt_buf_pos + 3) >= x2)
--y;
}
StatusBar *statusbar = self->stb; StatusBar *statusbar = self->stb;
werase(statusbar->topline); werase(statusbar->topline);
mvwhline(statusbar->topline, 1, 0, ACS_HLINE, x2); mvwhline(statusbar->topline, 1, 0, ACS_HLINE, x2);
@ -169,13 +168,16 @@ static void prompt_onDraw(ToxWindow *self, Tox *m)
wprintw(statusbar->topline, "\n"); wprintw(statusbar->topline, "\n");
if (x > 1) {
wattron(self->window, COLOR_PAIR(GREEN)); wattron(self->window, COLOR_PAIR(GREEN));
mvwprintw(self->window, y, 0, "# "); mvwprintw(self->window, y, 0, "# ");
wattroff(self->window, COLOR_PAIR(GREEN)); wattroff(self->window, COLOR_PAIR(GREEN));
mvwprintw(self->window, y, 2, "%s", prompt_buf); }
wclrtoeol(self->window);
wrefresh(self->window); wclrtobot(self->window);
int ofst = ctx->len <= 0 ? y : y - ((ctx->len + 2 ) / x2);
mvwprintw(self->window, ofst, 2, wcs_to_char(ctx->line));
} }
static void prompt_onInit(ToxWindow *self, Tox *m) static void prompt_onInit(ToxWindow *self, Tox *m)
@ -272,11 +274,13 @@ ToxWindow new_prompt(void)
strcpy(ret.name, "prompt"); strcpy(ret.name, "prompt");
ChatContext *chatwin = calloc(1, sizeof(ChatContext));
StatusBar *stb = calloc(1, sizeof(StatusBar)); StatusBar *stb = calloc(1, sizeof(StatusBar));
if (stb != NULL) if (stb != NULL && chatwin != NULL) {
ret.chatwin = chatwin;
ret.stb = stb; ret.stb = stb;
else { } else {
endwin(); endwin();
fprintf(stderr, "calloc() failed. Aborting...\n"); fprintf(stderr, "calloc() failed. Aborting...\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);