From b9ed7d8cb72ef76176cfbc84dd7adf70df2a7428 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Tue, 11 Oct 2022 09:11:46 +0200 Subject: [PATCH] fixed -X output in combination with -c --- go.mod | 2 +- lib/common.go | 30 +++++++++++++++++++++++++++--- lib/helpers.go | 15 ++++++++++++++- lib/io.go | 4 ++++ lib/printer.go | 14 +++----------- 5 files changed, 49 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index 2e2c57e..5568c4a 100644 --- a/go.mod +++ b/go.mod @@ -7,12 +7,12 @@ require ( github.com/gookit/color v1.5.2 github.com/olekukonko/tablewriter v0.0.5 github.com/spf13/cobra v1.5.0 + github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 ) require ( github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/mattn/go-runewidth v0.0.9 // indirect github.com/spf13/pflag v1.0.5 // indirect - github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 // indirect ) diff --git a/lib/common.go b/lib/common.go index c1e9b33..e7fcdec 100644 --- a/lib/common.go +++ b/lib/common.go @@ -17,6 +17,11 @@ along with this program. If not, see . package lib +import ( + "github.com/gookit/color" + //"github.com/xo/terminfo" +) + var ( // command line flags Debug bool @@ -35,11 +40,30 @@ var ( InvertMatch bool Pattern string - // FIXME: make configurable somehow, config file or ENV - MatchFG string = "black" // see https://github.com/gookit/color - MatchBG string = "green" + /* + FIXME: make configurable somehow, config file or ENV + see https://github.com/gookit/color will be set by + io.ProcessFiles() according to currently supported + color mode. + */ + MatchFG string + MatchBG string NoColor bool + // colors to be used per supported color mode + Colors = map[color.Level]map[string]string{ + color.Level16: map[string]string{ + "bg": "green", "fg": "black", + }, + color.Level256: map[string]string{ + "bg": "lightGreen", "fg": "black", + }, + color.LevelRgb: map[string]string{ + // FIXME: maybe use something nicer + "bg": "lightGreen", "fg": "black", + }, + } + // used for validation validOutputmodes = "(orgtbl|markdown|extended|ascii)" diff --git a/lib/helpers.go b/lib/helpers.go index 38c109a..3c4c492 100644 --- a/lib/helpers.go +++ b/lib/helpers.go @@ -53,7 +53,10 @@ func PrepareColumns() error { func numberizeHeaders(data *Tabdata) { // prepare headers: add numbers to headers numberedHeaders := []string{} + maxwidth := 0 // start from scratch, so we only look at displayed column widths + for i, head := range data.headers { + headlen := 0 if len(Columns) > 0 { // -c specified if !contains(UseColumns, i+1) { @@ -63,11 +66,21 @@ func numberizeHeaders(data *Tabdata) { } if NoNumbering { numberedHeaders = append(numberedHeaders, head) + headlen = len(head) } else { - numberedHeaders = append(numberedHeaders, fmt.Sprintf("%s(%d)", head, i+1)) + numhead := fmt.Sprintf("%s(%d)", head, i+1) + headlen = len(numhead) + numberedHeaders = append(numberedHeaders, numhead) + } + + if headlen > maxwidth { + maxwidth = headlen } } data.headers = numberedHeaders + if data.maxwidthHeader != maxwidth && maxwidth > 0 { + data.maxwidthHeader = maxwidth + } } func reduceColumns(data *Tabdata) { diff --git a/lib/io.go b/lib/io.go index 3ad8f75..913c9b2 100644 --- a/lib/io.go +++ b/lib/io.go @@ -29,6 +29,10 @@ func ProcessFiles(args []string) error { if !isTerminal(os.Stdout) { color.Disable() + } else { + level := color.TermColorLevel() + MatchFG = Colors[level]["fg"] + MatchBG = Colors[level]["bg"] } if err != nil { diff --git a/lib/printer.go b/lib/printer.go index ad80bfb..6f3f76e 100644 --- a/lib/printer.go +++ b/lib/printer.go @@ -137,22 +137,14 @@ func printAsciiData(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 + format := fmt.Sprintf("%%%ds: %%s\n", data.maxwidthHeader) if len(data.entries) > 0 { - var idx int for _, entry := range data.entries { - idx = 0 for i, value := range entry { - if len(Columns) > 0 { - if !contains(UseColumns, i+1) { - continue - } - } - - color.Printf(format, data.headers[idx], value) - idx++ + color.Printf(format, data.headers[i], value) } + fmt.Println() } }