Skip to content

Commit f099d6e

Browse files
committed
builtin/repo: fix table alignment for UTF-8 characters
The previous implementation used simple width formatting 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. Specifically: - Replace printf width formatting with strbuf_utf8_align() calls for both header and data rows - Initialize a strbuf to handle UTF-8 aware alignment - Reset the buffer between operations to avoid content contamination - Release the buffer when finished to prevent memory leaks Change-Id: I7a1bc60eaa97ce3e359f2a5e729042a3417c93f1 Co-developed-by: Gemini <noreply@developers.google.com> Signed-off-by: Jiang Xin <worldhello.net@gmail.com>
1 parent 625c781 commit f099d6e

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

builtin/repo.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,21 @@ static void stats_table_print_structure(const struct stats_table *table)
292292
int name_col_width = utf8_strwidth(name_col_title);
293293
int value_col_width = utf8_strwidth(value_col_title);
294294
struct string_list_item *item;
295+
struct strbuf buf = STRBUF_INIT;
295296

296297
if (table->name_col_width > name_col_width)
297298
name_col_width = table->name_col_width;
298299
if (table->value_col_width > value_col_width)
299300
value_col_width = table->value_col_width;
300301

301-
printf("| %-*s | %-*s |\n", name_col_width, name_col_title,
302-
value_col_width, value_col_title);
302+
strbuf_addstr(&buf, "| ");
303+
strbuf_utf8_align(&buf, ALIGN_LEFT, name_col_width, name_col_title);
304+
strbuf_addstr(&buf, " | ");
305+
strbuf_utf8_align(&buf, ALIGN_LEFT, value_col_width, value_col_title);
306+
strbuf_addstr(&buf, " |");
307+
printf("%s\n", buf.buf);
308+
strbuf_reset(&buf);
309+
303310
printf("| ");
304311
for (int i = 0; i < name_col_width; i++)
305312
putchar('-');
@@ -317,9 +324,16 @@ static void stats_table_print_structure(const struct stats_table *table)
317324
value = entry->value;
318325
}
319326

320-
printf("| %-*s | %*s |\n", name_col_width, item->string,
321-
value_col_width, value);
327+
strbuf_reset(&buf);
328+
strbuf_addstr(&buf, "| ");
329+
strbuf_utf8_align(&buf, ALIGN_LEFT, name_col_width, item->string);
330+
strbuf_addstr(&buf, " | ");
331+
strbuf_utf8_align(&buf, ALIGN_LEFT, value_col_width, value);
332+
strbuf_addstr(&buf, " |");
333+
printf("%s\n", buf.buf);
322334
}
335+
336+
strbuf_release(&buf);
323337
}
324338

325339
static void stats_table_clear(struct stats_table *table)

0 commit comments

Comments
 (0)