tprint/table_header.go

45 lines
1.1 KiB
Go
Raw Permalink Normal View History

2022-08-25 16:55:19 +02:00
package tprint
import (
"strings"
"git.milar.in/milarin/slices"
)
2023-01-19 15:40:42 +01:00
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
2022-08-25 16:55:19 +02:00
// 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
2023-01-19 15:40:42 +01:00
func formatHeaderTableStr(header string, tab string) string {
2022-08-25 16:55:19 +02:00
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')
})
2022-08-25 17:07:37 +02:00
ret := b.String()
return ret[:len(ret)-1]
2022-08-25 16:55:19 +02:00
}