From 8c9df1809b506c363495264c20fbae948583ff53 Mon Sep 17 00:00:00 2001 From: FogDong Date: Thu, 7 Apr 2022 12:02:37 +0800 Subject: [PATCH 1/2] Fix: fix multi color wrapping Signed-off-by: FogDong --- example/main.go | 12 ++++++------ table.go | 7 ++----- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/example/main.go b/example/main.go index ab6b534..794aafc 100644 --- a/example/main.go +++ b/example/main.go @@ -32,9 +32,9 @@ func main() { table.MaxColWidth = 80 table.Wrap = true for _, hacker := range hackers { - table.AddRow("Name:", hacker.Name) - table.AddRow("Birthday:", hacker.Birthday) - table.AddRow("Bio:", hacker.Bio) + table.AddRow("1. Name:", hacker.Name) + table.AddRow("2. Birthday:", hacker.Birthday) + table.AddRow("3. Bio:", hacker.Bio) table.AddRow("") // blank } fmt.Println(table) @@ -44,9 +44,9 @@ func main() { table.MaxColWidth = 80 table.Wrap = true for _, hacker := range hackers { - table.AddRow(color.RedString("Name:"), color.WhiteString(hacker.Name)) - table.AddRow(color.BlueString("Birthday:"), hacker.Birthday) - table.AddRow(color.GreenString("Bio:"), hacker.Bio) + table.AddRow(color.RedString("1. Name:"), color.WhiteString(hacker.Name)) + table.AddRow(color.BlueString("2. Birthday:"), hacker.Birthday) + table.AddRow(color.GreenString("3. Bio:"), hacker.Bio) table.AddRow("") // blank } fmt.Println(table) diff --git a/table.go b/table.go index b764e51..2722ad0 100644 --- a/table.go +++ b/table.go @@ -6,7 +6,6 @@ import ( "strings" "sync" - "github.com/fatih/color" "github.com/gosuri/uitable/util/strutil" "github.com/gosuri/uitable/util/wordwrap" ) @@ -191,11 +190,9 @@ func (c *Cell) String() string { if c.Data == nil { return strutil.PadLeft(" ", int(c.Width), ' ') } - col := color.New(color.FgBlack) - col.DisableColor() - s := fmt.Sprintf("%v", col.Sprint(c.Data)) + s := fmt.Sprint(c.Data) if c.Width > 0 { - if c.Wrap && uint(len(s)) > c.Width { + if c.Wrap && uint(strutil.StringWidth(s)) > c.Width { return wordwrap.WrapString(s, c.Width) } else { return strutil.Resize(s, c.Width, c.RightAlign) From dd706cfab7a52b1b7eb7907b5c867751af970b3d Mon Sep 17 00:00:00 2001 From: FogDong Date: Fri, 8 Apr 2022 11:55:45 +0800 Subject: [PATCH 2/2] Fix: fix color max length in wrap string Signed-off-by: FogDong --- util/wordwrap/wordwrap.go | 1 + 1 file changed, 1 insertion(+) diff --git a/util/wordwrap/wordwrap.go b/util/wordwrap/wordwrap.go index b2c63b8..4aa0c94 100644 --- a/util/wordwrap/wordwrap.go +++ b/util/wordwrap/wordwrap.go @@ -23,6 +23,7 @@ func WrapString(s string, lim uint) string { var wordBuf, spaceBuf bytes.Buffer var wordWidth, spaceWidth int + lim = lim + uint(len(s)) - uint(strutil.StringWidth(s)) for _, char := range s { if char == '\n' { if wordBuf.Len() == 0 {