tprint/table_header.go
2022-08-25 17:07:37 +02:00

38 lines
858 B
Go

package tprint
import (
"strings"
"git.milar.in/milarin/slices"
)
// TODO FormatHeaderTable 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 FormatHeaderTable(header string, table *Table) string {
b := new(strings.Builder)
tab := table.String()
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]
}