From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id QPCAEsD/62DFygAAgWs5BA (envelope-from ) for ; Mon, 12 Jul 2021 10:39:28 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id yOUgDsD/62AWJQAAbx9fmQ (envelope-from ) for ; Mon, 12 Jul 2021 08:39:28 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [144.217.243.247]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 9259BBC87 for ; Mon, 12 Jul 2021 10:39:27 +0200 (CEST) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 7356F290B9; Mon, 12 Jul 2021 04:39:23 -0400 (EDT) Received: from mail-lj1-f179.google.com (mail-lj1-f179.google.com [209.85.208.179]) by mail.notmuchmail.org (Postfix) with ESMTPS id 61D91290B8 for ; Mon, 12 Jul 2021 04:39:20 -0400 (EDT) Received: by mail-lj1-f179.google.com with SMTP id u25so22887528ljj.11 for ; Mon, 12 Jul 2021 01:39:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZMXh8XBZ3/52K1jew8M92M9nwkcsdSvuTJ+iKdvXjug=; b=klaTXMTwQWxT/KMH2Y5gz115Pb0nw/8o0pDqy+NiQygSfAqMvIUMuY0oJGZIlwJ6lz fn2II3db1aMhDzjxZ6gmSBGfiExUQ+gIMxKGoK0ONoinQ94QdJcAusm6ww8Rasab8SID 4iyfC0Ll06Iex+7OyD0oS0qCdrTe/T4Txs2wmqRUdLfAKyR/+JqIuErZ/K43yuqaz+iL kxIIUiTyOh6IEjSAHmRN+3VdV3aiJfoCygcFFCU9mQ+LvDlD24zczrkG5Cu+WAEOl51z WUIMy6sWCKN4s60VIqpQ+yPkox/QRzjWypS9/77Qp4KmbU+0dVTaOB4ExzL9uXxQb+Qp iAkw== X-Gm-Message-State: AOAM532PTq/cGZ/onJK7uBLne6bgTtXli+RM/iUOkWq9zhmwQdMD5Uw+ 0Q+OXo1xY9IoP443eG8XgBKyE7nOLzIgvg== X-Google-Smtp-Source: ABdhPJwzx3b4aDOz1XiES9+zH0Ta0zxxXiextsdlCb6qLEvBME2O8VMDy/zGWOs/3ZKFv4J8vRdY0g== X-Received: by 2002:a2e:8813:: with SMTP id x19mr5822926ljh.272.1626079158869; Mon, 12 Jul 2021 01:39:18 -0700 (PDT) Received: from localhost (88-112-11-80.elisa-laajakaista.fi. [88.112.11.80]) by smtp.gmail.com with ESMTPSA id m20sm1143781lfu.8.2021.07.12.01.39.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Jul 2021 01:39:18 -0700 (PDT) From: Hannu Hartikainen To: notmuch@notmuchmail.org Subject: [PATCH v3 2/2] cli/show: add color for --format=pretty Date: Mon, 12 Jul 2021 11:38:21 +0300 Message-Id: <20210712083821.442587-3-hannu@hrtk.in> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210712083821.442587-1-hannu@hrtk.in> References: <20210702203153.47289-1-hannu@hrtk.in>, <20210622123410.323216-1-hannu@hrtk.in> <20210712083821.442587-1-hannu@hrtk.in> MIME-Version: 1.0 Message-ID-Hash: R4R7PF7BGX3JRFNIVAZHNIL5LVHVKC43 X-Message-ID-Hash: R4R7PF7BGX3JRFNIVAZHNIL5LVHVKC43 X-MailFrom: hannu.hartikainen@gmail.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-notmuch.notmuchmail.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: Hannu Hartikainen X-Mailman-Version: 3.2.1 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1626079167; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=mxXwjdUbIDv7UG2/aUjsMaPcAfJrWg/33IQ6bWjHjAs=; b=b7b11TuaLbLiesCNkljQxX8CzfAfnRD6pwRiMiW2mS4sYEn97qW78wR0vxL7iRmfKS3WPS 5l4gidV046paywzw/rGZ/9lhfxfPUoDuCefcLO3jexqE7q4SztJGU6jBCrbbyXT40otHyu ASwX37+mExp5RuXRD9yynJe27Vuxqhr0HonuN5svVi56MTE9oclnFJ6QZce8ru6uS8OBn1 TMgVjrQBZwAaXs1MYpUZmHPSyYJOaJCuZxQhp0FXCVctLiCoWHzjgClKp+QJJgQai4fSJj QJU+La5WLktOiTgY/5F7+Rtsk5IgcrAzmpMhYvfM1GRMpzAJT5Q/Kq4zNlWTWg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1626079167; a=rsa-sha256; cv=none; b=aP/J3WHqdIBpyPrrDef8U4a7r7lXXOnSVf3gXExM6PH9GQpybZ7U4SW/uSjhebRoFMd4zl oGgWGVLOrcCSqyo/GKD/UItS84qA6/rYeKG4sDr11Z0psFm2sPafoVt4KOjQ1lQLp7SaDm Sw52ahnkbfK0QEI2lmdQKJq/Z6iBb7j3kfpJ10slGqIXmgfj1TatKJNpoMgf4MF1Uquso9 m9T4xw3KsTr6lBGprWraRypyRG9P/h7WTlGLsVOHZhNS5tLqBNqd/6qZY4D/Jbl1VgHthM NIhrnQMgFum+lg+gUEd3p1zYOiMF/g9u4VHQu1fbuBaaaG6NmJofpkyf/O4ylg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 144.217.243.247 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Spam-Score: -1.07 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 144.217.243.247 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: 9259BBC87 X-Spam-Score: -1.07 X-Migadu-Scanner: scn0.migadu.com X-TUID: CoIlsMQc+5pt 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:" 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 = ¶ms.crypto.verify, .name = "verify" }, { .opt_bool = ¶ms.output_body, .name = "body" }, { .opt_bool = ¶ms.include_html, .name = "include-html" }, + { .opt_bool = ¶ms.color, .name = "color" }, { .opt_inherit = notmuch_shared_options }, { } }; -- 2.32.0