]> git.notmuchmail.org Git - notmuch/blobdiff - notmuch-show.c
show: Associate an sprinter with each format
[notmuch] / notmuch-show.c
index 8f3c60e9882cd7aae718a8c27a50cc8cfe999569..d04943f7d337fbb688c5790185d2da5627e1b25e 100644 (file)
 
 #include "notmuch-client.h"
 #include "gmime-filter-reply.h"
+#include "sprinter.h"
 
 static notmuch_status_t
 format_part_text (const void *ctx, mime_node_t *node,
                  int indent, const notmuch_show_params_t *params);
 
 static const notmuch_show_format_t format_text = {
+    .new_sprinter = sprinter_text_create,
     .part = format_part_text,
 };
 
@@ -34,6 +36,7 @@ format_part_json_entry (const void *ctx, mime_node_t *node,
                        int indent, const notmuch_show_params_t *params);
 
 static const notmuch_show_format_t format_json = {
+    .new_sprinter = sprinter_json_create,
     .message_set_start = "[",
     .part = format_part_json_entry,
     .message_set_sep = ", ",
@@ -46,6 +49,7 @@ format_part_mbox (const void *ctx, mime_node_t *node,
                  int indent, const notmuch_show_params_t *params);
 
 static const notmuch_show_format_t format_mbox = {
+    .new_sprinter = sprinter_text_create,
     .part = format_part_mbox,
 };
 
@@ -55,6 +59,7 @@ format_part_raw (unused (const void *ctx), mime_node_t *node,
                 unused (const notmuch_show_params_t *params));
 
 static const notmuch_show_format_t format_raw = {
+    .new_sprinter = sprinter_text_create,
     .part = format_part_raw,
 };
 
@@ -559,7 +564,7 @@ format_part_text (const void *ctx, mime_node_t *node,
 }
 
 void
-format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
+format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first, notmuch_bool_t output_body)
 {
     /* Any changes to the JSON format should be reflected in the file
      * devel/schemata. */
@@ -571,10 +576,12 @@ format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
        printf ("\"headers\": ");
        format_headers_json (ctx, GMIME_MESSAGE (node->part), FALSE);
 
-       printf (", \"body\": [");
-       format_part_json (ctx, mime_node_child (node, 0), first);
-
-       printf ("]}");
+       if (output_body) {
+           printf (", \"body\": [");
+           format_part_json (ctx, mime_node_child (node, 0), first, TRUE);
+           printf ("]");
+       }
+       printf ("}");
        return;
     }
 
@@ -652,16 +659,16 @@ format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
     talloc_free (local);
 
     for (i = 0; i < node->nchildren; i++)
-       format_part_json (ctx, mime_node_child (node, i), i == 0);
+       format_part_json (ctx, mime_node_child (node, i), i == 0, TRUE);
 
     printf ("%s}", terminator);
 }
 
 static notmuch_status_t
 format_part_json_entry (const void *ctx, mime_node_t *node, unused (int indent),
-                       unused (const notmuch_show_params_t *params))
+                       const notmuch_show_params_t *params)
 {
-    format_part_json (ctx, node, TRUE);
+    format_part_json (ctx, node, TRUE, params->output_body);
 
     return NOTMUCH_STATUS_SUCCESS;
 }
@@ -1001,9 +1008,11 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
     char *query_string;
     int opt_index, ret;
     const notmuch_show_format_t *format = &format_text;
+    sprinter_t *sprinter;
     notmuch_show_params_t params = {
        .part = -1,
        .omit_excluded = TRUE,
+       .output_body = TRUE,
        .crypto = {
            .verify = FALSE,
            .decrypt = FALSE
@@ -1032,6 +1041,7 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
        { NOTMUCH_OPT_INT, &params.part, "part", 'p', 0 },
        { NOTMUCH_OPT_BOOLEAN, &params.crypto.decrypt, "decrypt", 'd', 0 },
        { NOTMUCH_OPT_BOOLEAN, &params.crypto.verify, "verify", 'v', 0 },
+       { NOTMUCH_OPT_BOOLEAN, &params.output_body, "body", 'b', 0 },
        { 0, 0, 0, 0, 0 }
     };
 
@@ -1086,6 +1096,16 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
            entire_thread = ENTIRE_THREAD_FALSE;
     }
 
+    if (!params.output_body) {
+       if (params.part > 0) {
+           fprintf (stderr, "Warning: --body=false is incompatible with --part > 0. Disabling.\n");
+           params.output_body = TRUE;
+       } else {
+           if (format != &format_json)
+               fprintf (stderr, "Warning: --body=false only implemented for format=json\n");
+       }
+    }
+
     if (entire_thread == ENTIRE_THREAD_TRUE)
        params.entire_thread = TRUE;
     else
@@ -1116,6 +1136,9 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
        return 1;
     }
 
+    /* Create structure printer. */
+    sprinter = format->new_sprinter(ctx, stdout);
+
     /* If a single message is requested we do not use search_excludes. */
     if (params.part >= 0)
        ret = do_show_single (ctx, query, format, &params);