fix: a wide-character filename overflows the requested width - #613
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A filename containing wide characters (CJK, emoji) overflows the requested width,
because the name is measured in columns but truncated in characters.
A directory whose name is 26 CJK characters, with
dust -P -c -w 40. Measuringthe real display width of the output:
The 40-column request produced a 66-column line, 65% over. Verified the same way
at
-w 24,-w 40,-w 50and-w 60: after the change every line fitsexactly within the requested width.
Cause
maybe_trim_filenamemeasures withUnicodeWidthStr::widthand then cuts withchars().take(...):Each CJK character is one
charbut two columns, so takingmax_size - 2characters yields roughly twice that many columns. This is the path used when
bars are off (
-b, or when the tree is too wide for them).The fix
Accumulate characters while their widths fit in the budget, instead of counting
them. ASCII behaviour is unchanged, since there one character is one column, and
the existing
test_format_str_long_nameconfirms it.Verification
test_format_str_long_name_wide_charsasserts both the exact resulting stringand its rendered width, so an off-by-one would also fail it.
Reverting to
chars().take(max_size - 2)fails it:cargo testis 32 + 8 + 31 + 4 passed, 0 failed.cargo fmt --checkclean, andcargo clippyshows the same 4 pre-existing warnings as master.I kept a test here rather than going test-free as on #609: this replaces a single
expression with a loop, so its correctness does not read off the diff the way a
one-line config fix does.
Disclosure: written with AI assistance (Claude Code). I built binaries from master and from the patched tree and measured the real display width of their output, and ran the mutation check myself.