php-school / cli-menu

🖥 Build beautiful PHP CLI menus. Simple yet Powerful. Expressive DSL.
http://www.phpschool.io
MIT License
1.94k stars 107 forks source link

support full-width unicode characters #274

Closed mingtsay closed 1 year ago

mingtsay commented 1 year ago

Since full-width characters will break the UI, we should detect the visual width of the string using mb_strwidth instead of mb_strlen.

I replace all the mb_strlen into mb_strwidth because none of them are used to detecting the string length for word counting. They all are used to get the string width on the screen.

You may now use this commit to make CJK characters perfectly fit in the dialog. 😃

正體中文:由於 mb_strlen 是用來計算字串中的字元數量而非寬度,因此需將所有計算字串寬度的函數中的實作都改成使用 mb_strwidth 才不會造成跑版。現有的 mb_strlen 經人工檢視後發現,用途皆為計算寬度而非字數,因此我將該些實作全部從 mb_strlen 改成 mb_strwidth,如此 CJK 字元以及其他全型字元將不會造成跑版。

AydinHassan commented 1 year ago

Nice! looks good to me

AydinHassan commented 1 year ago

Don't suppose you know how to make it work with emojis? For example if you take a look at examples/crazy-redraw.php and replace:

$items[] = new LineBreakItem(array_rand(array_flip(['♥', '★', '^'])), rand(1, 3));

with:

$items[] = new LineBreakItem(array_rand(array_flip(['❤️', '★', '^'])), rand(1, 3));

It doesn't look good lol