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

Re: [PATCH] fetch: align new ref summary printout in UTF-8 locales

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 3, 2012, 20:30 UTC
Message-ID
<7vk3waetnf.fsf@alter.siamese.dyndns.org>
In-Reply-To
<50450C8D.7010301@kdbg.org>
Johannes Sixt <j6t@kdbg.org> writes:
Show 22 quoted lines
> Am 03.09.2012 21:26, schrieb Junio C Hamano:
>> Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
>> 
>>> fetch does printf("%-*s", width, "foo") where "foo" can be an utf-8
>>> string, but width is bytes, not letters. This results in misaligned
>>> ref summary table.
>> 
>> "but width is bytes, not letters" is a misleading statement.
>> 
>> Be careful about three different quantities when talking about
>> aligning on terminal output with monospaced fonts:
>> 
>>  - How many bytes does the string occupy in memory?
>>  - How many unicode codepoints are in the string?
>>  - How many display columns does the string occupy on terminal?
>> 
>> Note that some "letters" (e.g. Han) occupy two display columns, and
>> you want to measure the "width" and compensate that for "bytes".
>> Letter count do not come into the picture for the purpose of aligning
>> the terminal output.
>
> If I'm reading POSIX correctly, printf measures the width in %*s in bytes.
Yes, that is the problem.
When we give allocated_width columns to display str, and call
        printf("%.*s", allocated_width, str)

we end up getting (allocated_width - strlen(str)) SPs as filler. If the str occupies less than strlen(str) columns (and it often is the case for a string with non-ascii characters), insufficient number of filler SPs will be given, and an item on the same line that comes after this printf() will appear too close to str. By measuring the required number of columns to display str, you can compensate. i.e.

        printf("%.*s", allocated_width +
                (strlen(str) - display_width(str)), str)
to give it enough filler.

I think the patch uses utf8_strwidth() which measures the display columns needed for the string, not number of unicode codepoints in the string. I was merely pointing out that the wording of the proposed log message "bytes, not letters" and the function name "foo_length" was misleading, when we mean "width".

Previous: Johannes SixtNext: Nguyen Thai Ngoc Duy
Message 4 of 14 in “fetch: align new ref summary printout in UTF-8 locales”
  1. fetch: align new ref summary printout in UTF-8 localesNguyễn Thái Ngọc Duy, Sep 3, 2012
  2. Junio C HamanoSep 3, 2012
  3. Johannes SixtSep 3, 2012
  4. Junio C HamanoSep 3, 2012
  5. Nguyen Thai Ngoc DuySep 4, 2012
  6. fetch: align new ref summary printout in UTF-8 localesNguyễn Thái Ngọc Duy, Sep 4, 2012
  7. Nguyen Thai Ngoc DuySep 12, 2012
  8. Junio C HamanoSep 12, 2012
  9. Torsten BögershausenSep 12, 2012
  10. Torsten BögershausenSep 5, 2012
  11. Nguyen Thai Ngoc DuySep 6, 2012
  12. Nguyen Thai Ngoc DuySep 6, 2012
  13. Torsten BögershausenSep 6, 2012
  14. Junio C HamanoSep 6, 2012

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.