Re: [PATCH v2 2/2] builtin/repo: fix table alignment for UTF-8 characters
From: Phillip Wood <hidden>
Date: 2025-11-15 15:04:18
Subsystem:
the rest · Maintainer:
Linus Torvalds
Hi Jiang On 15/11/2025 13:36, Jiang Xin wrote:
The output table from "git repo structure" is misaligned when displaying
UTF-8 characters (e.g., non-ASCII glyphs). E.g.:
| 仓库结构 | 值 |
| -------------- | ---- |
| * 引用 | |
| * 计数 | 67 |
The previous implementation used simple width formatting with printf()
which didn't properly handle multi-byte UTF-8 characters, causing
misaligned table columns when displaying repository structure
information.
This change modifies the stats_table_print_structure function to use
strbuf_utf8_align() instead of basic printf width specifiers. This
ensures proper column alignment regardless of the character encoding of
the content being displayed.How does it ensure proper column alignment for non-utf8 encodings? I don't see how it is possible to calculate the display width without knowing the encoding.
Also add test cases for strbuf_utf8_align(), a function newly introduced in "builtin/repo.c".
Nice. Using strbuf_utf8_align ends up being quite verbose. An alternative would be to keep using printf() but calculate the padding ourselves as shown below. Either way we end up calling utf8_strwidth() twice on the same string which is a bit of a shame but probably doesn't matter too much in the grand scheme of things. Thanks Phillip ---- 8< ---- builtin/repo.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/builtin/repo.c b/builtin/repo.c
index 9d4749f79be..1b139b89672 100644
--- a/builtin/repo.c
+++ b/builtin/repo.c@@ -298,8 +298,9 @@ static void stats_table_print_structure(const struct stats_table *table) if (table->value_col_width > value_col_width) value_col_width = table->value_col_width; - printf("| %-*s | %-*s |\n", name_col_width, name_col_title, - value_col_width, value_col_title); + printf("| %s%*s | %s%*s |\n", + name_col_title, name_col_width - utf8_strwidth(name_col_title), "", + value_col_title, value_col_width - utf8_strwidth(value_col_title), ""); printf("| "); for (int i = 0; i < name_col_width; i++) putchar('-');
@@ -317,8 +318,9 @@ static void stats_table_print_structure(const struct stats_table *table) value = entry->value; } - printf("| %-*s | %*s |\n", name_col_width, item->string, - value_col_width, value); + printf("| %s%*s | %*s%s |\n", + item->string, name_col_width - utf8_strwidth(item->string), "", + value_col_width - utf8_strwidth(value), "", value); } }