Handle CJK widths

This commit is contained in:
Yasuhiro Matsumoto 2015-03-12 16:45:30 +09:00
parent 09dd8e88a4
commit 66f1319170

View File

@ -1,8 +1,7 @@
package termui
import "unicode/utf8"
import "strings"
import tm "github.com/nsf/termbox-go"
import rw "github.com/mattn/go-runewidth"
/* ---------------Port from termbox-go --------------------- */
@ -33,25 +32,12 @@ func toTmAttr(x Attribute) tm.Attribute {
}
func str2runes(s string) []rune {
n := utf8.RuneCountInString(s)
ss := strings.Split(s, "")
rs := make([]rune, n)
for i := 0; i < n; i++ {
r, _ := utf8.DecodeRuneInString(ss[i])
rs[i] = r
}
return rs
return []rune(s)
}
func trimStr2Runes(s string, w int) []rune {
rs := str2runes(s)
if w <= 0 {
return []rune{}
}
if len(rs) > w {
rs = rs[:w]
rs[w-1] = '…'
}
return rs
return []rune(rw.Truncate(s, w, "…"))
}