github.com/prattmic/llgo-embedded@v0.0.0-20150820070356-41cfecea0e1e/third_party/liner/width.go (about) 1 package liner 2 3 import "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 9 var zeroWidth = []*unicode.RangeTable{ 10 unicode.Mn, 11 unicode.Me, 12 unicode.Cc, 13 unicode.Cf, 14 } 15 16 var 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. 25 func 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 39 func 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 52 func 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 }