git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: tboegi@web.de
To: git@vger.kernel.org
Cc: "Torsten Bögershausen" <tboegi@web.de>
Subject: [PATCH/RFC v1 1/1] convert.c: Escape sequences only for a tty in trace_encoding()
Date: Sun, 10 Mar 2019 07:19:14 +0100	[thread overview]
Message-ID: <20190310061914.24554-1-tboegi@web.de> (raw)

From: Torsten Bögershausen <tboegi@web.de>

The content of a buffer can be dumped using trace_encoding()
before and after the encoding is converted.
The current function trace_encoding() in convert.c tries to
make the output easier to read:
The byte position and the character itself are dimmed, allowing
the eye to focus on the hex values in the byte stream.

ANSI escape sequences are used to "dim" the display temporally,
and to restore the normal brightness.

When stdout is re-directed into a file, those sequences are not
working as expected (but shown in the editor) which is disturbing.
rather then helpful.

Disable them, if stdout is not a tty.

Signed-off-by: Torsten Bögershausen <tboegi@web.de>
---
 I am temped to remove the "dim" functionality all together,
 or to remove the printout of the values which are now dimmed,
 what do others think ?

convert.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/convert.c b/convert.c
index 5d0307fc10..70e58f1413 100644
--- a/convert.c
+++ b/convert.c
@@ -42,6 +42,9 @@ struct text_stat {
 	unsigned printable, nonprintable;
 };

+static const char *terminal_half_bright;
+static const char *terminal_reset_normal;
+
 static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats)
 {
 	unsigned long i;
@@ -330,14 +333,23 @@ static void trace_encoding(const char *context, const char *path,
 	static struct trace_key coe = TRACE_KEY_INIT(WORKING_TREE_ENCODING);
 	struct strbuf trace = STRBUF_INIT;
 	int i;
-
+	if (!terminal_half_bright || !terminal_reset_normal) {
+		if (isatty(1)) {
+			terminal_half_bright  = "\033[2m";
+			terminal_reset_normal = "\033[0m";
+		} else {
+			terminal_half_bright = "";
+			terminal_reset_normal = "";
+		}
+	}
 	strbuf_addf(&trace, "%s (%s, considered %s):\n", context, path, encoding);
 	for (i = 0; i < len && buf; ++i) {
+		char c = buf[i] > 32 && buf[i] < 127 ? buf[i] : ' ';
 		strbuf_addf(
-			&trace, "| \033[2m%2i:\033[0m %2x \033[2m%c\033[0m%c",
-			i,
+			&trace, "| %s%2i:%s %2x %s%c%s%c",
+			terminal_half_bright, i, terminal_reset_normal,
 			(unsigned char) buf[i],
-			(buf[i] > 32 && buf[i] < 127 ? buf[i] : ' '),
+			terminal_half_bright, c, terminal_reset_normal,
 			((i+1) % 8 && (i+1) < len ? ' ' : '\n')
 		);
 	}
--
2.21.0.135.g6e0cc67761


             reply	other threads:[~2019-03-10  6:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-10  6:19 tboegi [this message]
2019-03-11  2:58 ` [PATCH/RFC v1 1/1] convert.c: Escape sequences only for a tty in trace_encoding() Junio C Hamano

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: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=20190310061914.24554-1-tboegi@web.de \
    --to=tboegi@web.de \
    --cc=git@vger.kernel.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://80x24.org/mirrors/git.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).