py_cui
py_cui copied to clipboard
Improve handling of multi-space characters
Describe the bug
When using certain multi-space unicode characters in certain terminal emulators, character lengths are incorrectly reported when using the len
function, which causes some incorrect rendering of terminal elements.
To Reproduce Steps to reproduce the behavior:
- Write py_cui interface that use the FOLDER and FILE unicode icons
- Run in windows terminal
- See invalid display of widgets
Expected behavior Rendering multi space unicode characters should not result in artefacting
Screenshots Will update later.
Environment:
- OS: Windows 10
- Terminal: windows-terminal
- Version: v0.1.3 (pre-release)
I encountered this when first started making audio player with CJK letters.
for i.e. normally 2-width characters such as all CJK letters permanently breaks alignments, even in Linux:
This is just as same as 2-width Unicode characters.
For that reason, I was not able to rely on built-in functions, as it turned out like this. I used a module wcwidth
to fix that. Full implementation code if interested.
Intenally, there is no way of knowing how wide a character is in python, so what wcwidth module doing is, manually create a gigantic table with all known Unicode character's width. Since Python3 internally process all strings in utf8 this works.
However, importing such model is huge breaking change. Therefore, I suggest either creating PR for Curses, or make this module optional. If wcwidth is available, use new drawing logic. Else use original drawing logic.
Will make a new PR if you approve this "Optional import" way, let me know!
I'll take a look at your code. I'd rather avoid any major breaking changes to avoid having to maintain too many variations of things like the renderer. I have some ideas of how to handle this I'll probably get around to it when I have some more time away from work.