Wide characters are not supported.
mattn opened this issue · 1 comments
mattn commented
I have patch but it make changes for strutil. If you allow, I'll send p-r.
diff --git a/table.go b/table.go
index 705950d..c4df2e3 100644
--- a/table.go
+++ b/table.go
@@ -7,6 +7,7 @@ import (
"github.com/gosuri/uitable/util/strutil"
"github.com/gosuri/uitable/util/wordwrap"
+ "github.com/mattn/go-runewidth"
)
var (
@@ -153,8 +154,9 @@ type Cell struct {
func (c *Cell) LineWidth() uint {
width := 0
for _, s := range strings.Split(c.String(), "\n") {
- if len(s) > width {
- width = len(s)
+ w := runewidth.StringWidth(s)
+ if w > width {
+ width = w
}
}
return uint(width)
diff --git a/util/strutil/strutil.go b/util/strutil/strutil.go
index 25c4424..275a49d 100644
--- a/util/strutil/strutil.go
+++ b/util/strutil/strutil.go
@@ -3,15 +3,17 @@ package strutil
import (
"bytes"
+ "github.com/mattn/go-runewidth"
)
// Returns a new string of a specified length in which the end of the current string is padded with spaces or with a specified Unicode character.
func PadRight(str string, length int, pad byte) string {
- if len(str) >= length {
+ slen := runewidth.StringWidth(str)
+ if slen >= length {
return str
}
buf := bytes.NewBufferString(str)
- for i := 0; i < length-len(str); i++ {
+ for i := 0; i < length-slen; i++ {
buf.WriteByte(pad)
}
return buf.String()
@@ -19,11 +21,12 @@ func PadRight(str string, length int, pad byte) string {
// Returns a new string of a specified length in which the beginning of the current string is padded with spaces or with a specified Unicode character.
func PadLeft(str string, length int, pad byte) string {
- if len(str) >= length {
+ slen := runewidth.StringWidth(str)
+ if slen >= length {
return str
}
var buf bytes.Buffer
- for i := 0; i < length-len(str); i++ {
+ for i := 0; i < length-slen; i++ {
buf.WriteByte(pad)
}
buf.WriteString(str)
@@ -33,13 +36,14 @@ func PadLeft(str string, length int, pad byte) string {
// Resize resizes the string with the given length. It ellipses with '...' when the string's length exceeds
// the desired length or pads spaces to the right of the string when length is smaller than desired
func Resize(s string, length uint) string {
+ slen := runewidth.StringWidth(s)
n := int(length)
- if len(s) == n {
+ if slen == n {
return s
}
// Pads only when length of the string smaller than len needed
s = PadRight(s, n, ' ')
- if len(s) > n {
+ if slen > n {
b := []byte(s)
var buf bytes.Buffer
for i := 0; i < n-3; i++ {
gosuri commented
Please do!