]> mj.ucw.cz Git - checkmail.git/commitdiff
Refactored processing of snippets
authorMartin Mares <mj@ucw.cz>
Sun, 18 Jul 2010 12:46:17 +0000 (14:46 +0200)
committerMartin Mares <mj@ucw.cz>
Sun, 18 Jul 2010 12:47:22 +0000 (14:47 +0200)
Sender and subject snippets are now stored separately and the full
snippet is constructed from them whenever needed.

cm.c

diff --git a/cm.c b/cm.c
index 2d8c6ce1395d6abb7c3b75ad274875a0ce56b109..bfdcbd89d5bbbaed4fcbec9db19b9b6462a1e7c7 100644 (file)
--- a/cm.c
+++ b/cm.c
@@ -86,7 +86,8 @@ struct mbox {
   int last_beep_new;
   int force_refresh;
   int snippet_is_new;
-  char snippet[256];
+  char sender_snippet[128];
+  char subject_snippet[128];
 };
 
 static clist mboxes;
@@ -252,26 +253,41 @@ mbox_visible_p(struct mbox *b)
 }
 
 static void
-prepare_snippet(struct mbox *b, char *sender, char *subject)
+prepare_snippets(struct mbox *b, char *sender, char *subject)
 {
+  char *pos, *term;
+
   while (*sender == ' ' || *sender == '\t')
     sender++;
   while (*subject == ' ' || *subject == '\t')
     subject++;
 
-  char *pos = b->snippet;
-  char *term = b->snippet + sizeof(b->snippet) - 1;
+  pos = b->sender_snippet;
+  term = pos + sizeof(b->sender_snippet) - 1;
   if (sender[0] && (b->o.sender_mbox || b->o.sender_personal))
-    {
-      add_addr_snippet(&pos, term, sender, b->o.sender_mbox, b->o.sender_personal);
-      add_snippet(&pos, term, ": ");
-    }
+    add_addr_snippet(&pos, term, sender, b->o.sender_mbox, b->o.sender_personal);
+  else
+    *pos = 0;
+
+  pos = b->subject_snippet;
+  term = pos + sizeof(b->subject_snippet) - 1;
   if (subject[0])
     add_subject_snippet(&pos, term, subject);
   else
     add_snippet(&pos, term, "No subject");
 }
 
+static void
+build_snippet(char *buf, char *term, struct mbox *b)
+{
+  if (b->sender_snippet[0])
+    {
+      add_snippet(&buf, term, b->sender_snippet);
+      add_snippet(&buf, term, ": ");
+    }
+  add_snippet(&buf, term, b->subject_snippet);
+}
+
 static int mb_fd, mb_pos;
 static unsigned char mb_buf[4096], *mb_cc, *mb_end;
 
@@ -492,7 +508,7 @@ scan_mbox(struct mbox *b, struct stat *st)
       if (new || (flagged && !b->snippet_is_new))
        {
          b->snippet_is_new = new;
-         prepare_snippet(b, sender, subject);
+         prepare_snippets(b, sender, subject);
        }
 
       int ct = 1;
@@ -835,20 +851,24 @@ redraw_line(int i)
              attrset(attrs[cc][0][M_FLAG]);    /* We avoid the highlight intentionally */
              snip = 1;
            }
-         if (snip && b->o.snippets && b->snippet[0])
+         if (snip && b->o.snippets)
            {
              int xx, yy;
              getyx(stdscr, yy, xx);
              int remains = COLS-1-xx;
-             if (remains > 2)
+
+             char snip[256];
+             build_snippet(snip, snip + sizeof(snip) - 1, b);
+
+             if (snip[0] && remains > 2)
                {
 #ifdef CONFIG_WIDE_CURSES
-                 size_t len = strlen(b->snippet)+1;
-                 wchar_t snip[len];
-                 mbstowcs(snip, b->snippet, len);
-                 addnwstr(snip, remains);
+                 size_t len = strlen(snip)+1;
+                 wchar_t snip2[len];
+                 mbstowcs(snip2, snip, len);
+                 addnwstr(snip2, remains);
 #else
-                 printw("%-.*s", remains, b->snippet);
+                 printw("%-.*s", remains, snip);
 #endif
                }
            }