[PATCH v3 3/3] Use the JSON structure printer in notmuch-search.

2012-07-11 Thread cra...@gmx.net
This patch uses the JSON structure printer for notmuch search. The
changes necessary for switching to the structure printer are minor, a
large part of this patch is concerned with keeping the normal
unstructured text output.
---
 notmuch-search.c | 278 ++-
 1 file changed, 150 insertions(+), 128 deletions(-)

diff --git a/notmuch-search.c b/notmuch-search.c
index 3be296d..84e4cbc 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -19,6 +19,7 @@
  */

 #include "notmuch-client.h"
+#include "structured-output.h"

 typedef enum {
 OUTPUT_SUMMARY,
@@ -28,6 +29,7 @@ typedef enum {
 OUTPUT_TAGS
 } output_t;

+/* legacy, only needed for non-structured text output */
 typedef struct search_format {
 const char *results_start;
 const char *item_start;
@@ -64,6 +66,7 @@ format_thread_text (const void *ctx,
const int total,
const char *authors,
const char *subject);
+
 static const search_format_t format_text = {
 "",
"",
@@ -78,35 +81,6 @@ static const search_format_t format_text = {
 };

 static void
-format_item_id_json (const void *ctx,
-const char *item_type,
-const char *item_id);
-
-static void
-format_thread_json (const void *ctx,
-   const char *thread_id,
-   const time_t date,
-   const int matched,
-   const int total,
-   const char *authors,
-   const char *subject);
-
-/* Any changes to the JSON format should be reflected in the file
- * devel/schemata. */
-static const search_format_t format_json = {
-"[",
-   "{",
-   format_item_id_json,
-   format_thread_json,
-   "\"tags\": [",
-   "\"%s\"", ", ",
-   "]", ",\n",
-   "}",
-"]\n",
-"]\n",
-};
-
-static void
 format_item_id_text (unused (const void *ctx),
 const char *item_type,
 const char *item_id)
@@ -153,50 +127,9 @@ format_thread_text (const void *ctx,
 talloc_free (ctx_quote);
 }

-static void
-format_item_id_json (const void *ctx,
-unused (const char *item_type),
-const char *item_id)
-{
-void *ctx_quote = talloc_new (ctx);
-
-printf ("%s", json_quote_str (ctx_quote, item_id));
-
-talloc_free (ctx_quote);
-
-}
-
-static void
-format_thread_json (const void *ctx,
-   const char *thread_id,
-   const time_t date,
-   const int matched,
-   const int total,
-   const char *authors,
-   const char *subject)
-{
-void *ctx_quote = talloc_new (ctx);
-
-printf ("\"thread\": %s,\n"
-   "\"timestamp\": %ld,\n"
-   "\"date_relative\": \"%s\",\n"
-   "\"matched\": %d,\n"
-   "\"total\": %d,\n"
-   "\"authors\": %s,\n"
-   "\"subject\": %s,\n",
-   json_quote_str (ctx_quote, thread_id),
-   date,
-   notmuch_time_relative_date (ctx, date),
-   matched,
-   total,
-   json_quote_str (ctx_quote, authors),
-   json_quote_str (ctx_quote, subject));
-
-talloc_free (ctx_quote);
-}
-
 static int
-do_search_threads (const search_format_t *format,
+do_search_threads (const structure_printer_t *format,
+  void *state,
   notmuch_query_t *query,
   notmuch_sort_t sort,
   output_t output,
@@ -209,6 +142,8 @@ do_search_threads (const search_format_t *format,
 time_t date;
 int first_thread = 1;
 int i;
+int outermost_level = 0;
+int items_level = 0;

 if (offset < 0) {
offset += notmuch_query_count_threads (query);
@@ -220,7 +155,11 @@ do_search_threads (const search_format_t *format,
 if (threads == NULL)
return 1;

-fputs (format->results_start, stdout);
+if (format == unstructured_text_printer) {
+   fputs(format_text.results_start, stdout);
+} else { /* structured output */
+   outermost_level = format->list(state);
+}

 for (i = 0;
 notmuch_threads_valid (threads) && (limit < 0 || i < offset + limit);
@@ -235,43 +174,85 @@ do_search_threads (const search_format_t *format,
continue;
}

-   if (! first_thread)
-   fputs (format->item_sep, stdout);
+   if (format == unstructured_text_printer && ! first_thread)
+   fputs (format_text.item_sep, stdout);

if (output == OUTPUT_THREADS) {
-   format->item_id (thread, "thread:",
-notmuch_thread_get_thread_id (thread));
+   if (format == unstructured_text_printer) {
+   format_text.item_id (thread, "thread:",
+notmuch_thread_get_thread_id (thread));
+   } else { /* structured output */

[PATCH v3 3/3] Use the JSON structure printer in notmuch-search.

2012-07-11 Thread craven
This patch uses the JSON structure printer for notmuch search. The
changes necessary for switching to the structure printer are minor, a
large part of this patch is concerned with keeping the normal
unstructured text output.
---
 notmuch-search.c | 278 ++-
 1 file changed, 150 insertions(+), 128 deletions(-)

diff --git a/notmuch-search.c b/notmuch-search.c
index 3be296d..84e4cbc 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -19,6 +19,7 @@
  */
 
 #include notmuch-client.h
+#include structured-output.h
 
 typedef enum {
 OUTPUT_SUMMARY,
@@ -28,6 +29,7 @@ typedef enum {
 OUTPUT_TAGS
 } output_t;
 
+/* legacy, only needed for non-structured text output */
 typedef struct search_format {
 const char *results_start;
 const char *item_start;
@@ -64,6 +66,7 @@ format_thread_text (const void *ctx,
const int total,
const char *authors,
const char *subject);
+
 static const search_format_t format_text = {
 ,
,
@@ -78,35 +81,6 @@ static const search_format_t format_text = {
 };
 
 static void
-format_item_id_json (const void *ctx,
-const char *item_type,
-const char *item_id);
-
-static void
-format_thread_json (const void *ctx,
-   const char *thread_id,
-   const time_t date,
-   const int matched,
-   const int total,
-   const char *authors,
-   const char *subject);
-
-/* Any changes to the JSON format should be reflected in the file
- * devel/schemata. */
-static const search_format_t format_json = {
-[,
-   {,
-   format_item_id_json,
-   format_thread_json,
-   \tags\: [,
-   \%s\, , ,
-   ], ,\n,
-   },
-]\n,
-]\n,
-};
-
-static void
 format_item_id_text (unused (const void *ctx),
 const char *item_type,
 const char *item_id)
@@ -153,50 +127,9 @@ format_thread_text (const void *ctx,
 talloc_free (ctx_quote);
 }
 
-static void
-format_item_id_json (const void *ctx,
-unused (const char *item_type),
-const char *item_id)
-{
-void *ctx_quote = talloc_new (ctx);
-
-printf (%s, json_quote_str (ctx_quote, item_id));
-
-talloc_free (ctx_quote);
-
-}
-
-static void
-format_thread_json (const void *ctx,
-   const char *thread_id,
-   const time_t date,
-   const int matched,
-   const int total,
-   const char *authors,
-   const char *subject)
-{
-void *ctx_quote = talloc_new (ctx);
-
-printf (\thread\: %s,\n
-   \timestamp\: %ld,\n
-   \date_relative\: \%s\,\n
-   \matched\: %d,\n
-   \total\: %d,\n
-   \authors\: %s,\n
-   \subject\: %s,\n,
-   json_quote_str (ctx_quote, thread_id),
-   date,
-   notmuch_time_relative_date (ctx, date),
-   matched,
-   total,
-   json_quote_str (ctx_quote, authors),
-   json_quote_str (ctx_quote, subject));
-
-talloc_free (ctx_quote);
-}
-
 static int
-do_search_threads (const search_format_t *format,
+do_search_threads (const structure_printer_t *format,
+  void *state,
   notmuch_query_t *query,
   notmuch_sort_t sort,
   output_t output,
@@ -209,6 +142,8 @@ do_search_threads (const search_format_t *format,
 time_t date;
 int first_thread = 1;
 int i;
+int outermost_level = 0;
+int items_level = 0;
 
 if (offset  0) {
offset += notmuch_query_count_threads (query);
@@ -220,7 +155,11 @@ do_search_threads (const search_format_t *format,
 if (threads == NULL)
return 1;
 
-fputs (format-results_start, stdout);
+if (format == unstructured_text_printer) {
+   fputs(format_text.results_start, stdout);
+} else { /* structured output */
+   outermost_level = format-list(state);
+}
 
 for (i = 0;
 notmuch_threads_valid (threads)  (limit  0 || i  offset + limit);
@@ -235,43 +174,85 @@ do_search_threads (const search_format_t *format,
continue;
}
 
-   if (! first_thread)
-   fputs (format-item_sep, stdout);
+   if (format == unstructured_text_printer  ! first_thread)
+   fputs (format_text.item_sep, stdout);
 
if (output == OUTPUT_THREADS) {
-   format-item_id (thread, thread:,
-notmuch_thread_get_thread_id (thread));
+   if (format == unstructured_text_printer) {
+   format_text.item_id (thread, thread:,
+notmuch_thread_get_thread_id (thread));
+   } else { /* structured output */
+   format-string(state, notmuch_thread_get_thread_id