Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 96BEC431FC1 for ; Sat, 24 Apr 2010 04:45:39 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -1.9 X-Spam-Level: X-Spam-Status: No, score=-1.9 tagged_above=-999 required=5 tests=[BAYES_00=-1.9] autolearn=ham Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ENJNmvh24AYr for ; Sat, 24 Apr 2010 04:45:37 -0700 (PDT) Received: from pivot.cs.unb.ca (pivot.cs.unb.ca [131.202.240.57]) by olra.theworths.org (Postfix) with ESMTP id 3C37C414BAE for ; Sat, 24 Apr 2010 04:45:35 -0700 (PDT) Received: from fctnnbsc30w-142167190087.pppoe-dynamic.high-speed.nb.bellaliant.net ([142.167.190.87] helo=rocinante.cs.unb.ca) by pivot.cs.unb.ca with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.69) (envelope-from ) id 1O5dni-00030n-Mq; Sat, 24 Apr 2010 08:45:34 -0300 Received: from bremner by rocinante.cs.unb.ca with local (Exim 4.71) (envelope-from ) id 1O5dnB-0005Qo-Ku; Sat, 24 Apr 2010 08:45:01 -0300 From: david@tethera.net To: notmuch@notmuchmail.org Subject: [PATCH 3/3] notmuch-show.c: control which headers are show for json output. Date: Sat, 24 Apr 2010 08:44:38 -0300 Message-Id: <1272109478-20686-4-git-send-email-david@tethera.net> X-Mailer: git-send-email 1.7.0 In-Reply-To: <1272109478-20686-1-git-send-email-david@tethera.net> References: <1272109478-20686-1-git-send-email-david@tethera.net> X-Sender-Verified: bremner@pivot.cs.unb.ca Cc: David Bremner X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 24 Apr 2010 11:45:39 -0000 From: David Bremner 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