From 3fd2e6ac2f5499cf70fea5ffcd0955b2499993db Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Mon, 3 Oct 2022 13:10:23 +0200 Subject: [PATCH] use pointer of Tabdata, so there's only 1 copy around --- lib/helpers.go | 10 ++++++++++ lib/io.go | 2 +- lib/printer.go | 25 ++++++++----------------- lib/printer_test.go | 2 +- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/lib/helpers.go b/lib/helpers.go index 0ed1ed0..9498b1a 100644 --- a/lib/helpers.go +++ b/lib/helpers.go @@ -76,3 +76,13 @@ func PrepareModeFlags() error { return nil } + +func trimRow(row []string) []string { + // FIXME: remove this when we only use Tablewriter and strip in ParseFile()! + var fixedrow []string + for _, cell := range row { + fixedrow = append(fixedrow, strings.TrimSpace(cell)) + } + + return fixedrow +} diff --git a/lib/io.go b/lib/io.go index 4d70b94..adab95b 100644 --- a/lib/io.go +++ b/lib/io.go @@ -35,7 +35,7 @@ func ProcessFiles(args []string) error { if err != nil { return err } - printData(data) + printData(&data) } return nil diff --git a/lib/printer.go b/lib/printer.go index 2f6a056..c045c43 100644 --- a/lib/printer.go +++ b/lib/printer.go @@ -25,14 +25,15 @@ import ( "strings" ) -func printData(data Tabdata) { - // prepare headers - // FIXME: maybe do this already in parseFile()? +func printData(data *Tabdata) { + // prepare headers: add numbers to headers if !NoNumbering { numberedHeaders := []string{} for i, head := range data.headers { if len(Columns) > 0 { + // -c specified if !contains(UseColumns, i+1) { + // ignore this one continue } } @@ -73,20 +74,10 @@ func printData(data Tabdata) { } } -func trimRow(row []string) []string { - // FIXME: remove this when we only use Tablewriter and strip in ParseFile()! - var fixedrow []string - for _, cell := range row { - fixedrow = append(fixedrow, strings.TrimSpace(cell)) - } - - return fixedrow -} - /* Emacs org-mode compatible table (also orgtbl-mode) */ -func printOrgmodeData(data Tabdata) { +func printOrgmodeData(data *Tabdata) { tableString := &strings.Builder{} table := tablewriter.NewWriter(tableString) @@ -118,7 +109,7 @@ func printOrgmodeData(data Tabdata) { /* Markdown table */ -func printMarkdownData(data Tabdata) { +func printMarkdownData(data *Tabdata) { table := tablewriter.NewWriter(os.Stdout) table.SetHeader(data.headers) @@ -136,7 +127,7 @@ func printMarkdownData(data Tabdata) { /* Simple ASCII table without any borders etc, just like the input we expect */ -func printAsciiData(data Tabdata) { +func printAsciiData(data *Tabdata) { table := tablewriter.NewWriter(os.Stdout) table.SetHeader(data.headers) @@ -164,7 +155,7 @@ func printAsciiData(data Tabdata) { /* We simulate the \x command of psql (the PostgreSQL client) */ -func printExtendedData(data Tabdata) { +func printExtendedData(data *Tabdata) { // needed for data output format := fmt.Sprintf("%%%ds: %%s\n", data.maxwidthHeader) // FIXME: re-calculate if -c has been set diff --git a/lib/printer_test.go b/lib/printer_test.go index 2131156..9530574 100644 --- a/lib/printer_test.go +++ b/lib/printer_test.go @@ -60,7 +60,7 @@ asd igig cxxxncnc t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, data) } - printData(data) + printData(&data) buf := make([]byte, 1024) n, err := r.Read(buf)