[PATCH 4/9] cli/show: use a table for choosing the formatter

Subject: [PATCH 4/9] cli/show: use a table for choosing the formatter

Date: Fri, 6 Jan 2017 22:14:45 +0200

To: notmuch@notmuchmail.org

Cc:

From: Jani Nikula


Continue detangling format pointer and format selection variables. No
functional changes.
---
 notmuch-show.c | 36 +++++++++++++++---------------------
 1 file changed, 15 insertions(+), 21 deletions(-)

diff --git a/notmuch-show.c b/notmuch-show.c
index b8e45f187360..93b51008381c 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -978,6 +978,14 @@ enum {
     NOTMUCH_FORMAT_RAW
 };
 
+static const notmuch_show_format_t *formatters[] = {
+    [NOTMUCH_FORMAT_JSON] = &format_json,
+    [NOTMUCH_FORMAT_SEXP] = &format_sexp,
+    [NOTMUCH_FORMAT_TEXT] = &format_text,
+    [NOTMUCH_FORMAT_MBOX] = &format_mbox,
+    [NOTMUCH_FORMAT_RAW] = &format_raw,
+};
+
 enum {
     ENTIRE_THREAD_DEFAULT,
     ENTIRE_THREAD_TRUE,
@@ -997,7 +1005,7 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
     notmuch_query_t *query;
     char *query_string;
     int opt_index, ret;
-    const notmuch_show_format_t *format = &format_text;
+    const notmuch_show_format_t *formatter;
     sprinter_t *sprinter;
     notmuch_show_params_t params = {
 	.part = -1,
@@ -1063,29 +1071,14 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
 	    format_sel = NOTMUCH_FORMAT_TEXT;
     }
 
-    switch (format_sel) {
-    case NOTMUCH_FORMAT_JSON:
-	format = &format_json;
-	break;
-    case NOTMUCH_FORMAT_TEXT:
-	format = &format_text;
-	break;
-    case NOTMUCH_FORMAT_SEXP:
-	format = &format_sexp;
-	break;
-    case NOTMUCH_FORMAT_MBOX:
+    if (format_sel == NOTMUCH_FORMAT_MBOX) {
 	if (params.part > 0) {
 	    fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
 	    return EXIT_FAILURE;
 	}
-
-	format = &format_mbox;
-	break;
-    case NOTMUCH_FORMAT_RAW:
-	format = &format_raw;
+    } else if (format_sel == NOTMUCH_FORMAT_RAW) {
 	/* raw format only supports single message display */
 	single_message = TRUE;
-	break;
     }
 
     notmuch_exit_if_unsupported_format ();
@@ -1148,11 +1141,12 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
     }
 
     /* Create structure printer. */
-    sprinter = format->new_sprinter(config, stdout);
+    formatter = formatters[format_sel];
+    sprinter = formatter->new_sprinter(config, stdout);
 
     /* If a single message is requested we do not use search_excludes. */
     if (single_message) {
-	ret = do_show_single (config, query, format, sprinter, &params);
+	ret = do_show_single (config, query, formatter, sprinter, &params);
     } else {
 	/* We always apply set the exclude flag. The
 	 * exclude=true|false option controls whether or not we return
@@ -1171,7 +1165,7 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
 	    params.omit_excluded = FALSE;
 	}
 
-	ret = do_show (config, query, format, sprinter, &params);
+	ret = do_show (config, query, formatter, sprinter, &params);
     }
 
     notmuch_crypto_cleanup (&params.crypto);
-- 
2.11.0


Thread: