Skip to content

fix: a wide-character filename overflows the requested width - #613

Merged
bootandy merged 1 commit into
bootandy:masterfrom
VXNCXNX:fix/truncate-by-display-width
Aug 17, 2026
Merged

fix: a wide-character filename overflows the requested width#613
bootandy merged 1 commit into
bootandy:masterfrom
VXNCXNX:fix/truncate-by-display-width

Conversation

@VXNCXNX

@VXNCXNX VXNCXNX commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

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. Measuring
the real display width of the output:

before: 66 columns
after:  40 columns

The 40-column request produced a 66-column line, 65% over. Verified the same way
at -w 24, -w 40, -w 50 and -w 60: after the change every line fits
exactly within the requested width.

Cause

maybe_trim_filename measures with UnicodeWidthStr::width and then cuts with
chars().take(...):

if UnicodeWidthStr::width(&*name_in) > max_size {
    let name = name_in.chars().take(max_size - 2).collect::<String>();

Each CJK character is one char but two columns, so taking max_size - 2
characters 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_name confirms it.

Verification

test_format_str_long_name_wide_chars asserts both the exact resulting string
and its rendered width, so an off-by-one would also fail it.

Reverting to chars().take(max_size - 2) fails it:

assertion `left == right` failed
  left: "4.0Ki ┌─┴ ラウトは難しいですラウトは難し.."
 right: "4.0Ki ┌─┴ ラウトは難しい.."

cargo test is 32 + 8 + 31 + 4 passed, 0 failed. cargo fmt --check clean, and
cargo clippy shows 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.

@bootandy
bootandy merged commit 2c12db1 into bootandy:master Aug 17, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants