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, "…")) }