From 215fb2042a4ec7e80ad4faab7a638a4cd6944802 Mon Sep 17 00:00:00 2001 From: wens Date: Tue, 17 Jun 2008 01:52:09 +0000 Subject: Rewrite namecomplete2 using visio API git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@4375 63ad8ddf-47c3-0310-b6dd-a9e9d9715204 --- mbbsd/mail.c | 8 +- mbbsd/name.c | 255 ++++++++++++++++++++++++++++------------------------------- 2 files changed, 124 insertions(+), 139 deletions(-) (limited to 'mbbsd') diff --git a/mbbsd/mail.c b/mbbsd/mail.c index a4b3093f..8dc9ad30 100644 --- a/mbbsd/mail.c +++ b/mbbsd/mail.c @@ -543,7 +543,7 @@ multi_list(struct Vector *namelist, int *recipient) while (1) { vs_hdr("群組寄信名單"); - ShowVector(namelist, 3, 0, msg_cc); + ShowVector(namelist, 3, 0, msg_cc, 0); move(1, 0); outs("(I)引入好友 (O)引入上線通知 (0-9)引入其他特別名單"); getdata(2, 0, @@ -566,7 +566,7 @@ multi_list(struct Vector *namelist, int *recipient) Vector_add(namelist, uid); (*recipient)++; } - ShowVector(namelist, 3, 0, msg_cc); + ShowVector(namelist, 3, 0, msg_cc, 0); } break; case 'd': @@ -577,7 +577,7 @@ multi_list(struct Vector *namelist, int *recipient) break; if (Vector_remove(namelist, uid)) (*recipient)--; - ShowVector(namelist, 3, 0, msg_cc); + ShowVector(namelist, 3, 0, msg_cc, 0); } break; case '0': @@ -650,7 +650,7 @@ multi_send(char *title) } } fclose(fp); - ShowVector(&namelist, 3, 0, msg_cc); + ShowVector(&namelist, 3, 0, msg_cc, 0); } multi_list(&namelist, &recipient); move(1, 0); diff --git a/mbbsd/name.c b/mbbsd/name.c index bf34dc06..cae27f1b 100644 --- a/mbbsd/name.c +++ b/mbbsd/name.c @@ -3,30 +3,6 @@ #define MORE_MSG "按空白鍵可列出更多項目" -void -ShowVector(struct Vector *self, int row, int column, const char *prompt) -{ - int i; - - move(row, column); - clrtobot(); - outs(prompt); - - column = 80; - for (i = 0; i < Vector_length(self); i++) { - const char *p = Vector_get(self, i); - row = strlen(p) + 1; - if (column + row > 76) { - column = row; - outc('\n'); - } else { - column += row; - outc(' '); - } - outs(p); - } -} - void ToggleVector(struct Vector *list, int *recipient, const char *listfile, const char *msg) { @@ -48,137 +24,146 @@ ToggleVector(struct Vector *list, int *recipient, const char *listfile, const ch } } fclose(fp); - ShowVector(list, 3, 0, msg); + ShowVector(list, 3, 0, msg, 0); } } -void -namecomplete2(const struct Vector *namelist, const char *prompt, char *data) +int +ShowVector(struct Vector *list, int row, int col, const char * msg, int idx) { - char *temp; - int x, y, origx, scrx; - int ch; - int count = 0; - int clearbot = NA; - struct Vector sublist; - int viewoffset = 0; + int i, len; + + move(row, col); + clrtobot(); + + if (msg) { + outs(msg); + row++; + } + col = 0; + + len = Vector_MaxLen(list, idx, b_lines - row); + while (len + col < t_columns) { + for (i = row; idx < Vector_length(list) && (i < b_lines); i++) { + move(i, col); + outs(Vector_get(list, idx)); + idx++; + } + col += len + 2; + if (idx == Vector_length(list)) { + idx = 0; + break; + } + len = Vector_MaxLen(list, idx, b_lines - row); + } - Vector_init(&sublist, IDLEN + 1); + return idx; +} - Vector_sublist(namelist, &sublist, ""); - temp = data; +struct namecomplete_int { + const struct Vector * base; + struct Vector sublist; + int idx, dirty; +}; - outs(prompt); - clrtoeol(); - getyx(&y, &x); - scrx = origx = x; - data[count] = 0; - viewoffset = 0; +static int +nc_cb_peek(int key, VGET_RUNTIME *prt, void *instance) +{ + struct namecomplete_int * nc_int = (struct namecomplete_int *) instance; - while (1) - { - // print input field - move(y, scrx); outc(' '); clrtoeol(); move(y, scrx); - outs(ANSI_REVERSE); - prints("%-*s", IDLEN + 1, data); - outs(ANSI_RESET); - move(y, scrx + count); + prt->buf[prt->iend] = 0; - // get input - if ((ch = igetch()) == EOF) { - Vector_delete(&sublist); - break; - } + if (nc_int->dirty < 0) { + Vector_sublist(nc_int->base, &nc_int->sublist, prt->buf); + nc_int->idx = 0; + nc_int->dirty = 0; + } - if (ch == KEY_ENTER) { - *temp = '\0'; - if (Vector_length(&sublist)==1) - strcpy(data, Vector_get(&sublist, 0)); - else if (!Vector_search(&sublist, data)) - data[0] = '\0'; - Vector_delete(&sublist); + switch (key) { + case KEY_ENTER: + if (Vector_length(&nc_int->sublist) == 1) + strlcpy(prt->buf, Vector_get(&nc_int->sublist, 0), prt->len); + else if (!Vector_search(&nc_int->sublist, prt->buf)) + prt->buf[0] = '\0'; + prt->icurr = prt->iend = strlen(prt->buf); break; - } - if (ch == ' ') { - int col, len; - if (Vector_length(&sublist) == 1) { - strcpy(data, Vector_get(&sublist, 0)); - count = strlen(data); - temp = data + count; - continue; + case ' ': + if (Vector_length(&nc_int->sublist) == 1) { + strlcpy(prt->buf, Vector_get(&nc_int->sublist, 0), prt->len); + prt->icurr = prt->iend = strlen(prt->buf); + return VGETCB_NEXT; } - clearbot = YEA; - col = 0; - len = Vector_MaxLen(&sublist, viewoffset, p_lines); + move(2, 0); clrtobot(); printdash("相關資訊一覽表", 0); - while (len + col < t_columns) { - int i; - for (i = p_lines; viewoffset < Vector_length(&sublist) && (i > 0); i--) { - move(3 + (p_lines - i), col); - outs(Vector_get(&sublist, viewoffset)); - viewoffset++; - } - col += len + 2; - if (viewoffset == Vector_length(&sublist)) { - viewoffset = 0; - break; - } - len = Vector_MaxLen(&sublist, viewoffset, p_lines); - } - if (viewoffset < Vector_length(&sublist)) { + nc_int->idx = ShowVector(&nc_int->sublist, 3, 0, NULL, nc_int->idx); + if (nc_int->idx < Vector_length(&nc_int->sublist)) vshowmsg(MORE_MSG); + return VGETCB_NEXT; + break; + + case KEY_BS2: case KEY_BS: /* backspace */ + nc_int->dirty = -1; + break; + + case KEY_HOME: case Ctrl('A'): + case KEY_END: case Ctrl('E'): + case KEY_LEFT: case Ctrl('B'): + case KEY_RIGHT: case Ctrl('F'): + case KEY_DEL: case Ctrl('D'): + case Ctrl('Y'): + case Ctrl('K'): + return VGETCB_NEXT; + break; + + default: + if (isprint(key)) { + struct Vector tmplist; + + prt->buf[prt->iend] = key; + prt->buf[prt->iend + 1] = 0; + + Vector_init(&tmplist, IDLEN + 1); + Vector_sublist(&nc_int->sublist, &tmplist, prt->buf); + + if (Vector_length(&tmplist) == 0) { + Vector_delete(&tmplist); + prt->buf[prt->iend] = 0; + return VGETCB_NEXT; + } else { + Vector_delete(&nc_int->sublist); + nc_int->sublist = tmplist; + nc_int->idx = 0; + prt->buf[prt->iend] = 0; + } } - continue; - } - if (ch == KEY_BS2 || ch == KEY_BS) { /* backspace */ - if (temp == data) - continue; - temp--; - count--; - *temp = '\0'; - Vector_sublist(namelist, &sublist, data); - viewoffset = 0; - continue; - } - if (count < STRLEN && isprint(ch)) { - struct Vector tmplist; - Vector_init(&tmplist, IDLEN + 1); - - *temp++ = ch; - count++; - *temp = '\0'; - - Vector_sublist(&sublist, &tmplist, data); - if (Vector_length(&tmplist)==0) { - Vector_delete(&tmplist); - temp--; - *temp = '\0'; - count--; - continue; - } - Vector_delete(&sublist); - sublist = tmplist; - viewoffset = 0; - } - } - if (ch == EOF) - /* longjmp(byebye, -1); */ - raise(SIGHUP); /* jochang: don't know if this is - * necessary... */ - outc('\n'); - if (clearbot) { - move(2, 0); - clrtobot(); - } - if (*data) { - move(y, origx); - outs(data); - outc('\n'); } + + return VGETCB_NONE; +} + +void +namecomplete2(const struct Vector *namelist, const char *prompt, char *data) +{ + struct namecomplete_int nc_int = { + .base = namelist, + .dirty = 0, + }; + VGET_CALLBACKS vcb = { + .peek = nc_cb_peek, + .data = NULL, + .post = NULL, + }; + + outs(prompt); + clrtoeol(); + Vector_init(&nc_int.sublist, IDLEN+1); + Vector_sublist(namelist, &nc_int.sublist, ""); + vgetstring(data, IDLEN + 1, VGET_ASCII_ONLY, NULL, &vcb, &nc_int); + Vector_delete(&nc_int.sublist); } void -- cgit v1.2.3