Skip to contentSkip to conversation

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

PATCH In review René Scharfe 2 participants 4 messages Oct 13, 2019 – Oct 14, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 column: use utf8_strnwidth() to strip out ANSI color escapes

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.

---
 column.c | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)
column.c +1 -12
index 7a17c14b82..4a38eed322 100644
@@ -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

#1 lore raw

Hi René,

Quoted 3 lines · René Scharfe

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.

Sounds good.

Quoted 7 lines

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.

Arguably, the "Erase in Line" thing should re-set the width to 0, no? But as you say, this is not needed for this patch.

Thanks,
Dscho

Quoted 33 lines
---
 column.c | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)
column.c +1 -13
index 7a17c14b82..4a38eed322 100644
@@ -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

#2 lore raw

Am 14.10.19 um 13:13 schrieb Johannes Schindelin:

Quoted 12 lines

Hi René,

Quoted 7 lines · René Scharfe

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.

Quoted 2 lines

Arguably, the "Erase in Line" thing should re-set the width to 0, no? But as you say, this is not needed for this patch.

It doesn't move the cursor, just clears the characters to the right, to the left or both sides, depending on its parameter. So ignoring it for
width calculation like the old code did would be appropriate -- if we'd
encounter such an escape sequence in text to be shown in columns.

René

#3 lore raw

Hi René,

Quoted 12 lines · René Scharfe

Am 14.10.19 um 13:13 schrieb Johannes Schindelin:

Quoted 10 lines · René Scharfe
Quoted 8 lines

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.

Quoted 7 lines
Quoted 2 lines

Arguably, the "Erase in Line" thing should re-set the width to 0, no? But as you say, this is not needed for this patch.

It doesn't move the cursor, just clears the characters to the right, to the left or both sides, depending on its parameter. So ignoring it for
width calculation like the old code did would be appropriate -- if we'd
encounter such an escape sequence in text to be shown in columns.

Whoops, you're right. I brainfarted, mistaking it for \r... My bad!

Ciao,
Dscho