peterh / liner

Pure Go line editor with history, inspired by linenoise
MIT License
1.05k stars 132 forks source link

Add support for double-width (CJK) characters #43

Closed jnjackins closed 9 years ago

jnjackins commented 9 years ago

Tested on OS X and the latest Ubuntu, using the default terminal application for each, and from a tty on Ubuntu.

Sorry for all the diff noise in width_test.go. The actual test semantics only change in TestCountGlyphs, which compares against the pre-computed testCase.glyphs rather than the length of a slice of runes.

Fixes #42

peterh commented 9 years ago

Thanks for the pull request.

Pulled, on the basis that it's clean, it doesn't affect what I'm doing, and it should be an improvement on the status quo even though it doesn't catch every fullwidth character.

I'm just going to point out that unicode.Katakana includes some halfwidth Katakana characters (eg. \uFF66 through \uFF9D http://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms#In_Unicode ), so those glyphs regress with this patch.

jnjackins commented 9 years ago

Good point. Looking at that article, it looks like there is halfwidth Katakana and Hangul, halfwidth versions of punctuation that is normally fullwidth, and fullwidth versions of ASCII characters. I'm not certain about Hangul (Korean), but at least in Japanese the halfwidth Katakana characters are used quite rarely, and I've never seen halfwidth punctuation used. I'd guess Hangul is about the same. Fullwidth latin (ASCII) characters are occasionally used in Japanese text.

Perhaps we should wait until this bothers me or someone else, and look at teaching countGlyphs about the exceptions then.

Thanks for merging!

roachsinai commented 6 years ago

Hi, does the double-width problem for Chinese characters have been solved?

I have a Chinese character double-width problem. :-(

peterh commented 6 years ago

As far as I am aware, double width characters should work. What exactly is the problem you are having?

roachsinai commented 6 years ago

I'm using a commandline program BaiduPCS-Go. It's a Baidu Wangpan cli write use Go. To use BaiduPCS-Go I should login use Baidu ID. And then it will show prompt my ID included. And my ID include Chinese chars.

The problem is the cursor is left shift two spaces.

Oops, you're write. May be it's not problem about Chinese characters. Because I also use two special characters: and (『』apologize for my odd ID). Cause the policy of Baidu I can't change may Baidu ID.

jnjackins commented 6 years ago

I can reproduce the issue. Seems that

+var doubleWidth = []*unicode.RangeTable{
+   unicode.Han,
+   unicode.Hangul,
+   unicode.Hiragana,
+   unicode.Katakana,
+}

isn't enough.