Handle CJK widths

This commit is contained in:
Yasuhiro Matsumoto 2015-03-12 16:45:30 +09:00
parent 09dd8e88a4
commit 66f1319170
1 changed files with 3 additions and 17 deletions

View File

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