optimize ascii tables, use custom style instead of tab inserters (#53)

* optimize ascii tables, use custom style instead of tab inserters
This commit is contained in:
T.v.Dein
2025-06-10 16:12:03 +02:00
committed by GitHub
parent 787178b17e
commit 5168b04339
3 changed files with 7 additions and 41 deletions

View File

@@ -28,7 +28,7 @@ import (
) )
const DefaultSeparator string = `(\s\s+|\t)` const DefaultSeparator string = `(\s\s+|\t)`
const Version string = "v1.4.2" const Version string = "v1.4.3"
const MAXPARTS = 2 const MAXPARTS = 2
var DefaultConfigfile = os.Getenv("HOME") + "/.config/tablizer/config" var DefaultConfigfile = os.Getenv("HOME") + "/.config/tablizer/config"

View File

@@ -34,39 +34,3 @@ func (data *Tabdata) CloneEmpty() Tabdata {
return newdata return newdata
} }
// add a TAB (\t) in front of every cell, but not the first
func (data *Tabdata) TabEntries() [][]string {
newentries := make([][]string, len(data.entries))
for rowidx, row := range data.entries {
newentries[rowidx] = make([]string, len(row))
for colidx, cell := range row {
switch colidx {
case 0:
newentries[rowidx][colidx] = cell
default:
newentries[rowidx][colidx] = "\t" + cell
}
}
}
return newentries
}
// add a TAB (\t) in front of every header, but not the first
func (data *Tabdata) TabHeaders() []string {
newheaders := make([]string, len(data.headers))
for colidx, cell := range data.headers {
switch colidx {
case 0:
newheaders[colidx] = cell
default:
newheaders[colidx] = "\t" + cell
}
}
return newheaders
}

View File

@@ -196,13 +196,15 @@ Simple ASCII table without any borders etc, just like the input we expect
func printASCIIData(writer io.Writer, conf cfg.Config, data *Tabdata) { func printASCIIData(writer io.Writer, conf cfg.Config, data *Tabdata) {
tableString := &strings.Builder{} tableString := &strings.Builder{}
styleTSV := tw.NewSymbolCustom("space").WithColumn("\t")
table := tablewriter.NewTable(tableString, table := tablewriter.NewTable(tableString,
tablewriter.WithRenderer( tablewriter.WithRenderer(
renderer.NewBlueprint(tw.Rendition{ renderer.NewBlueprint(tw.Rendition{
Borders: tw.BorderNone, Borders: tw.BorderNone,
Symbols: tw.NewSymbols(tw.StyleASCII), Symbols: styleTSV,
Settings: tw.Settings{ Settings: tw.Settings{
Separators: tw.Separators{BetweenRows: tw.Off, BetweenColumns: tw.Off}, Separators: tw.Separators{BetweenRows: tw.Off, BetweenColumns: tw.On},
Lines: tw.Lines{ShowFooterLine: tw.Off, ShowHeaderLine: tw.Off}, Lines: tw.Lines{ShowFooterLine: tw.Off, ShowHeaderLine: tw.Off},
}, },
})), })),
@@ -231,10 +233,10 @@ func printASCIIData(writer io.Writer, conf cfg.Config, data *Tabdata) {
) )
if !conf.NoHeaders { if !conf.NoHeaders {
table.Header(data.TabHeaders()) table.Header(data.headers)
} }
if err := table.Bulk(data.TabEntries()); err != nil { if err := table.Bulk(data.entries); err != nil {
log.Fatalf("Failed to add data to table renderer: %s", err) log.Fatalf("Failed to add data to table renderer: %s", err)
} }