From 66f131917065b77f1aed95883f8416de40bfd752 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Thu, 12 Mar 2015 16:45:30 +0900 Subject: [PATCH] Handle CJK widths --- helper.go | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/helper.go b/helper.go index 0bb316f..2a7d6b1 100644 --- a/helper.go +++ b/helper.go @@ -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, "…")) }