[PATCH] cli/show: add --color/--no-color

Subject: [PATCH] cli/show: add --color/--no-color

Date: Tue, 22 Jun 2021 15:34:10 +0300

To: notmuch@notmuchmail.org

Cc: Hannu Hartikainen

From: Hannu Hartikainen


These arguments allow setting color explicitly on or off when using
`--format=pretty`. The default is still to use color iff stdout is a
TTY.
---

I got a reply off-list to my previous patch (cli/show: add
--format=pretty) about explicitly enabling color (for use with $PAGER).
It's quite a minor addition so I just implemented it. IMHO this warrants
its own commit; it's a separate feature from --format=pretty. But I'm ok
with squashing these together if that's considered better.

Of course, with an explicit --color command-line argument in one command
for one purpose, one could reasonably ask if other commands should
support colored output as well. What do you all think?

 doc/man1/notmuch-show.rst | 6 ++++++
 notmuch-client.h          | 1 +
 notmuch-show.c            | 4 +++-
 3 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/doc/man1/notmuch-show.rst b/doc/man1/notmuch-show.rst
index 1fe4dcc7..e61000a6 100644
--- a/doc/man1/notmuch-show.rst
+++ b/doc/man1/notmuch-show.rst
@@ -209,6 +209,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 fe3b753e..6274a99a 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -612,7 +612,7 @@ 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 = isatty (fileno (stdout));
+    bool color = params->color;
 
     if (GMIME_IS_MESSAGE (node->part)) {
 	GMimeMessage *message = GMIME_MESSAGE (node->part);
@@ -1300,6 +1300,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;
@@ -1334,6 +1335,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: