tprint/table_header.go

36 lines
832 B
Go
Raw Permalink Normal View History

2022-08-25 16:55:19 +02:00
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')
})
return b.String()
}