public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
From: Phillip Wood <phillip.wood123@gmail•com>
To: Jiang Xin <worldhello.net@gmail•com>,
	Junio C Hamano <gitster@pobox•com>,
	Git List <git@vger•kernel.org>,
	Justin Tobler <jltobler@gmail•com>
Subject: Re: [PATCH v2 2/2] builtin/repo: fix table alignment for UTF-8 characters
Date: Sat, 15 Nov 2025 15:04:11 +0000	[thread overview]
Message-ID: <0eee1597-3e83-4a47-90a5-60942da01673@gmail.com> (raw)
In-Reply-To: <d0975427c9002ed28e6bbf18403034709f286a2c.1763213290.git.worldhello.net@gmail.com>

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);
  	}
  }
  


  reply	other threads:[~2025-11-15 15:04 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-14  5:52 [PATCH 0/2] Fix misaligned output of git repo structure Jiang Xin
2025-11-14  5:52 ` [PATCH 1/2] t/unit-tests: add UTF-8 width tests for CJK chars Jiang Xin
2025-11-14 20:17   ` Junio C Hamano
2025-11-15 12:38     ` Jiang Xin
2025-11-14  5:52 ` [PATCH 2/2] builtin/repo: fix table alignment for UTF-8 characters Jiang Xin
2025-11-14 17:50   ` Justin Tobler
2025-11-15 12:41     ` Jiang Xin
2025-11-14 20:00   ` Junio C Hamano
2025-11-15 12:54     ` Jiang Xin
2025-11-15 16:36       ` Junio C Hamano
2025-11-16 13:32         ` Jiang Xin
2025-11-16 16:51           ` Junio C Hamano
2025-11-14  7:41 ` [PATCH 0/2] Fix misaligned output of git repo structure Kristoffer Haugsbakk
2025-11-14  9:52   ` Jiang Xin
2025-11-14 19:22     ` Junio C Hamano
2025-11-15 12:25       ` Jiang Xin
2025-11-14 16:13 ` Junio C Hamano
2025-11-15 13:36 ` [PATCH v2 " Jiang Xin
2025-11-15 13:36   ` [PATCH v2 1/2] t/unit-tests: add UTF-8 width tests for CJK chars Jiang Xin
2025-11-15 13:36   ` [PATCH v2 2/2] builtin/repo: fix table alignment for UTF-8 characters Jiang Xin
2025-11-15 15:04     ` Phillip Wood [this message]
2025-11-15 16:49       ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0eee1597-3e83-4a47-90a5-60942da01673@gmail.com \
    --to=phillip.wood123@gmail$(echo .)com \
    --cc=git@vger$(echo .)kernel.org \
    --cc=gitster@pobox$(echo .)com \
    --cc=jltobler@gmail$(echo .)com \
    --cc=phillip.wood@dunelm$(echo .)org.uk \
    --cc=worldhello.net@gmail$(echo .)com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox