Compare commits

..

No commits in common. "main" and "v0.0.1" have entirely different histories.
main ... v0.0.1

7 changed files with 37 additions and 132 deletions

1
.gitignore vendored
View File

@ -1 +0,0 @@
table_header_test.go

View File

@ -1,4 +1,4 @@
package tprint package tableprint
type ColSpec struct { type ColSpec struct {
Name string Name string

2
go.mod
View File

@ -6,13 +6,11 @@ require (
git.milar.in/milarin/gmath v0.0.2 git.milar.in/milarin/gmath v0.0.2
git.milar.in/milarin/slices v0.0.3 git.milar.in/milarin/slices v0.0.3
github.com/fatih/color v1.13.0 github.com/fatih/color v1.13.0
github.com/mattn/go-runewidth v0.0.13
) )
require ( require (
git.milar.in/milarin/channel v0.0.9 // indirect git.milar.in/milarin/channel v0.0.9 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-isatty v0.0.14 // indirect
github.com/rivo/uniseg v0.3.4 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
) )

5
go.sum
View File

@ -11,11 +11,6 @@ github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.3.4 h1:3Z3Eu6FGHZWSfNKJTOUiPatWwfc7DzJRU04jFUqJODw=
github.com/rivo/uniseg v0.3.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=

View File

@ -1,4 +1,4 @@
package tprint package tableprint
import ( import (
"fmt" "fmt"
@ -6,6 +6,7 @@ import (
"git.milar.in/milarin/gmath" "git.milar.in/milarin/gmath"
"git.milar.in/milarin/slices" "git.milar.in/milarin/slices"
"github.com/fatih/color"
) )
type Table struct { type Table struct {
@ -21,7 +22,7 @@ type Table struct {
data [][][]string data [][][]string
} }
func NewTable(head ...string) *Table { func NewDynamicTable(head ...string) *Table {
thead := slices.Map(head, func(s string) []string { return strings.Split(s, "\n") }) thead := slices.Map(head, func(s string) []string { return strings.Split(s, "\n") })
return &Table{ return &Table{
head: thead, head: thead,
@ -60,58 +61,31 @@ func (t *Table) AddRow(row ...interface{}) {
} }
} }
func (t *Table) StringNoHead() string {
b := new(strings.Builder)
b.WriteRune('┏')
for i, colwidth := range t.colwidth {
b.WriteString(strings.Repeat("━", colwidth))
if i < len(t.colwidth)-1 {
b.WriteRune('┯')
}
}
b.WriteRune('┓')
b.WriteRune('\n')
for i, row := range t.data {
t.printRow(b, row, t.rowheight[i])
if t.rowSep && i < len(t.data)-1 {
t.addNextCellLine(b)
}
}
t.addLastCellLine(b)
return b.String()
}
func (t *Table) String() string { func (t *Table) String() string {
b := new(strings.Builder) b := new(strings.Builder)
b.WriteRune('') b.WriteRune('┌')
for i, colwidth := range t.colwidth { for i, colwidth := range t.colwidth {
b.WriteString(strings.Repeat("", colwidth)) b.WriteString(strings.Repeat("─", colwidth))
if i < len(t.colwidth)-1 { if i < len(t.colwidth)-1 {
b.WriteRune('') b.WriteRune('┬')
} }
} }
b.WriteRune('') b.WriteRune('┐')
b.WriteRune('\n') b.WriteRune('\n')
t.printRow(b, t.head, t.headheight) bold := color.New(color.Bold)
t.printRow(b, bold.Sprint, t.head, t.headheight)
b.WriteRune('┣') if len(t.data) > 0 {
for i, colwidth := range t.colwidth { t.addNextCellLine(b)
b.WriteString(strings.Repeat("━", colwidth)) } else {
if i < len(t.colwidth)-1 { t.addLastCellLine(b)
b.WriteRune('┿') return b.String()
}
} }
b.WriteRune('┫')
b.WriteRune('\n')
for i, row := range t.data { for i, row := range t.data {
t.printRow(b, row, t.rowheight[i]) t.printRow(b, fmt.Sprint, row, t.rowheight[i])
if t.rowSep && i < len(t.data)-1 { if t.rowSep && i < len(t.data)-1 {
t.addNextCellLine(b) t.addNextCellLine(b)
} }
@ -122,9 +96,9 @@ func (t *Table) String() string {
return b.String() return b.String()
} }
func (t *Table) printRow(b *strings.Builder, rowData [][]string, rowHeight int) { func (t *Table) printRow(b *strings.Builder, printFunc func(...interface{}) string, rowData [][]string, rowHeight int) {
for lineIndex := 0; lineIndex < rowHeight; lineIndex++ { for lineIndex := 0; lineIndex < rowHeight; lineIndex++ {
b.WriteRune('') b.WriteRune('')
for colIndex := 0; colIndex < t.maxcols; colIndex++ { for colIndex := 0; colIndex < t.maxcols; colIndex++ {
line := "" line := ""
@ -135,34 +109,33 @@ func (t *Table) printRow(b *strings.Builder, rowData [][]string, rowHeight int)
} }
} }
b.WriteString(fmt.Sprint(padStringRight(line, ' ', t.colwidth[colIndex]))) b.WriteString(printFunc(padStringRight(line, ' ', t.colwidth[colIndex])))
if colIndex < t.maxcols-1 { b.WriteRune('│')
b.WriteRune('│')
}
} }
b.WriteString("┃\n") b.WriteRune('\n')
} }
} }
func (t *Table) addNextCellLine(b *strings.Builder) { func (t *Table) addNextCellLine(b *strings.Builder) {
b.WriteRune('') b.WriteRune('')
for i, colwidth := range t.colwidth { for i, colwidth := range t.colwidth {
b.WriteString(strings.Repeat("─", colwidth)) b.WriteString(strings.Repeat("─", colwidth))
if i < len(t.colwidth)-1 { if i < len(t.colwidth)-1 {
b.WriteRune('┼') b.WriteRune('┼')
} }
} }
b.WriteRune('') b.WriteRune('')
b.WriteRune('\n') b.WriteRune('\n')
} }
func (t *Table) addLastCellLine(b *strings.Builder) { func (t *Table) addLastCellLine(b *strings.Builder) {
b.WriteRune('') b.WriteRune('')
for i, colwidth := range t.colwidth { for i, colwidth := range t.colwidth {
b.WriteString(strings.Repeat("", colwidth)) b.WriteString(strings.Repeat("", colwidth))
if i < len(t.colwidth)-1 { if i < len(t.colwidth)-1 {
b.WriteRune('') b.WriteRune('')
} }
} }
b.WriteString("┛\n") b.WriteRune('┘')
b.WriteRune('\n')
} }

View File

@ -1,44 +0,0 @@
package tprint
import (
"strings"
"git.milar.in/milarin/slices"
)
func FormatHeaderTable(header string, table *Table) string {
return formatHeaderTableStr(header, table.String())
}
func FormatHeaderTableNoHead(header string, table *Table) string {
return formatHeaderTableStr(header, table.StringNoHead())
}
// TODO formatHeaderTableStr is a quick hack
// a better solution would be to support nested tables.
// in that case, a header table is just a table with a singe col, single header row and single data row
func formatHeaderTableStr(header string, tab string) string {
b := new(strings.Builder)
splits := strings.Split(tab, "\n")
tabwidth := strLen(splits[0])
b.WriteRune('┏')
b.WriteString(strings.Repeat("━", tabwidth-2))
b.WriteString("┓\n┃")
b.WriteString(padStringCenter(header, ' ', tabwidth-2))
b.WriteString("┃\n")
b.WriteRune('┣')
b.WriteString(splits[0][3 : len(splits[0])-3])
b.WriteString("┫\n")
slices.Each(splits[1:], func(s string) {
b.WriteString(s)
b.WriteRune('\n')
})
ret := b.String()
return ret[:len(ret)-1]
}

View File

@ -1,10 +1,8 @@
package tprint package tableprint
import ( import (
"strings" "strings"
"unicode" "unicode"
"github.com/mattn/go-runewidth"
) )
func maxLengthStr(a, b string) string { func maxLengthStr(a, b string) string {
@ -25,24 +23,20 @@ func strLen(str string) int {
l := 0 l := 0
runes := []rune(str) runes := []rune(str)
colored := false
for i := 0; i < len(runes); i++ { for i := 0; i < len(runes); i++ {
rn := runes[i] if unicode.IsControl(runes[i]) {
if colored {
// skip control sequences i += 3
if unicode.IsControl(rn) { } else {
for j := i; j < len(runes)-1 && runes[j] != 'm'; j++ { i += 4
i = j
} }
i++ colored = !colored
continue continue
} }
if rn <= 0xFF || rn >= '─' && rn <= '╿' { l++
l++
} else {
l += runewidth.RuneWidth(rn)
}
} }
return l return l
@ -52,13 +46,3 @@ func padStringRight(str string, pad rune, length int) string {
padding := length - strLen(str) padding := length - strLen(str)
return str + strings.Repeat(string(pad), padding) return str + strings.Repeat(string(pad), padding)
} }
func padStringLeft(str string, pad rune, length int) string {
padding := length - strLen(str)
return strings.Repeat(string(pad), padding) + str
}
func padStringCenter(str string, pad rune, length int) string {
l := strLen(str)
return padStringLeft(padStringRight(str, pad, (length-l)/2+l), pad, length)
}