From: Andrea Zagli Date: Sun, 22 Aug 2021 08:19:26 +0000 (+0200) Subject: Added customizable table header template. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=d93a1460cfdfd3dc96357b24990e20b16fac0908;p=zakgrid_go Added customizable table header template. --- diff --git a/main.go b/main.go index 0b4c734..9e684a0 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,9 @@ package main import ( "fmt" "log" + "bytes" + "text/template" + "database/sql" _ "github.com/mattn/go-sqlite3" ) @@ -16,8 +19,11 @@ type column struct { toCall missingCol } +var headerTmpl = "\n{{ . }}\n" + type grid struct { cols []column + headerTmpl string } func (g *grid) addColumn (c column) { @@ -25,16 +31,32 @@ func (g *grid) addColumn (c column) { } func (g *grid) getHeader () string { - header := "\n" + tmpl := g.headerTmpl + if tmpl == "" { + tmpl = headerTmpl + } + + t, err := template.New("header").Parse(tmpl) + if err != nil { + fmt.Println(err) + return "" + } + + header := "" for i, v := range g.cols { header += fmt.Sprintf("\t%s\n", v.title) fmt.Println(i, v) } - header += "\n" + var b bytes.Buffer + err = t.Execute(&b, header) + if err != nil { + fmt.Println(err) + return "" + } - return header + return b.String() } func (g *grid) getRowFromMap (m map[string]interface{}) string { @@ -72,6 +94,8 @@ func missing (row map[string]interface{}) interface{} { func main() { var g grid + g.headerTmpl = "\n{{ . }}\n" + c := column{name: "id", title: "ID"} g.addColumn(c)