changeset 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 fae4651d37bc
children c0988ab45afd
files libnnchat.h nnchat.c
diffstat 2 files changed, 68 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/libnnchat.h	Mon Jun 13 15:08:51 2011 +0300
+++ b/libnnchat.h	Tue Jun 14 09:02:01 2011 +0300
@@ -83,6 +83,7 @@
     
     char *buf;
     size_t len, bufsize;
+    size_t chlen;
 } nn_window_t;
 
 
--- a/nnchat.c	Mon Jun 13 15:08:51 2011 +0300
+++ b/nnchat.c	Tue Jun 14 09:02:01 2011 +0300
@@ -174,6 +174,25 @@
     return TRUE;
 }
 
+typedef struct {
+    size_t len;
+    char *str;
+} nn_strtuple_t;
+
+nn_strtuple_t *nn_strtuple_new(size_t len, char *str)
+{
+    nn_strtuple_t *tuple = th_calloc(1, sizeof(nn_strtuple_t));
+    tuple->len = len;
+    tuple->str = str;
+    return tuple;
+}
+
+void nn_strtuple_free(nn_strtuple_t *tuple)
+{
+    th_free(tuple->str);
+    th_free(tuple);
+}
+
 
 BOOL getTimeStamp(char *str, size_t len, const char *fmt)
 {
@@ -325,25 +344,13 @@
 
     while (*s) {
         if (*s == '½') {
-            int val = 0;
             s++;
             if (*s == '½') {
                 waddch(win, ((unsigned char) *s) | col);
                 s++;
             } else {
-                while (*s >= '0' && *s <= '9') {
-                    val *= 10;
-                    val += (*s - '0');
-                    s++;
-                }
-                if (*s != '½') return -1;
-                s++;
-
-                if (val < 9) {
-                    col = A_DIM | COLOR_PAIR(val);
-                } else if (val < 30) {
-                    col = A_BOLD | COLOR_PAIR(val - 9);
-                }
+                memcpy(&col, s, sizeof(int));
+                s += sizeof(int);
             }
         } else {
             waddch(win, ((unsigned char) *s) | col);
@@ -354,25 +361,61 @@
 }
 
 
-void nn_window_print(nn_window_t *win, const char *fmt)
+#define QPUTCH(ch) th_vputch(&(win->buf), &(win->bufsize), &(win->len), ch)
+
+int nn_window_print(nn_window_t *win, const char *fmt)
 {
     const char *s = fmt;
+    int col = 0;
     while (*s) {
+        if (*s == '½') {
+            s++;
+            if (*s == '½') {
+                QPUTCH(*s);
+                QPUTCH(*s);
+                win->chlen++;
+            } 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);
+
+                QPUTCH('½');
+
+                if (!th_growbuf(&(win->buf), &(win->bufsize), &(win->len), sizeof(int)))
+                    return -2;
+ 
+                memcpy(win->buf + win->len, &col, sizeof(int));
+                win->len += sizeof(int);
+            }
+        } else
         if (*s == '\n') {
-            th_vputch(&(win->buf), &(win->bufsize), &(win->len), '\n');
-            th_vputch(&(win->buf), &(win->bufsize), &(win->len), 0);
+            QPUTCH('\n');
+            QPUTCH(0);
+//            th_ringbuf_add(win->data, nn_strtuple_new(win->chlen, win->buf));
             th_ringbuf_add(win->data, win->buf);
             win->buf = NULL;
+            win->chlen = 0;
             win->dirty = TRUE;
         }
         else    
-        if ((unsigned char) *s == 255)
-            th_vputch(&(win->buf), &(win->bufsize), &(win->len), ' ');
-        else
-        if (*s != '\r')
-            th_vputch(&(win->buf), &(win->bufsize), &(win->len), *s);
+        if (*s != '\r') {
+            QPUTCH((unsigned char) *s == 255 ? ' ' : *s);
+            win->chlen++;
+        }
+
         s++;
     }
+
+    return 0;
 }
 
 
@@ -1707,10 +1750,10 @@
                 int oldPos = currWin->pos;
 
                 if (c == KEY_NPAGE) {
-                    if (currWin->pos > 10)
+                    if (currWin->pos > -10)
                         currWin->pos -= 10;
                     else
-                        currWin->pos = 0;
+                        currWin->pos = -10;
                 }
                 else {
                     if (currWin->pos < currWin->data->n - 10)