[PATCH 3/3] notmuch-show.c: control which headers are show for json output.

david at tethera.net david at tethera.net
Sat Apr 24 04:44:38 PDT 2010


From: David Bremner <bremner at unb.ca>

This commit adds argument handling from and callbacks to
notmuch-output.c to determine what headers to show in json output.
This requires passing a pointer to a struct containing the output
parameters into several functions, and in particular changes the type of
the function pointers in the show_format structure.
---
 notmuch-show.c |   81 ++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 55 insertions(+), 26 deletions(-)

diff --git a/notmuch-show.c b/notmuch-show.c
index 6aa9072..29e2819 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -19,6 +19,7 @@
  */
 
 #include "notmuch-client.h"
+#include "notmuch-output.h"
 
 typedef struct show_format {
     const char *message_set_start;
@@ -28,6 +29,7 @@ typedef struct show_format {
 		     int indent);
     const char *header_start;
     void (*header) (const void *ctx,
+		    notmuch_output_options_t *output_opts,
 		    notmuch_message_t *message);
     const char *header_end;
     const char *body_start;
@@ -45,6 +47,7 @@ format_message_text (unused (const void *ctx),
 		     int indent);
 static void
 format_headers_text (const void *ctx,
+		     notmuch_output_options_t *opts,
 		     notmuch_message_t *message);
 static void
 format_part_text (GMimeObject *part,
@@ -64,6 +67,7 @@ format_message_json (const void *ctx,
 		     unused (int indent));
 static void
 format_headers_json (const void *ctx,
+		     unused (notmuch_output_options_t *opts),
 		     notmuch_message_t *message);
 static void
 format_part_json (GMimeObject *part,
@@ -164,7 +168,9 @@ format_message_json (const void *ctx, notmuch_message_t *message, unused (int in
 }
 
 static void
-format_headers_text (const void *ctx, notmuch_message_t *message)
+format_headers_text (const void *ctx,
+		     unused (notmuch_output_options_t *opts),
+		     notmuch_message_t *message)
 {
     const char *headers[] = {
 	"Subject", "From", "To", "Cc", "Bcc", "Date"
@@ -183,28 +189,38 @@ format_headers_text (const void *ctx, notmuch_message_t *message)
 }
 
 static void
-format_headers_json (const void *ctx, notmuch_message_t *message)
+format_headers_json (const void *ctx,
+		     notmuch_output_options_t *output_opts,
+		     notmuch_message_t *message)
 {
-    const char *headers[] = {
-	"Subject", "From", "To", "Cc", "Bcc", "Date"
+    const struct { const char *name; notmuch_output_t key; } headers[] = {
+	{ "Subject", NOTMUCH_OUTPUT_SUBJECT},
+	{ "From", NOTMUCH_OUTPUT_FROM},
+	{ "To", NOTMUCH_OUTPUT_TO},
+	{ "Cc", NOTMUCH_OUTPUT_TO},
+	{ "Bcc", NOTMUCH_OUTPUT_BCC },
+	{ "Date", 	NOTMUCH_OUTPUT_DATE}
     };
+
     const char *name, *value;
     unsigned int i;
     int first_header = 1;
     void *ctx_quote = talloc_new (ctx);
 
     for (i = 0; i < ARRAY_SIZE (headers); i++) {
-	name = headers[i];
-	value = notmuch_message_get_header (message, name);
-	if (value)
-	{
-	    if (!first_header)
-		fputs (", ", stdout);
-	    first_header = 0;
-
-	    printf ("%s: %s",
-		    json_quote_str (ctx_quote, name),
-		    json_quote_str (ctx_quote, value));
+	if (output_get_flag(output_opts,headers[i].key)) {
+	    name = headers[i].name;
+	    value = notmuch_message_get_header (message, name);
+	    if (value)
+	    {
+		if (!first_header)
+		    fputs (", ", stdout);
+		first_header = 0;
+
+		printf ("%s: %s",
+			json_quote_str (ctx_quote, name),
+			json_quote_str (ctx_quote, value));
+	    }
 	}
     }
 
@@ -337,7 +353,9 @@ format_part_json (GMimeObject *part, int *part_count)
 }
 
 static void
-show_message (void *ctx, const show_format_t *format, notmuch_message_t *message, int indent)
+show_message (void *ctx, const show_format_t *format,
+	      notmuch_output_options_t *output_options,
+	      notmuch_message_t *message, int indent)
 {
     fputs (format->message_start, stdout);
     if (format->message)
@@ -345,20 +363,24 @@ show_message (void *ctx, const show_format_t *format, notmuch_message_t *message
 
     fputs (format->header_start, stdout);
     if (format->header)
-	format->header(ctx, message);
+	format->header(ctx, output_options, message);
     fputs (format->header_end, stdout);
 
-    fputs (format->body_start, stdout);
-    if (format->part)
-	show_message_body (notmuch_message_get_filename (message), format->part);
-    fputs (format->body_end, stdout);
+    if (output_get_flag(output_options, NOTMUCH_OUTPUT_BODY)){
+	fputs (format->body_start, stdout);
+	if (format->part)
+	    show_message_body (notmuch_message_get_filename (message), format->part);
+	fputs (format->body_end, stdout);
+    }
 
     fputs (format->message_end, stdout);
 }
 
 
 static void
-show_messages (void *ctx, const show_format_t *format, notmuch_messages_t *messages, int indent,
+show_messages (void *ctx, const show_format_t *format,
+	       notmuch_output_options_t *output_opts,
+	       notmuch_messages_t *messages, int indent,
 	       notmuch_bool_t entire_thread)
 {
     notmuch_message_t *message;
@@ -385,13 +407,14 @@ show_messages (void *ctx, const show_format_t *format, notmuch_messages_t *messa
 	next_indent = indent;
 
 	if (match || entire_thread) {
-	    show_message (ctx, format, message, indent);
+	    show_message (ctx, format, output_opts, message, indent);
 	    next_indent = indent + 1;
 
 	    fputs (format->message_set_sep, stdout);
 	}
 
-	show_messages (ctx, format, notmuch_message_get_replies (message),
+	show_messages (ctx, format, output_opts,
+		       notmuch_message_get_replies (message),
 		       next_indent, entire_thread);
 
 	notmuch_message_destroy (message);
@@ -411,6 +434,7 @@ notmuch_show_command (void *ctx, int argc, char *argv[])
     notmuch_threads_t *threads;
     notmuch_thread_t *thread;
     notmuch_messages_t *messages;
+    notmuch_output_options_t *output_opts;
     char *query_string;
     char *opt;
     const show_format_t *format = &format_text;
@@ -418,12 +442,17 @@ notmuch_show_command (void *ctx, int argc, char *argv[])
     int i;
     int first_toplevel = 1;
 
+    output_opts = output_init(ctx);
+
     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
 	if (strcmp (argv[i], "--") == 0) {
 	    i++;
 	    break;
 	}
-	if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
+	if (STRNCMP_LITERAL (argv[i], "--output=") == 0) {
+	  if(output_parse_arg(output_opts, argv[i]))
+	    return 1;
+	} else if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
 	    opt = argv[i] + sizeof ("--format=") - 1;
 	    if (strcmp (opt, "text") == 0) {
 		format = &format_text;
@@ -489,7 +518,7 @@ notmuch_show_command (void *ctx, int argc, char *argv[])
 	    fputs (format->message_set_sep, stdout);
 	first_toplevel = 0;
 
-	show_messages (ctx, format, messages, 0, entire_thread);
+	show_messages (ctx, format, output_opts, messages, 0, entire_thread);
 
 	notmuch_thread_destroy (thread);
 
-- 
1.7.0



More information about the notmuch mailing list