Skip to content

fix(tui): truncate multibyte chars without panic - #3

Open
efanhome wants to merge 1 commit into
biliboss:mainfrom
efanhome:fix/tui-truncate-multibyte
Open

efanhome wants to merge 1 commit into
biliboss:mainfrom
efanhome:fix/tui-truncate-multibyte

Conversation

@efanhome

Copy link
Copy Markdown

Problem

The TUI panics when encountering filenames with multibyte Unicode characters (e.g. Chinese).

Repro: run disky tui on a directory containing paths like 【不忘初心】Windows10…iso — it hits:

thread 'main' panicked at src/tui/ui.rs:169:26:
end byte index 39 is not a char boundary; it is inside '性' (bytes 38..41)

Root cause: truncate() &s[..max] slices by byte index, which cuts inside 3-byte UTF-8 sequences.

Fix

  • Add unicode-width = "0.2" dependency
  • Rewrite truncate() using char_indices() + UnicodeWidthChar to respect char boundaries and CJK display width (2 cols)

All 91 tests pass.

The old implementation sliced by byte index (&s[..max]), which cut inside
multibyte UTF-8 characters like 性 (bytes 38..41) and panicked at runtime.

Use unicode-width to compute display width per character and break on
char boundaries instead.

- Add unicode-width 0.2 dependency
- Rewrite truncate() with char_indices() + UnicodeWidthChar
- Use UnicodeWidthStr for width calculation
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.

1 participant