]> git.notmuchmail.org Git - notmuch/commitdiff
notmuch search: Add (relative) date to search output
authorCarl Worth <cworth@cworth.org>
Fri, 30 Oct 2009 00:31:07 +0000 (17:31 -0700)
committerCarl Worth <cworth@cworth.org>
Fri, 30 Oct 2009 00:31:07 +0000 (17:31 -0700)
The new function for formatting relative dates is nice enough that
we need to start using it more places. Here's one of them.

notmuch-private.h
notmuch.c
notmuch.h
query.cc
thread.cc

index b67aa9051b85eea1991a46da38089ecabba0096b..99fbb4b2eb1b2e6c5d736e937cf5c3f5309aafe9 100644 (file)
@@ -155,10 +155,8 @@ _notmuch_thread_create (const void *talloc_owner,
                        const char *thread_id);
 
 void
-_notmuch_thread_add_tag (notmuch_thread_t *thread, const char *tag);
-
-void
-_notmuch_thread_set_subject (notmuch_thread_t *thread, const char *subject);
+_notmuch_thread_add_message (notmuch_thread_t *thread,
+                            notmuch_message_t *message);
 
 /* message.cc */
 
index c6807e832af2fef868e346630321bdd3e8d94aaa..ba5ba831a01745e5fc13df64edce7d9fb9f20909 100644 (file)
--- a/notmuch.c
+++ b/notmuch.c
@@ -733,70 +733,6 @@ query_string_from_args (void *ctx, int argc, char *argv[])
     return query_string;
 }
 
-static int
-search_command (int argc, char *argv[])
-{
-    void *local = talloc_new (NULL);
-    notmuch_database_t *notmuch = NULL;
-    notmuch_query_t *query;
-    notmuch_thread_results_t *results;
-    notmuch_thread_t *thread;
-    notmuch_tags_t *tags;
-    char *query_str;
-    notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
-
-    notmuch = notmuch_database_open (NULL);
-    if (notmuch == NULL) {
-       ret = 1;
-       goto DONE;
-    }
-
-    query_str = query_string_from_args (local, argc, argv);
-
-    query = notmuch_query_create (notmuch, query_str);
-    if (query == NULL) {
-       fprintf (stderr, "Out of memory\n");
-       ret = 1;
-       goto DONE;
-    }
-
-    for (results = notmuch_query_search_threads (query);
-        notmuch_thread_results_has_more (results);
-        notmuch_thread_results_advance (results))
-    {
-       int first = 1;
-
-       thread = notmuch_thread_results_get (results);
-
-       printf ("%s %s",
-               notmuch_thread_get_thread_id (thread),
-               notmuch_thread_get_subject (thread));
-
-       printf (" (");
-       for (tags = notmuch_thread_get_tags (thread);
-            notmuch_tags_has_more (tags);
-            notmuch_tags_advance (tags))
-       {
-           if (! first)
-               printf (" ");
-           printf ("%s", notmuch_tags_get (tags));
-           first = 0;
-       }
-       printf (")\n");
-
-       notmuch_thread_destroy (thread);
-    }
-
-    notmuch_query_destroy (query);
-
-  DONE:
-    if (notmuch)
-       notmuch_database_close (notmuch);
-    talloc_free (local);
-
-    return ret;
-}
-
 /* Format a nice representation of 'time' relative to the current time.
  *
  * Examples include:
@@ -868,6 +804,76 @@ _format_relative_date (void *ctx, time_t then)
 #undef HOUR
 #undef DAY
 
+static int
+search_command (int argc, char *argv[])
+{
+    void *local = talloc_new (NULL);
+    notmuch_database_t *notmuch = NULL;
+    notmuch_query_t *query;
+    notmuch_thread_results_t *results;
+    notmuch_thread_t *thread;
+    notmuch_tags_t *tags;
+    char *query_str;
+    const char *relative_date;
+    time_t date;
+    notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
+
+    notmuch = notmuch_database_open (NULL);
+    if (notmuch == NULL) {
+       ret = 1;
+       goto DONE;
+    }
+
+    query_str = query_string_from_args (local, argc, argv);
+
+    query = notmuch_query_create (notmuch, query_str);
+    if (query == NULL) {
+       fprintf (stderr, "Out of memory\n");
+       ret = 1;
+       goto DONE;
+    }
+
+    for (results = notmuch_query_search_threads (query);
+        notmuch_thread_results_has_more (results);
+        notmuch_thread_results_advance (results))
+    {
+       int first = 1;
+
+       thread = notmuch_thread_results_get (results);
+
+       date = notmuch_thread_get_oldest_date (thread);
+       relative_date = _format_relative_date (local, date);
+
+       printf ("%s (%s) %s",
+               notmuch_thread_get_thread_id (thread),
+               relative_date,
+               notmuch_thread_get_subject (thread));
+
+       printf (" (");
+       for (tags = notmuch_thread_get_tags (thread);
+            notmuch_tags_has_more (tags);
+            notmuch_tags_advance (tags))
+       {
+           if (! first)
+               printf (" ");
+           printf ("%s", notmuch_tags_get (tags));
+           first = 0;
+       }
+       printf (")\n");
+
+       notmuch_thread_destroy (thread);
+    }
+
+    notmuch_query_destroy (query);
+
+  DONE:
+    if (notmuch)
+       notmuch_database_close (notmuch);
+    talloc_free (local);
+
+    return ret;
+}
+
 /* Get a nice, single-line summary of message. */
 static const char *
 _get_one_line_summary (void *ctx, notmuch_message_t *message)
