git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] column: use utf8_strnwidth() to strip out ANSI color escapes

From
René Scharfe <l.s.r@web.de>
Date
Oct 13, 2019, 12:49 UTC
Message-ID
<9b3f6960-ea75-c3a7-3a24-0554320bb359@web.de>

Make use of utf8_strnwidth()'s feature to skip ANSI escape sequences instead of open-coding it. This shortens the code and makes it more consistent.

This changes the behavior, though: The old code skips all kinds of Control Sequence Introducer sequences, while utf8_strnwidth() only skips the Select Graphic Rendition kind, i.e. those ending with "m". They are used for specifying color and font attributes like boldness. The only other kind of escape sequence we print in Git is Erase in Line, ending with "K". That's not used for columnar output, so this difference actually doesn't matter here.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 column.c | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)
diff --git a/column.c b/column.c
index 7a17c14b82..4a38eed322 100644
--- a/column.c
+++ b/column.c
@@ -23,18 +23,7 @@ struct column_data {
 /* return length of 's' in letters, ANSI escapes stripped */
 static int item_length(const char *s)
 {
-	int len, i = 0;
-	struct strbuf str = STRBUF_INIT;
-
-	strbuf_addstr(&str, s);
-	while ((s = strstr(str.buf + i, "\033[")) != NULL) {
-		int len = strspn(s + 2, "0123456789;");
-		i = s - str.buf;
-		strbuf_remove(&str, i, len + 3); /* \033[<len><func char> */
-	}
-	len = utf8_strwidth(str.buf);
-	strbuf_release(&str);
-	return len;
+	return utf8_strnwidth(s, -1, 1);
 }

 /*
--
2.23.0
Next: Johannes Schindelin
Message 1 of 4 in “column: use utf8_strnwidth() to strip out ANSI color escapes”
  1. column: use utf8_strnwidth() to strip out ANSI color escapesRené Scharfe, Oct 13, 2019
  2. Johannes SchindelinOct 14, 2019
  3. René ScharfeOct 14, 2019
  4. Johannes SchindelinOct 14, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.