blob: 5c6bf6854f55032660004422c81227d43f43eaf0 [file] [log] [blame]
Andrew Wilkins4ffbbd72015-05-23 15:16:09 +00001package liner
2
3import "unicode"
4
5// These character classes are mostly zero width (when combined).
6// A few might not be, depending on the user's font. Fixing this
7// is non-trivial, given that some terminals don't support
8// ANSI DSR/CPR
9var zeroWidth = []*unicode.RangeTable{
10 unicode.Mn,
11 unicode.Me,
12 unicode.Cc,
13 unicode.Cf,
14}
15
16var doubleWidth = []*unicode.RangeTable{
17 unicode.Han,
18 unicode.Hangul,
19 unicode.Hiragana,
20 unicode.Katakana,
21}
22
23// countGlyphs considers zero-width characters to be zero glyphs wide,
24// and members of Chinese, Japanese, and Korean scripts to be 2 glyphs wide.
25func countGlyphs(s []rune) int {
26 n := 0
27 for _, r := range s {
28 switch {
29 case unicode.IsOneOf(zeroWidth, r):
30 case unicode.IsOneOf(doubleWidth, r):
31 n += 2
32 default:
33 n++
34 }
35 }
36 return n
37}
38
39func getPrefixGlyphs(s []rune, num int) []rune {
40 p := 0
41 for n := 0; n < num && p < len(s); p++ {
42 if !unicode.IsOneOf(zeroWidth, s[p]) {
43 n++
44 }
45 }
46 for p < len(s) && unicode.IsOneOf(zeroWidth, s[p]) {
47 p++
48 }
49 return s[:p]
50}
51
52func getSuffixGlyphs(s []rune, num int) []rune {
53 p := len(s)
54 for n := 0; n < num && p > 0; p-- {
55 if !unicode.IsOneOf(zeroWidth, s[p-1]) {
56 n++
57 }
58 }
59 return s[p:]
60}