index 3f59709a579542db6eeaa1d00baa7022eff9396f..a3608ba0dfcc03e0e8701192c57d4eab7199c165 100644 (file)
--- a/notmuch.h
+++ b/notmuch.h
@@ -488,6 +488,16 @@ notmuch_thread_get_thread_id (notmuch_thread_t *thread);
 const char *
 notmuch_thread_get_subject (notmuch_thread_t *thread);
 
+/* Get the date of the oldest message in 'thread' as a time_t value.
+ */
+time_t
+notmuch_thread_get_oldest_date (notmuch_thread_t *thread);
+
+/* Get the date of the oldest message in 'thread' as a time_t value.
+ */
+time_t
+notmuch_thread_get_newest_date (notmuch_thread_t *thread);
+
 /* Get the tags for 'thread', returning a notmuch_tags_t object which
  * can be used to iterate over all tags.
  *
index 170ab7523957de7ba317ea86b311281e4be76981..2fe3965f64549f3beb1fe42b6f585f492c82a09d 100644 (file)
--- a/query.cc
+++ b/query.cc
@@ -178,8 +178,6 @@ notmuch_query_search_threads (notmuch_query_t *query)
     const char *thread_id;
     notmuch_message_results_t *message_results;
     notmuch_message_t *message;
-    notmuch_tags_t *tags;
-    const char *tag;
     GHashTable *seen;
 
     thread_results = talloc (query, notmuch_thread_results_t);
@@ -207,27 +205,15 @@ notmuch_query_search_threads (notmuch_query_t *query)
                                            thread_id, NULL,
                                            (void **) &thread))
        {
-           const char *subject;
-
            thread = _notmuch_thread_create (query, query->notmuch,
                                             thread_id);
 
-           subject = notmuch_message_get_header (message, "subject");
-
-           _notmuch_thread_set_subject (thread, subject);
-
            g_hash_table_insert (seen, xstrdup (thread_id), thread);
 
            g_ptr_array_add (thread_results->threads, thread);
        }
 
-       for (tags = notmuch_message_get_tags (message);
-            notmuch_tags_has_more (tags);
-            notmuch_tags_advance (tags))
-       {
-           tag = notmuch_tags_get (tags);
-           _notmuch_thread_add_tag (thread, tag);
-       }
+       _notmuch_thread_add_message (thread, message);
 
        notmuch_message_destroy (message);
     }
index e4c9e111a7c4af21901a6cadbf0df4543ce86697..b67dfade5e16091fde964ddbafd69e1da6b5eb01 100644 (file)
--- a/thread.cc
+++ b/thread.cc
@@ -30,6 +30,10 @@ struct _notmuch_thread {
     char *thread_id;
     char *subject;
     GHashTable *tags;
+
+    notmuch_bool_t has_message;
+    time_t oldest;
+    time_t newest;
 };
 
 static int
@@ -74,6 +78,10 @@ _notmuch_thread_create (const void *talloc_owner,
     thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
                                          free, NULL);
 
+    thread->has_message = 0;
+    thread->oldest = 0;
+    thread->newest = 0;
+
     return thread;
 }
 
@@ -84,15 +92,36 @@ notmuch_thread_get_thread_id (notmuch_thread_t *thread)
 }
 
 void
-_notmuch_thread_add_tag (notmuch_thread_t *thread, const char *tag)
+_notmuch_thread_add_message (notmuch_thread_t *thread,
+                            notmuch_message_t *message)
 {
-    g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
-}
+    notmuch_tags_t *tags;
+    const char *tag;
+    time_t date;
 
-void
-_notmuch_thread_set_subject (notmuch_thread_t *thread, const char *subject)
-{
-    thread->subject = talloc_strdup (thread, subject);
+    if (! thread->subject) {
+       const char *subject;
+       subject = notmuch_message_get_header (message, "subject");
+       thread->subject = talloc_strdup (thread, subject);
+    }
+
+    for (tags = notmuch_message_get_tags (message);
+        notmuch_tags_has_more (tags);
+        notmuch_tags_advance (tags))
+    {
+       tag = notmuch_tags_get (tags);
+       g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
+    }
+
+    date = notmuch_message_get_date (message);
+
+    if (date < thread->oldest || ! thread->has_message)
+       thread->oldest = date;
+
+    if (date > thread->newest || ! thread->has_message)
+       thread->newest = date;
+
+    thread->has_message = 1;
 }
 
 const char *
@@ -101,6 +130,18 @@ notmuch_thread_get_subject (notmuch_thread_t *thread)
     return thread->subject;
 }
 
+time_t
+notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
+{
+    return thread->oldest;
+}
+
+time_t
+notmuch_thread_get_newest_date (notmuch_thread_t *thread)
+{
+    return thread->newest;
+}
+
 notmuch_tags_t *
 notmuch_thread_get_tags (notmuch_thread_t *thread)
 {