changeset 75:ff119bb11563

[/trunk] Changeset 89 by mikael * Fix wrapping (prefix) issue.
author mikael
date Sat, 16 Apr 2005 17:14:55 +0000
parents b392112ab995
children a8f8492abd44
files mcabber/src/hbuf.c mcabber/src/hbuf.h mcabber/src/screen.c
diffstat 3 files changed, 42 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/mcabber/src/hbuf.c	Sat Apr 16 16:37:31 2005 +0000
+++ b/mcabber/src/hbuf.c	Sat Apr 16 17:14:55 2005 +0000
@@ -26,6 +26,7 @@
 
 /* This is a private structure type */
 
+#define PREFIX_LENGTH 32
 typedef struct {
   char *ptr;
   char *ptr_end;
@@ -35,19 +36,20 @@
   // (for ex. when HBB_FLAG_PERSISTENT is set).
   struct { // hbuf_line_info
     char *ptr_end_alloc;
-    char prefix[32];
+    char prefix[PREFIX_LENGTH];
   } persist;
 } hbuf_block;
 
 
-//  hbuf_add_line(p_hbuf, text, width)
+//  hbuf_add_line(p_hbuf, text, prefix, width)
 // Add a line to the given buffer.  If width is not null, then lines are
 // wrapped at this length.
 //
 // Note 1: Splitting according to width won't work if there are tabs; they
 //         should be expanded before.
 // Note 2: width does not include the ending \0.
