From 4331b2ac298e6358fdd48367197ff7eedc25dc38 Mon Sep 17 00:00:00 2001 From: milarin Date: Thu, 19 Jan 2023 15:40:42 +0100 Subject: [PATCH] added StringNoHead method --- .gitignore | 1 + table.go | 25 +++++++++++++++++++++++++ table_header.go | 13 ++++++++++--- 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8599d40 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +table_header_test.go diff --git a/table.go b/table.go index 899cac1..e028dd4 100644 --- a/table.go +++ b/table.go @@ -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 { b := new(strings.Builder) diff --git a/table_header.go b/table_header.go index 6322c48..8a6bfbc 100644 --- a/table_header.go +++ b/table_header.go @@ -6,14 +6,21 @@ import ( "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. // 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) - tab := table.String() splits := strings.Split(tab, "\n") tabwidth := strLen(splits[0])