diff --git a/width.go b/width.go index 42e8999..0395f3a 100644 --- a/width.go +++ b/width.go @@ -1,6 +1,10 @@ package liner -import "unicode" +import ( + "unicode" + + "github.com/mattn/go-runewidth" +) // These character classes are mostly zero width (when combined). // A few might not be, depending on the user's font. Fixing this @@ -13,13 +17,6 @@ var zeroWidth = []*unicode.RangeTable{ unicode.Cf, } -var doubleWidth = []*unicode.RangeTable{ - unicode.Han, - unicode.Hangul, - unicode.Hiragana, - unicode.Katakana, -} - // countGlyphs considers zero-width characters to be zero glyphs wide, // and members of Chinese, Japanese, and Korean scripts to be 2 glyphs wide. func countGlyphs(s []rune) int { @@ -31,13 +28,7 @@ func countGlyphs(s []rune) int { continue } - switch { - case unicode.IsOneOf(zeroWidth, r): - case unicode.IsOneOf(doubleWidth, r): - n += 2 - default: - n++ - } + n += runewidth.RuneWidth(r) } return n } @@ -49,17 +40,17 @@ func countMultiLineGlyphs(s []rune, columns int, start int) int { n++ continue } - switch { - case unicode.IsOneOf(zeroWidth, r): - case unicode.IsOneOf(doubleWidth, r): + switch runewidth.RuneWidth(r) { + case 0: + case 1: + n++ + case 2: n += 2 // no room for a 2-glyphs-wide char in the ending // so skip a column and display it at the beginning if n%columns == 1 { n++ } - default: - n++ } } return n diff --git a/width_test.go b/width_test.go index add779c..081306d 100644 --- a/width_test.go +++ b/width_test.go @@ -22,7 +22,7 @@ type testCase struct { var testCases = []testCase{ {[]rune("query"), 5}, {[]rune("私"), 2}, - {[]rune("hello世界"), 9}, + {[]rune("hello『世界』"), 13}, } func TestCountGlyphs(t *testing.T) {