comparison nnchat.c @ 328:a8bda904846c

More work towards a working backbuffer implementation.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 14 Jun 2011 09:02:01 +0300
parents 2f7849e505f3
children c0988ab45afd
comparison
equal deleted inserted replaced
327:fae4651d37bc 328:a8bda904846c
172 } 172 }
173 173
174 return TRUE; 174 return TRUE;
175 } 175 }
176 176
177 typedef struct {
178 size_t len;
179 char *str;
180 } nn_strtuple_t;
181
182 nn_strtuple_t *nn_strtuple_new(size_t len, char *str)
183 {
184 nn_strtuple_t *tuple = th_calloc(1, sizeof(nn_strtuple_t));
185 tuple->len = len;
186 tuple->str = str;
187 return tuple;
188 }
189
190 void nn_strtuple_free(nn_strtuple_t *tuple)
191 {
192 th_free(tuple->str);
193 th_free(tuple);
194 }
195
177 196
178 BOOL getTimeStamp(char *str, size_t len, const char *fmt) 197 BOOL getTimeStamp(char *str, size_t len, const char *fmt)
179 { 198 {
180 time_t stamp = time(NULL); 199 time_t stamp = time(NULL);
181 struct tm *stamp_tm; 200 struct tm *stamp_tm;
323 const char *s = fmt; 342 const char *s = fmt;
324 int col = 0; 343 int col = 0;
325 344
326 while (*s) { 345 while (*s) {
327 if (*s == '½') { 346 if (*s == '½') {
328 int val = 0;
329 s++; 347 s++;
330 if (*s == '½') { 348 if (*s == '½') {
331 waddch(win, ((unsigned char) *s) | col); 349 waddch(win, ((unsigned char) *s) | col);
332 s++; 350 s++;
333 } else { 351 } else {
352 memcpy(&col, s, sizeof(int));
353 s += sizeof(int);
354 }
355 } else {
356 waddch(win, ((unsigned char) *s) | col);
357 s++;
358 }
359 }
360 return 0;
361 }
362
363
364 #define QPUTCH(ch) th_vputch(&(win->buf), &(win->bufsize), &(win->len), ch)
365
366 int nn_window_print(nn_window_t *win, const char *fmt)
367 {
368 const char *s = fmt;
369 int col = 0;
370 while (*s) {
371 if (*s == '½') {
372 s++;
373 if (*s == '½') {
374 QPUTCH(*s);
375 QPUTCH(*s);
376 win->chlen++;
377 } else {
378 int val = 0;
334 while (*s >= '0' && *s <= '9') { 379 while (*s >= '0' && *s <= '9') {
335 val *= 10; 380 val *= 10;
336 val += (*s - '0'); 381 val += (*s - '0');
337 s++; 382 s++;
338 } 383 }
339 if (*s != '½') return -1; 384 if (*s != '½') return -1;
340 s++; 385
341 386 if (val < 9)
342 if (val < 9) {
343 col = A_DIM | COLOR_PAIR(val); 387 col = A_DIM | COLOR_PAIR(val);
344 } else if (val < 30) { 388 else if (val < 30)
345 col = A_BOLD | COLOR_PAIR(val - 9); 389 col = A_BOLD | COLOR_PAIR(val - 9);
346 } 390
391 QPUTCH('½');
392
393 if (!th_growbuf(&(win->buf), &(win->bufsize), &(win->len), sizeof(int)))
394 return -2;
395
396 memcpy(win->buf + win->len, &col, sizeof(int));
397 win->len += sizeof(int);
347 } 398 }
348 } else { 399 } else
349 waddch(win, ((unsigned char) *s) | col);
350 s++;
351 }
352 }
353 return 0;
354 }
355
356
357 void nn_window_print(nn_window_t *win, const char *fmt)
358 {
359 const char *s = fmt;
360 while (*s) {
361 if (*s == '\n') { 400 if (*s == '\n') {
362 th_vputch(&(win->buf), &(win->bufsize), &(win->len), '\n'); 401 QPUTCH('\n');
363 th_vputch(&(win->buf), &(win->bufsize), &(win->len), 0); 402 QPUTCH(0);
403 // th_ringbuf_add(win->data, nn_strtuple_new(win->chlen, win->buf));
364 th_ringbuf_add(win->data, win->buf); 404 th_ringbuf_add(win->data, win->buf);
365 win->buf = NULL; 405 win->buf = NULL;
406 win->chlen = 0;
366 win->dirty = TRUE; 407 win->dirty = TRUE;
367 } 408 }
368 else 409 else
369 if ((unsigned char) *s == 255) 410 if (*s != '\r') {
370 th_vputch(&(win->buf), &(win->bufsize), &(win->len), ' '); 411 QPUTCH((unsigned char) *s == 255 ? ' ' : *s);
371 else 412 win->chlen++;
372 if (*s != '\r') 413 }
373 th_vputch(&(win->buf), &(win->bufsize), &(win->len), *s); 414
374 s++; 415 s++;
375 } 416 }
417
418 return 0;
376 } 419 }
377 420
378 421
379 void updateMainWin(BOOL force) 422 void updateMainWin(BOOL force)
380 { 423 {
1705 if (currWin != NULL) 1748 if (currWin != NULL)
1706 { 1749 {
1707 int oldPos = currWin->pos; 1750 int oldPos = currWin->pos;
1708 1751
1709 if (c == KEY_NPAGE) { 1752 if (c == KEY_NPAGE) {
1710 if (currWin->pos > 10) 1753 if (currWin->pos > -10)
1711 currWin->pos -= 10; 1754 currWin->pos -= 10;
1712 else 1755 else
1713 currWin->pos = 0; 1756 currWin->pos = -10;
1714 } 1757 }
1715 else { 1758 else {
1716 if (currWin->pos < currWin->data->n - 10) 1759 if (currWin->pos < currWin->data->n - 10)
1717 currWin->pos += 10; 1760 currWin->pos += 10;
1718 else 1761 else