unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* Initial support for selecting what is output
@ 2010-04-24 11:44 david
  2010-04-24 11:44 ` [PATCH 1/3] remove stale "unused" from around argv and argc in notmuch_show_command david
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: david @ 2010-04-24 11:44 UTC (permalink / raw)
  To: notmuch

These are some preliminary patches to add a --output argument to
notmuch show.

	notmuch show --format=json --output=from,subject ${query}

or

	notmuch show --format=json --output=to,body ${query}

I'd like to implement a bit finer control (to get message-id's only is
not possible right now), and it needs documentation and a test, but I
thought I'd toss this out there to see what people think.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] remove stale "unused" from around argv and argc in notmuch_show_command.
  2010-04-24 11:44 Initial support for selecting what is output david
@ 2010-04-24 11:44 ` david
  2010-04-24 11:44 ` [PATCH 2/3] notmuch-output.[ch]: initial implementation of output selection argument handling david
  2010-04-24 11:44 ` [PATCH 3/3] notmuch-show.c: control which headers are show for json output david
  2 siblings, 0 replies; 4+ messages in thread
From: david @ 2010-04-24 11:44 UTC (permalink / raw)
  To: notmuch; +Cc: David Bremner

From: David Bremner <bremner@unb.ca>

In fact argc and argv are used in this function.
---
 notmuch-show.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/notmuch-show.c b/notmuch-show.c
index 26449fa..6aa9072 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -403,7 +403,7 @@ show_messages (void *ctx, const show_format_t *format, notmuch_messages_t *messa
 }
 
 int
-notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
+notmuch_show_command (void *ctx, int argc, char *argv[])
 {
     notmuch_config_t *config;
     notmuch_database_t *notmuch;
-- 
1.7.0

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] notmuch-output.[ch]: initial implementation of output selection argument handling.
  2010-04-24 11:44 Initial support for selecting what is output david
  2010-04-24 11:44 ` [PATCH 1/3] remove stale "unused" from around argv and argc in notmuch_show_command david
@ 2010-04-24 11:44 ` david
  2010-04-24 11:44 ` [PATCH 3/3] notmuch-show.c: control which headers are show for json output david
  2 siblings, 0 replies; 4+ messages in thread
From: david @ 2010-04-24 11:44 UTC (permalink / raw)
  To: notmuch; +Cc: David Bremner

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 5139 bytes --]

From: David Bremner <bremner@unb.ca>

These two files provide a more or less "object oriented" approach to
parsing output selection arguments.  The use of a struct to track
which output pieces are selected is intended to hide the
implementation, which currently uses bitmasks.
---
 Makefile.local   |    1 +
 notmuch-output.c |   95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 notmuch-output.h |   49 ++++++++++++++++++++++++++++
 3 files changed, 145 insertions(+), 0 deletions(-)
 create mode 100644 notmuch-output.c
 create mode 100644 notmuch-output.h

diff --git a/Makefile.local b/Makefile.local
index 5bb570b..eb78679 100644
--- a/Makefile.local
+++ b/Makefile.local
@@ -240,6 +240,7 @@ notmuch_client_srcs =		\
 	notmuch-count.c		\
 	notmuch-dump.c		\
 	notmuch-new.c		\
+	notmuch-output.c	\
 	notmuch-reply.c		\
 	notmuch-restore.c	\
 	notmuch-search.c	\
diff --git a/notmuch-output.c b/notmuch-output.c
new file mode 100644
index 0000000..a84bbe1
--- /dev/null
+++ b/notmuch-output.c
@@ -0,0 +1,95 @@
+/* notmuch-output.h --- encapsulate handling of output selection.
+ *
+ * Copyright 2010 © David Bremner
+ *
+ * This file is part of Notmuch.
+ *
+ * Notmuch is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation, either version 3 of the License, or (at
+ * your option) any later version.
+ *
+ * Notmuch is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Notmuch.  If not, see <http:www.gnu.org/licenses/>.
+ *
+ * Authors: David Bremner <david@tethera.net>
+ */
+
+#include "notmuch-client.h"
+#include "notmuch-output.h"
+
+notmuch_output_options_t *
+output_init(void *ctx){
+    notmuch_output_options_t *output_opts;
+
+    output_opts = talloc (ctx, notmuch_output_options_t);
+    output_opts->output_all=TRUE;
+    output_opts->mask=0;
+    return output_opts;
+}
+
+int
+output_parse_arg(notmuch_output_options_t *output_opts, const char *arg){
+
+    char *opt;
+
+    struct { const char *name; notmuch_output_t key; } parse_try[]={
+      {"bcc", NOTMUCH_OUTPUT_BCC},
+      {"body", NOTMUCH_OUTPUT_BODY},
+      {"cc", NOTMUCH_OUTPUT_CC},
+      {"date",NOTMUCH_OUTPUT_DATE},
+      {"from",NOTMUCH_OUTPUT_FROM},
+      {"message-id",NOTMUCH_OUTPUT_MESSAGE_ID},
+      {"subject",NOTMUCH_OUTPUT_SUBJECT},
+      {"to", NOTMUCH_OUTPUT_TO},
+    };
+
+    opt = strchr(arg, '=');
+    if (!opt)
+	return 1;
+
+    opt++;
+
+    output_opts->output_all=FALSE;
+
+    while(opt) {
+	unsigned int i;
+	notmuch_bool_t found = FALSE;
+	notmuch_output_t key = 0;
+
+	for (i = 0; i<ARRAY_SIZE(parse_try) ; i++){
+	    if (strncmp(opt, parse_try[i].name,
+			strlen(parse_try[i].name)) == 0){
+		key = parse_try[i].key;
+		found = TRUE;
+	    }
+	}
+
+	if (!found) {
+	    fprintf (stderr, "Unrecognized output selection: %s\n", opt);
+	    return 1;
+	}
+
+	output_opts->mask |= (1<<key);
+
+	opt = strchr(opt, ',');
+	if (opt) opt++;
+    }
+
+    return 0;
+}
+
+notmuch_bool_t
+output_get_flag(notmuch_output_options_t *opts,
+		notmuch_output_t flag){
+
+    if (opts->output_all)
+	return 1;
+
+    return (opts->mask & (1<<flag));
+}
diff --git a/notmuch-output.h b/notmuch-output.h
new file mode 100644
index 0000000..1216499
--- /dev/null
+++ b/notmuch-output.h
@@ -0,0 +1,49 @@
+/* notmuch-output.h --- encapsulate handling of output selection.
+ *
+ * Copyright 2010 © David Bremner
+ *
+ * This file is part of Notmuch.
+ *
+ * Notmuch is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation, either version 3 of the License, or (at
+ * your option) any later version.
+ *
+ * Notmuch is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Notmuch.  If not, see <http:www.gnu.org/licenses/>.
+ *
+ * Authors: David Bremner <david@tethera.net>
+ */
+
+#ifndef NOTMUCH_OUTPUT_H
+#define NOTMUCH_OUTPUT_H
+
+typedef struct notmuch_output_struct {
+    notmuch_bool_t output_all;
+    int mask;
+} notmuch_output_options_t;
+
+typedef enum notmuch_output_enum {
+    NOTMUCH_OUTPUT_BCC,
+    NOTMUCH_OUTPUT_BODY,
+    NOTMUCH_OUTPUT_CC,
+    NOTMUCH_OUTPUT_DATE,
+    NOTMUCH_OUTPUT_FROM,
+    NOTMUCH_OUTPUT_MESSAGE_ID,
+    NOTMUCH_OUTPUT_SUBJECT,
+    NOTMUCH_OUTPUT_TO
+} notmuch_output_t;
+
+notmuch_output_options_t *output_init(void *ctx);
+
+int output_parse_arg(notmuch_output_options_t *opts,
+		     const char* arg);
+
+notmuch_bool_t output_get_flag(notmuch_output_options_t *opts,
+			       notmuch_output_t flag);
+#endif
-- 
1.7.0

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] notmuch-show.c: control which headers are show for json output.
  2010-04-24 11:44 Initial support for selecting what is output david
  2010-04-24 11:44 ` [PATCH 1/3] remove stale "unused" from around argv and argc in notmuch_show_command david
  2010-04-24 11:44 ` [PATCH 2/3] notmuch-output.[ch]: initial implementation of output selection argument handling david
@ 2010-04-24 11:44 ` david
  2 siblings, 0 replies; 4+ messages in thread
From: david @ 2010-04-24 11:44 UTC (permalink / raw)
  To: notmuch; +Cc: David Bremner

From: David Bremner <bremner@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

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-04-24 11:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-04-24 11:44 Initial support for selecting what is output david
2010-04-24 11:44 ` [PATCH 1/3] remove stale "unused" from around argv and argc in notmuch_show_command david
2010-04-24 11:44 ` [PATCH 2/3] notmuch-output.[ch]: initial implementation of output selection argument handling david
2010-04-24 11:44 ` [PATCH 3/3] notmuch-show.c: control which headers are show for json output david

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).