unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Hannu Hartikainen <hannu@hrtk.in>
To: notmuch@notmuchmail.org
Cc: Hannu Hartikainen <hannu@hrtk.in>
Subject: [PATCH v3 2/2] cli/show: add color for --format=pretty
Date: Mon, 12 Jul 2021 11:38:21 +0300	[thread overview]
Message-ID: <20210712083821.442587-3-hannu@hrtk.in> (raw)
In-Reply-To: <20210712083821.442587-1-hannu@hrtk.in>

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

  parent reply	other threads:[~2021-07-12  8:39 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-19 20:38 [PATCH] cli/show: add --format=pretty Hannu Hartikainen
2021-06-22 12:34 ` [PATCH] cli/show: add --color/--no-color Hannu Hartikainen
2021-07-12  8:38   ` Yet another revision of --format=pretty Hannu Hartikainen
2021-07-12  8:38     ` [PATCH v3 1/2] cli/show: add --format=pretty Hannu Hartikainen
2021-07-12 23:40       ` David Bremner
2021-07-13 10:24         ` David Bremner
2021-07-13 11:50           ` Hannu Hartikainen
2021-07-12  8:38     ` Hannu Hartikainen [this message]
2021-07-12 23:19     ` Yet another revision of --format=pretty David Bremner
2021-07-13 12:07       ` Hannu Hartikainen
2021-07-02 20:31 ` [PATCH v2] cli/show: add --format=pretty Hannu Hartikainen
2021-07-04  9:40   ` Tomi Ollila
2021-07-04 11:54     ` Hannu Hartikainen
2021-07-04 14:38 ` [PATCH] " David Bremner
2021-07-05 13:31   ` Hannu Hartikainen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210712083821.442587-3-hannu@hrtk.in \
    --to=hannu@hrtk.in \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).