-/* If the user asks for more than this number of threads, then break
- the results down into chunks so that results start appearing
- quickly rather than the user having to wait until all results are
- available before anything appears.
-
- Since each subsequent chunk ends up having to re-do threading work
- done by all previous chunks, we double the chunk size repeatedly
- until all desired results have been returned.
-*/
-#define NOTMUCH_SEARCH_INITIAL_CHUNK_SIZE 100
-
-/* Do the actual search for a chunk of threads and display the results,
- (returning the number of threads found in this chunk). */
+typedef enum {
+ OUTPUT_SUMMARY,
+ OUTPUT_THREADS,
+ OUTPUT_MESSAGES,
+ OUTPUT_FILES,
+ OUTPUT_TAGS
+} output_t;
+
+typedef struct search_format {
+ const char *results_start;
+ const char *item_start;
+ void (*item_id) (const void *ctx,
+ const char *item_type,
+ const char *item_id);
+ void (*thread_summary) (const void *ctx,
+ const char *thread_id,
+ const time_t date,
+ const int matched,
+ const int total,
+ const char *authors,
+ const char *subject);
+ const char *tag_start;
+ const char *tag;
+ const char *tag_sep;
+ const char *tag_end;
+ const char *item_sep;
+ const char *item_end;
+ const char *results_end;
+ const char *results_null;
+} search_format_t;
+
+static void
+format_item_id_text (const void *ctx,
+ const char *item_type,
+ const char *item_id);
+
+static void
+format_thread_text (const void *ctx,
+ const char *thread_id,
+ const time_t date,
+ const int matched,
+ const int total,
+ const char *authors,
+ const char *subject);
+static const search_format_t format_text = {
+ "",
+ "",
+ format_item_id_text,
+ format_thread_text,
+ " (",
+ "%s", " ",
+ ")", "\n",
+ "",
+ "\n",
+ "",
+};
+
+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);
+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)
+{
+ printf ("%s%s", item_type, item_id);
+}
+
+static char *
+sanitize_string (const void *ctx, const char *str)
+{
+ char *out, *loop;
+
+ if (NULL == str)
+ return NULL;
+
+ loop = out = talloc_strdup (ctx, str);
+
+ for (; *loop; loop++) {
+ if ((unsigned char)(*loop) < 32)
+ *loop = '?';
+ }
+ return out;
+}
+
+static void
+format_thread_text (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 %12s [%d/%d] %s; %s",
+ thread_id,
+ notmuch_time_relative_date (ctx, date),
+ matched,
+ total,
+ sanitize_string (ctx_quote, authors),
+ sanitize_string (ctx_quote, subject));
+
+ 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"
+ "\"matched\": %d,\n"
+ "\"total\": %d,\n"
+ "\"authors\": %s,\n"
+ "\"subject\": %s,\n",
+ json_quote_str (ctx_quote, thread_id),
+ date,
+ matched,
+ total,
+ json_quote_str (ctx_quote, authors),
+ json_quote_str (ctx_quote, subject));
+
+ talloc_free (ctx_quote);
+}
+