# HG changeset patch # User Matti Hamalainen # Date 1338656588 -10800 # Node ID bac3f9af112cda094aa2efce09790783e482d19d # Parent 52da406caf54272969dc047615e515f46c96fc3b More work on curses cleanup. Almost working now. diff -r 52da406caf54 -r bac3f9af112c main.c --- a/main.c Fri Jun 01 17:45:38 2012 +0300 +++ b/main.c Sat Jun 02 20:03:08 2012 +0300 @@ -1634,7 +1634,7 @@ if (cursesInit) { int c, cnt = 0; - BOOL update = FALSE, updateMain = FALSE; + BOOL update = FALSE; // Handle several buffered keypresses at once do @@ -1717,7 +1717,7 @@ if (win != NULL) { currWin = win; - update = updateMain = TRUE; + update = TRUE; } c = ERR; } @@ -1735,7 +1735,7 @@ if (win != NULL) { currWin = win; - update = updateMain = TRUE; + update = TRUE; } c = ERR; } @@ -1749,7 +1749,7 @@ erase(); timeout(SET_DELAY); nnwin_reset(); - update = updateMain = TRUE; + update = TRUE; break; #endif @@ -1787,8 +1787,6 @@ // Update time value of last sent message for unidle timeouts prevKeepAlive = time(NULL); } - - updateMain = update = TRUE; } break; @@ -1803,7 +1801,6 @@ histPos++; nn_editbuf_free(editBuf); editBuf = nn_editbuf_copy(histBuf[histPos]); - update = TRUE; } break; @@ -1813,7 +1810,6 @@ histPos--; nn_editbuf_free(editBuf); editBuf = nn_editbuf_copy(histBuf[histPos]); - update = TRUE; } break; @@ -1823,7 +1819,6 @@ editBuf->pos--; while (editBuf->pos > 0 && !isspace((int) editBuf->data[editBuf->pos - 1])) editBuf->pos--; - update = TRUE; editBuf->dirty = TRUE; break; @@ -1833,25 +1828,20 @@ editBuf->pos++; while (editBuf->pos < editBuf->len && !isspace((int) editBuf->data[editBuf->pos])) editBuf->pos++; - update = TRUE; editBuf->dirty = TRUE; break; case KEY_HOME: nn_editbuf_setpos(editBuf, 0); - update = TRUE; break; case KEY_END: nn_editbuf_setpos(editBuf, editBuf->len); - update = TRUE; break; case KEY_LEFT: nn_editbuf_setpos(editBuf, editBuf->pos - 1); - update = TRUE; break; case KEY_RIGHT: nn_editbuf_setpos(editBuf, editBuf->pos + 1); - update = TRUE; break; case KEY_BACKSPACE: @@ -1859,23 +1849,19 @@ case 0x7f: nn_editbuf_delete(editBuf, editBuf->pos - 1); nn_editbuf_setpos(editBuf, editBuf->pos - 1); - update = TRUE; break; case KEY_DC: // Delete character nn_editbuf_delete(editBuf, editBuf->pos); - update = TRUE; break; case KEY_IC: // Ins = Toggle insert / overwrite mode insertMode = !insertMode; - update = TRUE; break; case KEY_F(2): // F2 = Clear editbuffer nn_editbuf_clear(editBuf); - update = TRUE; break; case KEY_F(5): // F5 = Ignore mode @@ -1898,11 +1884,10 @@ case 0x09: // Tab = complete username or command nn_tabcomplete_buffer(editBuf); - update = TRUE; break; case 0x0c: // Ctrl + L - wrefresh(stdscr); + update = TRUE; break; case KEY_NPAGE: @@ -1920,7 +1905,7 @@ currWin->pos = currWin->data->n - 10; if (oldPos != currWin->pos) - updateMain = TRUE; + update = TRUE; } break; @@ -1936,7 +1921,6 @@ else nn_editbuf_write(editBuf, editBuf->pos, c); nn_editbuf_setpos(editBuf, editBuf->pos + 1); - update = TRUE; } else { @@ -1948,7 +1932,7 @@ } while (c != ERR && !exitProg && ++cnt < 10); - nnwin_update(FALSE, editBuf, optUserName, optUserColor); + nnwin_update(update, editBuf, optUserName, optUserColor); } // cursesInit if (++updateCount > 10) diff -r 52da406caf54 -r bac3f9af112c ui.c --- a/ui.c Fri Jun 01 17:45:38 2012 +0300 +++ b/ui.c Sat Jun 02 20:03:08 2012 +0300 @@ -194,6 +194,27 @@ } } +static BOOL nnwin_get_color(char const **s, int *col) +{ + int val = 0; + + while (**s >= '0' && **s <= '9') + { + val *= 10; + val += (**s - '0'); + (*s)++; + } + if (**s != '½') + return FALSE; + + if (val < 9) + *col = A_DIM | COLOR_PAIR(val); + else if (val < 30) + *col = A_BOLD | COLOR_PAIR(val - 9); + + return TRUE; +} + #define QPUTCH(ch) th_vputch(&(win->line->buf), &(win->line->bufsize), &(win->line->len), ch) @@ -222,20 +243,9 @@ } else { - int val = 0; - while (*s >= '0' && *s <= '9') - { - val *= 10; - val += (*s - '0'); - s++; - } - if (*s != '½') return -1; - - if (val < 9) - col = A_DIM | COLOR_PAIR(val); - else if (val < 30) - col = A_BOLD | COLOR_PAIR(val - 9); - + if (!nnwin_get_color(&s, &col)) + return -1; + QPUTCH('½'); if (!th_growbuf(&(win->line->buf), &(win->line->bufsize), &(win->line->len), sizeof(int))) @@ -288,97 +298,129 @@ } -void nnwin_update(BOOL force, nn_editbuf_t *ebuf, char *optUserName, int optUserColor) +static void nnwin_print_str(WINDOW *win, const char *fmt, BOOL clip) { - if (force) + const char *s = fmt; + int col = 0; + while (*s) + { + if (clip && getcurx(win) >= getmaxx(win)) + return; + + if (*s == '½') + { + s++; + if (*s == '½') + { + waddch(win, ((unsigned char) *s) | col); + s++; + } + else + { + if (!nnwin_get_color(&s, &col)) + return; + } + } + else + { + waddch(win, ((unsigned char) *s) | col); + s++; + } + } +} + + +void nnwin_update(BOOL force, nn_editbuf_t *ebuf, char *username, int usercolor) +{ + int sx, sy; + + // Save cursor position + getyx(stdscr, sy, sx); + + // Clear screen if forced or main or editbuf are dirty + if (force || (currWin != NULL && currWin->dirty) || (ebuf != NULL && ebuf->dirty)) { wattrset(stdscr, A_NORMAL); wbkgdset(stdscr, COLOR_PAIR(0)); werase(stdscr); + force = TRUE; } // Check if update is forced or if the window is dirty if (currWin != NULL && (force || currWin->dirty)) { - int h = scrHeight - 5, y, offs; + int y, offs; qringbuf_t *buf = currWin->data; - wattrset(stdscr, A_NORMAL); - wbkgdset(stdscr, COLOR_PAIR(0)); - wmove(stdscr, 0, 0); - - for (y = 0, offs = buf->size - h - currWin->pos; offs >= 0 && offs < buf->size - currWin->pos && offs < buf->size && y < h; offs++) + for (y = scrHeight - 4, offs = buf->size - 1 - currWin->pos; offs >= 0 && y > 0; offs--) { - int col = 0; nn_line_t *line = buf->data[offs]; - if (line == NULL) - continue; - - while (*s && y < h) + if (line != NULL) { - if (*s == '½') + const char *s = line->buf; + int col = 0; + y -= 1 + (line->chlen / scrWidth); + wmove(stdscr, y, 0); + + while (*s) { - s++; if (*s == '½') { + s++; + if (*s == '½') + { + waddch(stdscr, ((unsigned char) *s) | col); + s++; + } + else + { + memcpy(&col, s, sizeof(int)); + s += sizeof(int); + } + } + else + { waddch(stdscr, ((unsigned char) *s) | col); s++; } - else - { - memcpy(&col, s, sizeof(int)); - s += sizeof(int); - } } - else - { - waddch(stdscr, ((unsigned char) *s) | col); - s++; - } - y = getcury(stdscr); } } currWin->dirty = FALSE; } + // Update statusline { - // Statusline - char tmpStr[128]; + char tmpStamp[32], tmpStr[128]; int i; - str_get_timestamp(tmpStr, sizeof(tmpStr), "%H:%M:%S"); + str_get_timestamp(tmpStamp, sizeof(tmpStamp), "%H:%M:%S"); +#if 0 + snprintf(tmpStr, sizeof(tmpStr), + " ½10½%s½13½ | ½16½%s½13½ | ½11½#%06x½13½ | WIN: %d: %s / %d | ½11½", + tmpStamp, + username, + usercolor, + currWin->num + 1, + currWin->id != NULL ? currWin->id : "MAIN", + currWin->pos); +#else + snprintf(tmpStr, sizeof(tmpStr), + " %s | %s | #%06x | WIN: %d: %s / %d | ", + tmpStamp, + username, + usercolor, + currWin->num + 1, + currWin->id != NULL ? currWin->id : "MAIN", + currWin->pos); +#endif + wmove(stdscr, scrHeight - 4, 0); wbkgdset(stdscr, COLOR_PAIR(10)); wclrtoeol(stdscr); - - wattrset(stdscr, A_BOLD | COLOR_PAIR(11)); - mvwaddstr(stdscr, scrHeight - 4, 1, tmpStr); - - wattrset(stdscr, A_BOLD | COLOR_PAIR(13)); - waddstr(stdscr, " | "); - wattrset(stdscr, A_BOLD | COLOR_PAIR(16)); - waddstr(stdscr, optUserName); - wattrset(stdscr, A_BOLD | COLOR_PAIR(13)); - - wattrset(stdscr, A_BOLD | COLOR_PAIR(13)); - waddstr(stdscr, " | "); - wattrset(stdscr, A_BOLD | COLOR_PAIR(11)); - snprintf(tmpStr, sizeof(tmpStr), "#%06x", optUserColor); - waddstr(stdscr, tmpStr); - - wattrset(stdscr, A_BOLD | COLOR_PAIR(13)); - waddstr(stdscr, " | WIN: "); - snprintf(tmpStr, sizeof(tmpStr), "%d: %s / %d", - currWin->num + 1, - currWin->id != NULL ? currWin->id : "MAIN", - currWin->pos); - waddstr(stdscr, tmpStr); - - wattrset(stdscr, A_BOLD | COLOR_PAIR(13)); - waddstr(stdscr, " | "); - wattrset(stdscr, A_BOLD | COLOR_PAIR(11)); + nnwin_print_str(stdscr, tmpStr, TRUE); for (i = 0; i < SET_MAX_WINDOWS; i++) { @@ -389,9 +431,12 @@ } } } + + // Restore cursor position + wmove(stdscr, sy, sx); - if (ebuf != NULL) - // && (force || ebuf->dirty)) + // Update editbuf if needed + if (ebuf != NULL && (force || ebuf->dirty)) { int yoffs = ebuf->pos / scrWidth, xoffs = ebuf->pos % scrWidth;