[PATCH v3 2/2] cli/show: add color for --format=pretty

Subject:[PATCH v3 2/2] cli/show: add color for --format=pretty

Date:Mon, 12 Jul 2021 11:38:21 +0300

To:notmuch@notmuchmail.org

Cc:Hannu Hartikainen

From:Hannu Hartikainen


The arguments `--color` and `--no-color` allow setting color explicitly
on or off when using `--format=pretty`. The default is to use color iff
stdout is a TTY.
---
 NEWS                      |  4 +++-
 doc/man1/notmuch-show.rst |  9 ++++++++-
 notmuch-client.h          |  1 +
 notmuch-show.c            | 15 +++++++++++++++
 4 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/NEWS b/NEWS
index 57826734..ac6fdddd 100644
--- a/NEWS
+++ b/NEWS
@@ -6,7 +6,9 @@ CLI
 
 `notmuch show` now has `--format=pretty`, optimized for reading plain
 text emails on the command line. It only shows the most important
-headers and plain text parts.
+headers and plain text parts. The output is colored if the output is a
+TTY or.  Alternatively, `--color` and `--no-color` can be used to set
+color explicitly on or off.
 
 Emacs
 -----
diff --git a/doc/man1/notmuch-show.rst b/doc/man1/notmuch-show.rst
index c68f0b2b..d1faf8fe 100644
--- a/doc/man1/notmuch-show.rst
+++ b/doc/man1/notmuch-show.rst
@@ -49,7 +49,8 @@ Supported options for **show** include
    **pretty**
      The plain-text parts of all matching messages are printed in a
      format optimized for readability. Only the most important
-     headers are displayed.
+     headers are displayed. Color is used if the output is to a TTY
+     unless ``--color`` or ``--no-color`` is used explicitly.
 
    **json**
      The output is formatted with Javascript Object Notation
@@ -221,6 +222,12 @@ Supported options for **show** include
    "text/html" parts, no part with content type "text/html" is included
    in the output.
 
+.. option:: --color
+
+   Use colored output for formats that support that (currently only
+   ``--format=pretty``). By default, color is used if the output
+   goes to a TTY. To disable color, use ``--no-color``.
+
 A common use of **notmuch show** is to display a single thread of
 email messages. For this, use a search term of "thread:<thread-id>" as
 can be seen in the first column of output from the
diff --git a/notmuch-client.h b/notmuch-client.h
index 8227fea4..c4f7cfd6 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -78,6 +78,7 @@ typedef struct notmuch_show_params {
     int part;
     _notmuch_crypto_t crypto;
     bool include_html;
+    bool color;
     GMimeStream *out_stream;
 } notmuch_show_params_t;
 
diff --git a/notmuch-show.c b/notmuch-show.c
index 69b1e697..a67bd698 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -617,14 +617,21 @@ format_part_pretty (const void *ctx, sprinter_t *sp, mime_node_t *node,
     GMimeContentType *content_type = g_mime_object_get_content_type (meta);
     GMimeStream *stream = params->out_stream;
     int i;
+    bool color = params->color;
 
     if (GMIME_IS_MESSAGE (node->part)) {
 	GMimeMessage *message = GMIME_MESSAGE (node->part);
 	char *recipients_string;
 	char *date_string;
 
+	if (color)
+	    g_mime_stream_printf (stream, "\e[36m");
 	g_mime_stream_printf (stream, "Subject: %s\n", g_mime_message_get_subject (message));
+	if (color)
+	    g_mime_stream_printf (stream, "\e[33m");
 	g_mime_stream_printf (stream, "From: %s\n", g_mime_message_get_from_string (message));
+	if (color)
+	    g_mime_stream_printf (stream, "\e[31m");
 	recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_TO);
 	if (recipients_string)
 	    g_mime_stream_printf (stream, "To: %s\n", recipients_string);
@@ -634,9 +641,15 @@ format_part_pretty (const void *ctx, sprinter_t *sp, mime_node_t *node,
 	    g_mime_stream_printf (stream, "Cc: %s\n", recipients_string);
 	g_free (recipients_string);
 	date_string = g_mime_message_get_date_string (node, message);
+	if (color)
+	    g_mime_stream_printf (stream, "\e[35m");
 	g_mime_stream_printf (stream, "Date: %s\n", date_string);
+	if (color)
+	    g_mime_stream_printf (stream, "\e[32m");
 	g_mime_stream_printf (stream, "Message-ID: <%s>\n\n", g_mime_message_get_message_id (
 				  message));
+	if (color)
+	    g_mime_stream_printf (stream, "\e[0m");
     }
 
     if (GMIME_IS_PART (node->part) &&
@@ -1294,6 +1307,7 @@ notmuch_show_command (notmuch_database_t *notmuch, int argc, char *argv[])
 	.part = -1,
 	.omit_excluded = true,
 	.output_body = true,
+	.color = isatty (fileno (stdout)),
 	.crypto = { .decrypt = NOTMUCH_DECRYPT_AUTO },
     };
     int format = NOTMUCH_FORMAT_NOT_SPECIFIED;
@@ -1333,6 +1347,7 @@ notmuch_show_command (notmuch_database_t *notmuch, int argc, char *argv[])
 	{ .opt_bool = &params.crypto.verify, .name = "verify" },
 	{ .opt_bool = &params.output_body, .name = "body" },
 	{ .opt_bool = &params.include_html, .name = "include-html" },
+	{ .opt_bool = &params.color, .name = "color" },
 	{ .opt_inherit = notmuch_shared_options },
 	{ }
     };
-- 
2.32.0
_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: