Repository navigation
fix: count runes, not bytes, when wrapping help text - #2511
Conversation
|
LGTM. |
paoValle
left a comment
There was a problem hiding this comment.
Counting runes matches textWidth and the tabwriter, so wrapping and alignment now agree for multi-byte text. Two things before this merges: the new test only covers offset 0, while the diff also moves the spaceLeft arithmetic in the offset path, so a case with offset 4 and two multi-byte words would pin what it changed. And runes are not columns: 日本語 is three runes and six cells, so CJK help text now under-wraps where it used to over-wrap. Consistent with the tabwriter, so not wrong, but the comment could say "runes, not display columns" and save the next reader the assumption about terminal width.
wrapLine measured lines and words with len, so text with multi-byte characters wrapped well before the wrapAt column. Count runes, as offset and the tabwriter that aligns the help output already do. Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
b7c29d5 to
ca8f45a
Compare
Juneezee
left a comment
There was a problem hiding this comment.
I rebased this onto current main, applied abitrolly's suggestion, and updated the CJK sentence in the description. The suggestion computes wrapAt - offset once and doesn't change behavior.
I reproduced the example from the description on main, and the fix works. Changing any of the five rune counts back to len fails the new test. Offset only sets the line width, so I don't think the test needs an offset > 0 case.
On the CJK point above: wide characters now count as one column each. Korean usage text can run past wrapAt (the first line is 47 cells wide at wrapAt 30). Counting display width would need East Asian Width data, which the standard library doesn't have. offset and the tabwriter already count runes, so I'd keep this consistent with them:
Lines 577 to 581 in d854bb4
What type of PR is this?
What this PR does / why we need it:
With
wrapAtset,wrapLinemeasured lines and words in bytes, so help text with multi-byte characters wrapped before the configured column. AtwrapAt30,app - ändert die Größe für Überprüfungbroke after "Größe"; it now breaks after "für".offsetand the tabwriter that aligns help output already count runes (#2493), and this makes wrapping agree with them. Wide East Asian characters now count as one column each, as they do in the tabwriter, so text in those scripts can run pastwrapAt.Which issue(s) this PR fixes:
None.
Testing
TestWrapLineCountsRunesinhelp_test.go.Release Notes