added StringNoHead method

This commit is contained in:
milarin 2023-01-19 15:40:42 +01:00
parent 5be0ec3905
commit 4331b2ac29
3 changed files with 36 additions and 3 deletions

1
.gitignore vendored Normal file
View File

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

View File

@ -60,6 +60,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)

View File

@ -6,14 +6,21 @@ import (
"git.milar.in/milarin/slices" "git.milar.in/milarin/slices"
) )
// TODO FormatHeaderTable is a quick hack 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. // 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 // 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 { func formatHeaderTableStr(header string, tab string) string {
b := new(strings.Builder) b := new(strings.Builder)
tab := table.String()
splits := strings.Split(tab, "\n") splits := strings.Split(tab, "\n")
tabwidth := strLen(splits[0]) tabwidth := strLen(splits[0])