[notmuch] [PATCH] notmuch-show.c: provide an option --format=message-ids which outputs only ids.

david at tethera.net david at tethera.net
Thu Dec 17 18:44:54 PST 2009


From: David Bremner <bremner at unb.ca>

If the option --format=message-ids is passed on the command line, only message
IDs are output.  A new structure show_options_t is defined to keep track of
all (currently 2) command line options.
---

It seems a shame to parse the entire output of notmuch show to get a
message id (or all of the message ids) contained in a thread.  One
might like a shorter command line option, but this seems to leave room
for --format=json, and so on.  Similarly, defining an option structure is 
arguably overengineering; I'm happy to rework to just pass a second boolean 
parameter to notmuch_show_messages.  

 notmuch-show.c |   30 ++++++++++++++++++++++--------
 1 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/notmuch-show.c b/notmuch-show.c
index 376aacd..4ed8cec 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -20,6 +20,11 @@
 
 #include "notmuch-client.h"
 
+typedef struct {
+  enum { FORMAT_DEFAULT, FORMAT_MESSAGE_ID } format;
+  int entire_thread;
+} show_options_t;
+    
 static const char *
 _get_tags_as_string (void *ctx, notmuch_message_t *message)
 {
@@ -185,7 +190,7 @@ show_message (void *ctx, notmuch_message_t *message, int indent)
 
 static void
 show_messages (void *ctx, notmuch_messages_t *messages, int indent,
-	       notmuch_bool_t entire_thread)
+	       show_options_t *options)
 {
     notmuch_message_t *message;
     notmuch_bool_t match;
@@ -201,13 +206,17 @@ show_messages (void *ctx, notmuch_messages_t *messages, int indent,
 
 	next_indent = indent;
 
-	if (match || entire_thread) {
-	    show_message (ctx, message, indent);
-	    next_indent = indent + 1;
+	if (match || options->entire_thread) {
+	    if (options->format == FORMAT_DEFAULT) {
+		show_message (ctx, message, indent);
+		next_indent = indent + 1;
+	    } else {
+ 		puts (notmuch_message_get_message_id (message));
+	    } 
 	}
 
 	show_messages (ctx, notmuch_message_get_replies (message),
-		       next_indent, entire_thread);
+		       next_indent, options);
 
 	notmuch_message_destroy (message);
     }
@@ -222,17 +231,22 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
     notmuch_threads_t *threads;
     notmuch_thread_t *thread;
     notmuch_messages_t *messages;
+    show_options_t options;
     char *query_string;
-    int entire_thread = 0;
     int i;
 
+    options.entire_thread = 0;
+    options.format = FORMAT_DEFAULT;
+        
     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
 	if (strcmp (argv[i], "--") == 0) {
 	    i++;
 	    break;
 	}
         if (strcmp(argv[i], "--entire-thread") == 0) {
-	    entire_thread = 1;
+	    options.entire_thread = 1;
+	} else if (strcmp (argv[i], "--format=message-ids") == 0) {
+	    options.format = FORMAT_MESSAGE_ID;
 	} else {
 	    fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
 	    return 1;
@@ -280,7 +294,7 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
 	    INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
 			    notmuch_thread_get_thread_id (thread));
 
-	show_messages (ctx, messages, 0, entire_thread);
+	show_messages (ctx, messages, 0, &options);
 
 	notmuch_thread_destroy (thread);
     }
-- 
1.6.5.3



More information about the notmuch mailing list