core/rawdb: make table more like tablewriter

This commit is contained in:
MariusVanDerWijden 2025-11-19 18:55:11 +01:00
parent 8927e9aca8
commit e8900180f8

View file

@ -86,6 +86,7 @@ func (t *Table) render() error {
rowSeparator := t.buildRowSeparator(widths) rowSeparator := t.buildRowSeparator(widths)
if len(t.headers) > 0 { if len(t.headers) > 0 {
fmt.Fprintln(t.out, rowSeparator)
t.printRow(t.headers, widths) t.printRow(t.headers, widths)
fmt.Fprintln(t.out, rowSeparator) fmt.Fprintln(t.out, rowSeparator)
} }
@ -97,6 +98,7 @@ func (t *Table) render() error {
if len(t.footer) > 0 { if len(t.footer) > 0 {
fmt.Fprintln(t.out, rowSeparator) fmt.Fprintln(t.out, rowSeparator)
t.printRow(t.footer, widths) t.printRow(t.footer, widths)
fmt.Fprintln(t.out, rowSeparator)
} }
return nil return nil
@ -169,21 +171,22 @@ func (t *Table) calculateColumnWidths() []int {
// //
// It generates a string with dashes (-) for each column width, joined by plus signs (+). // It generates a string with dashes (-) for each column width, joined by plus signs (+).
// //
// Example output: "----------+--------+-----------" // Example output: "+----------+--------+-----------+"
func (t *Table) buildRowSeparator(widths []int) string { func (t *Table) buildRowSeparator(widths []int) string {
parts := make([]string, len(widths)) parts := make([]string, len(widths))
for i, w := range widths { for i, w := range widths {
parts[i] = strings.Repeat("-", w) parts[i] = strings.Repeat("-", w)
} }
return strings.Join(parts, "+") return "+" + strings.Join(parts, "+") + "+"
} }
// printRow outputs a single row to the table writer. // printRow outputs a single row to the table writer.
// //
// Each cell is padded with spaces and separated by pipe characters (|). // Each cell is padded with spaces and separated by pipe characters (|).
// //
// Example output: " Database | Size | Items " // Example output: "| Database | Size | Items |"
func (t *Table) printRow(row []string, widths []int) { func (t *Table) printRow(row []string, widths []int) {
fmt.Fprintf(t.out, "|")
for i, cell := range row { for i, cell := range row {
if i > 0 { if i > 0 {
fmt.Fprint(t.out, "|") fmt.Fprint(t.out, "|")
@ -201,5 +204,6 @@ func (t *Table) printRow(row []string, widths []int) {
fmt.Fprintf(t.out, "%s%s%s", leftPadding, cell, rightPadding) fmt.Fprintf(t.out, "%s%s%s", leftPadding, cell, rightPadding)
} }
fmt.Fprintf(t.out, "|")
fmt.Fprintln(t.out) fmt.Fprintln(t.out)
} }