Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/jnjackins/liner
Browse files Browse the repository at this point in the history
  • Loading branch information
peterh committed May 4, 2018
2 parents 6106ee4 + c68d746 commit 80ce870
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
31 changes: 11 additions & 20 deletions 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
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion width_test.go
Expand Up @@ -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) {
Expand Down

0 comments on commit 80ce870

Please sign in to comment.