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

[PATCH 2/2] builtin/repo: fix table alignment for UTF-8 characters

From
Jiang Xin <worldhello.net@gmail.com>
Date
Nov 14, 2025, 05:52 UTC
Message-ID
<a50bcde6446fbd87b4fb04b28c579a915457813a.1763098804.git.worldhello.net@gmail.com>
In-Reply-To
<cover.1763098804.git.worldhello.net@gmail.com>

The output table from "git repo structure" is misaligned when displaying UTF-8 characters (e.g., non-ASCII glyphs). E.g.:

    | 仓库结构   | 值  |
    | -------------- | ---- |
    | * 引用       |      |
    |   * 计数     |   67 |
    |     * 分支   |    6 |
    |     * 标签   |   30 |
    |     * 远程   |   19 |
    |     * 其它   |   12 |
    |                |      |
    | * 可达对象 |      |
    |   * 计数     | 2217 |
    |     * 提交   |  279 |
    |     * 树      |  740 |
    |     * 数据对象 | 1168 |
    |     * 标签   |   30 |

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.

Co-developed-by: Gemini <noreply@developers.google.com>
Signed-off-by: Jiang Xin <worldhello.net@gmail.com>
---
 builtin/repo.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
diff --git a/builtin/repo.c b/builtin/repo.c
index 9d4749f79b..d0b4a060b1 100644
--- a/builtin/repo.c
+++ b/builtin/repo.c
@@ -292,14 +292,21 @@ static void stats_table_print_structure(const struct stats_table *table)
 	int name_col_width = utf8_strwidth(name_col_title);
 	int value_col_width = utf8_strwidth(value_col_title);
 	struct string_list_item *item;
+	struct strbuf buf = STRBUF_INIT;
 
 	if (table->name_col_width > name_col_width)
 		name_col_width = table->name_col_width;
 	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);
+	strbuf_addstr(&buf, "| ");
+	strbuf_utf8_align(&buf, ALIGN_LEFT, name_col_width, name_col_title);
+	strbuf_addstr(&buf, " | ");
+	strbuf_utf8_align(&buf, ALIGN_LEFT, value_col_width, value_col_title);
+	strbuf_addstr(&buf, " |");
+	printf("%s\n", buf.buf);
+	strbuf_reset(&buf);
+
 	printf("| ");
 	for (int i = 0; i < name_col_width; i++)
 		putchar('-');
@@ -317,9 +324,16 @@ 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);
+		strbuf_reset(&buf);
+		strbuf_addstr(&buf, "| ");
+		strbuf_utf8_align(&buf, ALIGN_LEFT, name_col_width, item->string);
+		strbuf_addstr(&buf, " | ");
+		strbuf_utf8_align(&buf, ALIGN_RIGHT, value_col_width, value);
+		strbuf_addstr(&buf, " |");
+		printf("%s\n", buf.buf);
 	}
+
+	strbuf_release(&buf);
 }
 
 static void stats_table_clear(struct stats_table *table)
-- 
2.52.0.rc2.5.g4c20a63325.dirty
Previous: Jiang XinNext: Justin Tobler
Message 5 of 22 in “Fix misaligned output of git repo structure”
  1. 0/2 Fix misaligned output of git repo structureJiang Xin, Nov 14, 2025
  2. 1/2 t/unit-tests: add UTF-8 width tests for CJK charsJiang Xin, Nov 14, 2025
  3. Junio C HamanoNov 14, 2025
  4. Jiang XinNov 15, 2025
  5. 2/2 builtin/repo: fix table alignment for UTF-8 charactersJiang Xin, Nov 14, 2025
  6. Justin ToblerNov 14, 2025
  7. Jiang XinNov 15, 2025
  8. Junio C HamanoNov 14, 2025
  9. Jiang XinNov 15, 2025
  10. Junio C HamanoNov 15, 2025
  11. Jiang XinNov 16, 2025
  12. Junio C HamanoNov 16, 2025
  13. Kristoffer HaugsbakkNov 14, 2025
  14. Jiang XinNov 14, 2025
  15. Junio C HamanoNov 14, 2025
  16. Jiang XinNov 15, 2025
  17. Junio C HamanoNov 14, 2025
  18. 0/2 Fix misaligned output of git repo structureJiang Xin, Nov 15, 2025
  19. 1/2 t/unit-tests: add UTF-8 width tests for CJK charsJiang Xin, Nov 15, 2025
  20. 2/2 builtin/repo: fix table alignment for UTF-8 charactersJiang Xin, Nov 15, 2025
  21. Phillip WoodNov 15, 2025
  22. Junio C HamanoNov 15, 2025

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.