New Upstream Snapshot - golang-github-gosuri-uitable

Ready changes

Summary

Merged new upstream version: 0.0.4+git20220826.1.c2124e6 (was: 0.0~git20170830.36ee7e94).

Diff

diff --git a/.travis.yml b/.travis.yml
index 2139b89..26fc3b4 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -3,6 +3,4 @@ sudo: false
 install:
   - go get ./...
 go:
-  - 1.4
-  - 1.5.2
   - tip
diff --git a/README.md b/README.md
index 683b538..0dd32db 100644
--- a/README.md
+++ b/README.md
@@ -62,6 +62,3 @@ Bio:      Alan was a British pioneering computer scientist, mathematician, logic
 $ go get -v github.com/gosuri/uitable
 ```
 
-
-[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/gosuri/uitable/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
-
diff --git a/debian/changelog b/debian/changelog
index 85f7d1e..b69c321 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+golang-github-gosuri-uitable (0.0.4+git20220826.1.c2124e6-1) UNRELEASED; urgency=low
+
+  * New upstream snapshot.
+
+ -- Debian Janitor <janitor@jelmer.uk>  Wed, 08 Feb 2023 13:33:54 -0000
+
 golang-github-gosuri-uitable (0.0~git20170830.36ee7e94-2) unstable; urgency=medium
 
   [ Alexandre Viau ]
diff --git a/example/main.go b/example/main.go
index c59b2e7..ab6b534 100644
--- a/example/main.go
+++ b/example/main.go
@@ -3,6 +3,7 @@ package main
 import (
 	"fmt"
 
+	"github.com/fatih/color"
 	"github.com/gosuri/uitable"
 )
 
@@ -37,4 +38,16 @@ func main() {
 		table.AddRow("") // blank
 	}
 	fmt.Println(table)
+
+	fmt.Print("\n==> Multicolor Support\n")
+	table = uitable.New()
+	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("") // blank
+	}
+	fmt.Println(table)
 }
diff --git a/table.go b/table.go
index b792c88..b764e51 100644
--- a/table.go
+++ b/table.go
@@ -6,9 +6,9 @@ import (
 	"strings"
 	"sync"
 
+	"github.com/fatih/color"
 	"github.com/gosuri/uitable/util/strutil"
 	"github.com/gosuri/uitable/util/wordwrap"
-	"github.com/mattn/go-runewidth"
 )
 
 // Separator is the default column seperator
@@ -156,7 +156,7 @@ func (r *Row) String() string {
 		}
 		lines[x] = strutil.Join(line, r.Separator)
 	}
-	return strutil.Join(lines, "\n")
+	return strings.Join(lines, "\n")
 }
 
 // Cell represents a column in a row
@@ -178,7 +178,7 @@ type Cell struct {
 func (c *Cell) LineWidth() uint {
 	width := 0
 	for _, s := range strings.Split(c.String(), "\n") {
-		w := runewidth.StringWidth(s)
+		w := strutil.StringWidth(s)
 		if w > width {
 			width = w
 		}
@@ -191,7 +191,9 @@ func (c *Cell) String() string {
 	if c.Data == nil {
 		return strutil.PadLeft(" ", int(c.Width), ' ')
 	}
-	s := fmt.Sprintf("%v", c.Data)
+	col := color.New(color.FgBlack)
+	col.DisableColor()
+	s := fmt.Sprintf("%v", col.Sprint(c.Data))
 	if c.Width > 0 {
 		if c.Wrap && uint(len(s)) > c.Width {
 			return wordwrap.WrapString(s, c.Width)
diff --git a/util/strutil/strutil.go b/util/strutil/strutil.go
index cb35bce..53f30a8 100644
--- a/util/strutil/strutil.go
+++ b/util/strutil/strutil.go
@@ -3,12 +3,18 @@ package strutil
 
 import (
 	"bytes"
+	"regexp"
+
 	"github.com/mattn/go-runewidth"
 )
 
+const ansi = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"
+
+var re = regexp.MustCompile(ansi)
+
 // PadRight 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 {
-	slen := runewidth.StringWidth(str)
+	slen := StringWidth(str)
 	if slen >= length {
 		return str
 	}
@@ -21,7 +27,7 @@ func PadRight(str string, length int, pad byte) string {
 
 // PadLeft 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 {
-	slen := runewidth.StringWidth(str)
+	slen := StringWidth(str)
 	if slen >= length {
 		return str
 	}
@@ -36,7 +42,7 @@ 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, rightAlign bool) string {
-	slen := runewidth.StringWidth(s)
+	slen := StringWidth(s)
 	n := int(length)
 	if slen == n {
 		return s
@@ -53,7 +59,7 @@ func Resize(s string, length uint, rightAlign bool) string {
 		w := 0
 		for _, r := range rs {
 			buf.WriteRune(r)
-			rw := runewidth.RuneWidth(r)
+			rw := RuneWidth(r)
 			if w+rw >= n-3 {
 				break
 			}
@@ -65,8 +71,12 @@ func Resize(s string, length uint, rightAlign bool) string {
 	return s
 }
 
-// Join joins the list of the string with the delim provided
+// Join joins the list of the string with the delim provided.
+// Returns an empty string for empty list
 func Join(list []string, delim string) string {
+	if len(list) == 0 {
+		return ""
+	}
 	var buf bytes.Buffer
 	for i := 0; i < len(list)-1; i++ {
 		buf.WriteString(list[i] + delim)
@@ -74,3 +84,18 @@ func Join(list []string, delim string) string {
 	buf.WriteString(list[len(list)-1])
 	return buf.String()
 }
+
+// Strip strips the string of all colors
+func Strip(s string) string {
+	return re.ReplaceAllString(s, "")
+}
+
+// StringWidth returns the actual width of the string without colors
+func StringWidth(s string) int {
+	return runewidth.StringWidth(Strip(s))
+}
+
+// RuneWidth returns the actual width of the rune
+func RuneWidth(s rune) int {
+	return runewidth.RuneWidth(s)
+}
diff --git a/util/strutil/strutil_test.go b/util/strutil/strutil_test.go
index e7356d0..481660e 100644
--- a/util/strutil/strutil_test.go
+++ b/util/strutil/strutil_test.go
@@ -37,6 +37,13 @@ func TestJoin(t *testing.T) {
 	}
 }
 
+func TestJoin_blank(t *testing.T) {
+	got := Join([]string{}, ",")
+	if got != "" {
+		t.Fatal("want", "", "got", got)
+	}
+}
+
 func TestPadRight(t *testing.T) {
 	got := PadRight("foo", 5, '-')
 	if got != "foo--" {
diff --git a/util/wordwrap/wordwrap.go b/util/wordwrap/wordwrap.go
index e64dbc8..b2c63b8 100644
--- a/util/wordwrap/wordwrap.go
+++ b/util/wordwrap/wordwrap.go
@@ -3,8 +3,9 @@ package wordwrap
 
 import (
 	"bytes"
-	"github.com/mattn/go-runewidth"
 	"unicode"
+
+	"github.com/gosuri/uitable/util/strutil"
 )
 
 // WrapString wraps the given string within lim width in characters.
@@ -30,7 +31,7 @@ func WrapString(s string, lim uint) string {
 				} else {
 					current += uint(spaceWidth)
 					spaceBuf.WriteTo(buf)
-					spaceWidth += runewidth.StringWidth(buf.String())
+					spaceWidth += strutil.StringWidth(buf.String())
 				}
 				spaceBuf.Reset()
 				spaceWidth = 0
@@ -57,10 +58,10 @@ func WrapString(s string, lim uint) string {
 			}
 
 			spaceBuf.WriteRune(char)
-			spaceWidth += runewidth.RuneWidth(char)
+			spaceWidth += strutil.RuneWidth(char)
 		} else {
 			wordBuf.WriteRune(char)
-			wordWidth += runewidth.RuneWidth(char)
+			wordWidth += strutil.RuneWidth(char)
 
 			if current+uint(spaceWidth+wordWidth) > lim && uint(wordWidth) < lim {
 				buf.WriteRune('\n')

More details

Full run details

Historical runs