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. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Oct 13, 2019 at 14:49 UTC a81e42d23588188c49f9b6e519f99a734a5aacb9
1 file changed +1 -12
column.c
+1 -12
@@ -23,18 +23,7 @@ struct column_data {
23 /* return length of 's' in letters, ANSI escapes stripped */
24 static int item_length(const char *s)
25 {
26 - int len, i = 0;
27 - struct strbuf str = STRBUF_INIT;
28 -
29 - strbuf_addstr(&str, s);
30 - while ((s = strstr(str.buf + i, "\033[")) != NULL) {
31 - int len = strspn(s + 2, "0123456789;");
32 - i = s - str.buf;
33 - strbuf_remove(&str, i, len + 3); /* \033[<len><func char> */
34 - }
35 - len = utf8_strwidth(str.buf);
36 - strbuf_release(&str);
37 - return len;
26 + return utf8_strnwidth(s, -1, 1);
27 }
28
29 /*