mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-17 20:41:03 +01:00
add support for template output mode with new option --templage <tmpl> (#47)
This commit is contained in:
@@ -25,8 +25,10 @@ import (
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"codeberg.org/scip/tablizer/cfg"
|
||||
"github.com/Masterminds/sprig/v3"
|
||||
"github.com/gookit/color"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/olekukonko/tablewriter/renderer"
|
||||
@@ -66,6 +68,8 @@ func printData(writer io.Writer, conf cfg.Config, data *Tabdata) {
|
||||
printJsonData(writer, data)
|
||||
case cfg.CSV:
|
||||
printCSVData(writer, conf, data)
|
||||
case cfg.Template:
|
||||
printTemplateData(writer, conf, data)
|
||||
default:
|
||||
printASCIIData(writer, conf, data)
|
||||
}
|
||||
@@ -388,3 +392,25 @@ func printCSVData(writer io.Writer, conf cfg.Config, data *Tabdata) {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func printTemplateData(writer io.Writer, conf cfg.Config, data *Tabdata) {
|
||||
tmpl, err := template.New("printer").Funcs(sprig.TxtFuncMap()).Parse(conf.Template)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to parse template: %s", err)
|
||||
}
|
||||
|
||||
buf := strings.Builder{}
|
||||
|
||||
for line, dict := range data.ToMap() {
|
||||
err = tmpl.Execute(&buf, dict)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to execute template in line %d: %s", line, err)
|
||||
}
|
||||
|
||||
buf.WriteString("\n")
|
||||
}
|
||||
|
||||
if _, err := fmt.Fprintln(writer, buf.String()); err != nil {
|
||||
log.Fatalf("failed to print output: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
Copyright © 2022-2024 Thomas von Dein
|
||||
Copyright © 2022-2025 Thomas von Dein
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,6 +17,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package lib
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// contains a whole parsed table
|
||||
type Tabdata struct {
|
||||
maxwidthHeader int // longest header
|
||||
@@ -34,3 +39,41 @@ func (data *Tabdata) CloneEmpty() Tabdata {
|
||||
|
||||
return newdata
|
||||
}
|
||||
|
||||
// convert our internal data to a list of maps
|
||||
func (data *Tabdata) ToMap() []map[string]any {
|
||||
dictlist := make([]map[string]any, len(data.entries))
|
||||
headers := make([]string, len(data.headers))
|
||||
|
||||
for idx, header := range data.headers {
|
||||
headers[idx] = strings.ToLower(header)
|
||||
}
|
||||
|
||||
for line, row := range data.entries {
|
||||
dict := make(map[string]any, len(data.headers))
|
||||
|
||||
for col, cell := range row {
|
||||
n, ok := strconv.Atoi(cell)
|
||||
if ok == nil {
|
||||
dict[headers[col]] = n
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.ToLower(cell) == "true" {
|
||||
dict[headers[col]] = true
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.ToLower(cell) == "false" {
|
||||
dict[headers[col]] = false
|
||||
continue
|
||||
}
|
||||
|
||||
dict[headers[col]] = cell
|
||||
}
|
||||
|
||||
dictlist[line] = dict
|
||||
}
|
||||
|
||||
return dictlist
|
||||
}
|
||||
Reference in New Issue
Block a user