-void hbuf_add_line(GList **p_hbuf, char *text, unsigned int width)
+void hbuf_add_line(GList **p_hbuf, const char *text, const char *prefix,
+        unsigned int width)
 {
   GList *hbuf = *p_hbuf;
   char *line, *cr, *end;
@@ -55,6 +57,8 @@
   if (!text) return;
 
   hbuf_block *hbuf_block_elt = g_new0(hbuf_block, 1);
+  if (prefix)
+    strncpy(hbuf_block_elt->persist.prefix, prefix, PREFIX_LENGTH-1);
   if (!hbuf) {
     hbuf_block_elt->ptr    = g_new(char, HBB_BLOCKSIZE);
     hbuf_block_elt->flags  = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT;
@@ -216,14 +220,15 @@
 }
 
 //  hbuf_get_lines(hbuf, n, where)
-// Returns an array of n pointers (for n lines from hbuf)
+// Returns an array of 2*n pointers (for n prefixes + n lines from hbuf)
+// (prefix line 1, line 1, prefix line 2, line 2, etc.)
 // (The first line will be the line currently pointed by hbuf)
 // Note:The caller should free the array after use.
 char **hbuf_get_lines(GList *hbuf, unsigned int n)
 {
   unsigned int i;
 
-  char **array = g_new0(char*, n);
+  char **array = g_new0(char*, n*2);
   char **array_elt = array;
 
   for (i=0 ; i < n ; i++) {
@@ -231,6 +236,7 @@
       hbuf_block *blk = (hbuf_block*)(hbuf->data);
       int maxlen;
       maxlen = blk->ptr_end - blk->ptr;
+      *array_elt++ = blk->persist.prefix;
       *array_elt++ = g_strndup(blk->ptr, maxlen);
       hbuf = g_list_next(hbuf);
     } else
--- a/mcabber/src/hbuf.h	Sat Apr 16 16:37:31 2005 +0000
+++ b/mcabber/src/hbuf.h	Sat Apr 16 17:14:55 2005 +0000
@@ -12,7 +12,8 @@
 #define HBB_FLAG_PERSISTENT 2
 // #define HBB_FLAG_FREE       4
 
-void hbuf_add_line(GList **p_hbuf, char *text, unsigned int width);
+void hbuf_add_line(GList **p_hbuf, const char *text, const char *prefix,
+        unsigned int width);
 void hbuf_free(GList **p_hbuf);
 void hbuf_rebuild(GList **p_hbuf, unsigned int width);
 
--- a/mcabber/src/screen.c	Sat Apr 16 16:37:31 2005 +0000
+++ b/mcabber/src/screen.c	Sat Apr 16 17:14:55 2005 +0000
@@ -246,11 +246,17 @@
   wmove(win_entry->win, 0, 0);
   for (n = 0; n < CHAT_WIN_HEIGHT; n++) {
     int r = width;
-    if (*(lines+n)) {
-      wprintw(win_entry->win, "%s", *(lines+n));
-      r -= strlen(*(lines+n));
-    }// else
-    //  wmove(win_entry->win, n, 0);
+    if (*(lines+2*n)) {
+      if (**(lines+2*n))
+        wprintw(win_entry->win, "%s", *(lines+2*n));      // prefix
+      else {
+        wprintw(win_entry->win, "            ");
+        r -= 12;
+      }
+      wprintw(win_entry->win, "%s", *(lines+2*n+1));      // line
+      // Calculate the number of blank characters to empty the line
+      r -= strlen(*(lines+2*n)) + strlen(*(lines+2*n+1));
+    }
     for ( ; r>0 ; r--) {
       wprintw(win_entry->win, " ");
     }
@@ -291,25 +297,25 @@
 }
 
 
-void scr_WriteInWindow(const char *winId, char *text, int TimeStamp,
-        int force_show)
+void scr_WriteInWindow(const char *winId, const char *text, int TimeStamp,
+        const char *prefix, int force_show)
 {
-  char *line;
+  char *fullprefix = NULL;
   window_entry_t *win_entry;
   int dont_show = FALSE;
 
-  line = calloc(1, strlen(text)+16);
-
-  // Prepare line (timestamp + text)
-  // FIXME: actually timestamp and text should not be merged, there is a prefix
-  //        field in the hbuf_block structure just for that.
-  if (TimeStamp) {
-    time_t now = time(NULL);
-    strftime(line, 12, "[%H:%M] ", localtime(&now));
-  } else {
-    strcpy(line, "            ");
+  // Prepare the prefix
+  if (prefix || TimeStamp) {
+    if (!prefix)  prefix = "";
+    fullprefix = calloc(1, strlen(prefix)+16);
+    if (TimeStamp) {
+      time_t now = time(NULL);
+      strftime(fullprefix, 12, "[%H:%M] ", localtime(&now));
+    } else {
+      strcpy(fullprefix, "            ");
+    }
+    strcat(fullprefix, prefix);
   }
-  strcat(line, text);
 
   // Look for the window entry.
   win_entry = scr_SearchWindow(winId);
@@ -326,9 +332,9 @@
                           maxX - ROSTER_WIDTH, dont_show);
   }
 
-  hbuf_add_line(&win_entry->hbuf, line,
+  hbuf_add_line(&win_entry->hbuf, text, fullprefix,
                 maxX - scr_WindowWidth(rosterWnd) - 14);
-  free(line);
+  free(fullprefix);
 
   if (!dont_show) {
     // Show and refresh the window
@@ -407,28 +413,15 @@
   return;
 }
 
-// XXX This function is almost useless now.  Once we handle properly
-// the prefix in scr_WriteInWindow(), we can remove it...
 void scr_WriteMessage(const char *jid, const char *text, char *prefix)
 {
-  char *buffer = (char *) malloc(strlen(prefix) + strlen(text) + 1);
-
-  if (prefix)
-    strcpy(buffer, prefix);
-  else
-    *buffer = 0;
-
-  strcat(buffer, text);
-
-  scr_WriteInWindow(jid, buffer, TRUE, FALSE);
-
-  free(buffer);
+  scr_WriteInWindow(jid, text, TRUE, prefix, FALSE);
 }
 
 void scr_WriteIncomingMessage(const char *jidfrom, const char *text)
 {
   char *buffer = utf8_decode(text);
-  // FIXME expand tabs...
+  // FIXME expand tabs / filter out special chars...
   scr_WriteMessage(jidfrom, buffer, "<== ");
   free(buffer);
   top_panel(inputPanel);
@@ -441,7 +434,6 @@
   scr_WriteMessage(jidto, text, "--> ");
   scr_ShowWindow(jidto);
   top_panel(inputPanel);
-  //refresh(); // XXX ?
 }
 
 int scr_Getch(void)