2015-03-06 09:39:31 +00:00
|
|
|
package liner
|
|
|
|
|
|
|
|
import "unicode"
|
|
|
|
|
|
|
|
// These character classes are mostly zero width (when combined).
|
|
|
|
// A few might not be, depending on the user's font. Fixing this
|
|
|
|
// is non-trivial, given that some terminals don't support
|
|
|
|
// ANSI DSR/CPR
|
|
|
|
var zeroWidth = []*unicode.RangeTable{
|
|
|
|
unicode.Mn,
|
|
|
|
unicode.Me,
|
|
|
|
unicode.Cc,
|
|
|
|
unicode.Cf,
|
|
|
|
}
|
|
|
|
|
2016-02-11 14:16:52 +00:00
|
|
|
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.
|
2015-03-06 09:39:31 +00:00
|
|
|
func countGlyphs(s []rune) int {
|
|
|
|
n := 0
|
|
|
|
for _, r := range s {
|
2016-11-03 18:51:19 +00:00
|
|
|
// speed up the common case
|
|
|
|
if r < 127 {
|
|
|
|
n++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-02-11 14:16:52 +00:00
|
|
|
switch {
|
|
|
|
case unicode.IsOneOf(zeroWidth, r):
|
|
|
|
case unicode.IsOneOf(doubleWidth, r):
|
|
|
|
n += 2
|
|
|
|
default:
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func countMultiLineGlyphs(s []rune, columns int, start int) int {
|
|
|
|
n := start
|
|
|
|
for _, r := range s {
|
2016-11-03 18:51:19 +00:00
|
|
|
if r < 127 {
|
|
|
|
n++
|
|
|
|
continue
|
|
|
|
}
|
2016-02-11 14:16:52 +00:00
|
|
|
switch {
|
|
|
|
case unicode.IsOneOf(zeroWidth, r):
|
|
|
|
case unicode.IsOneOf(doubleWidth, r):
|
|
|
|
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:
|
2015-03-06 09:39:31 +00:00
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPrefixGlyphs(s []rune, num int) []rune {
|
|
|
|
p := 0
|
|
|
|
for n := 0; n < num && p < len(s); p++ {
|
2016-11-03 18:51:19 +00:00
|
|
|
// speed up the common case
|
|
|
|
if s[p] < 127 {
|
|
|
|
n++
|
|
|
|
continue
|
|
|
|
}
|
2015-03-06 09:39:31 +00:00
|
|
|
if !unicode.IsOneOf(zeroWidth, s[p]) {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for p < len(s) && unicode.IsOneOf(zeroWidth, s[p]) {
|
|
|
|
p++
|
|
|
|
}
|
|
|
|
return s[:p]
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSuffixGlyphs(s []rune, num int) []rune {
|
|
|
|
p := len(s)
|
|
|
|
for n := 0; n < num && p > 0; p-- {
|
2016-11-03 18:51:19 +00:00
|
|
|
// speed up the common case
|
|
|
|
if s[p-1] < 127 {
|
|
|
|
n++
|
|
|
|
continue
|
|
|
|
}
|
2015-03-06 09:39:31 +00:00
|
|
|
if !unicode.IsOneOf(zeroWidth, s[p-1]) {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s[p:]
|
|
|
|
}
|