mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-19 13:31:02 +01:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9dd2a49d9b | |||
| 90872e0c60 | |||
| baac74eb47 | |||
| 360dd28e20 | |||
| 1e36c148ff | |||
| 399620de98 | |||
| 5d10875a3f | |||
| 4481f59eda | |||
| 1b2f51dcaf | |||
| 0d6de3fe5b | |||
| ec23ae2e76 | |||
| 76930ab45a | |||
| a77e4dbc5a | |||
| 9305f48639 | |||
| da276a1b50 | |||
| dfd3ab9b77 |
33
.github/workflows/ci.yaml
vendored
33
.github/workflows/ci.yaml
vendored
@@ -23,3 +23,36 @@ jobs:
|
|||||||
|
|
||||||
- name: test
|
- name: test
|
||||||
run: make test
|
run: make test
|
||||||
|
|
||||||
|
golangci:
|
||||||
|
name: lint
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/setup-go@v3
|
||||||
|
with:
|
||||||
|
go-version: 1.17
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- name: golangci-lint
|
||||||
|
uses: golangci/golangci-lint-action@v3
|
||||||
|
#with:
|
||||||
|
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
|
||||||
|
# version: v1.29
|
||||||
|
|
||||||
|
# Optional: working directory, useful for monorepos
|
||||||
|
# working-directory: somedir
|
||||||
|
|
||||||
|
# Optional: golangci-lint command line arguments.
|
||||||
|
# args: --issues-exit-code=0
|
||||||
|
|
||||||
|
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
||||||
|
# only-new-issues: true
|
||||||
|
|
||||||
|
# Optional: if set to true then the all caching functionality will be complete disabled,
|
||||||
|
# takes precedence over all other caching options.
|
||||||
|
# skip-cache: true
|
||||||
|
|
||||||
|
# Optional: if set to true then the action don't cache or restore ~/go/pkg.
|
||||||
|
# skip-pkg-cache: true
|
||||||
|
|
||||||
|
# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
|
||||||
|
# skip-build-cache: true
|
||||||
|
|||||||
28
CHANGELOG.md
28
CHANGELOG.md
@@ -4,8 +4,34 @@ All notable changes to this project will be documented in this file.
|
|||||||
|
|
||||||
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org).
|
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org).
|
||||||
|
|
||||||
|
## [v1.0.11](https://github.com/TLINDEN/tablizer/tree/v1.0.11) - 2022-10-19
|
||||||
|
|
||||||
|
[Full Changelog](https://github.com/TLINDEN/tablizer/compare/v1.0.10...v1.0.11)
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Added CI job golinter to regularly check for common mistakes.
|
||||||
|
|
||||||
|
- Added YAML output mode.
|
||||||
|
|
||||||
|
- Added more unit tests, we're over 95% in the lib module.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- do not use any global variables anymore, makes the code easier to
|
||||||
|
maintain, understand and test
|
||||||
|
|
||||||
|
- using io.Writer in print* functions, which is easier to test, also
|
||||||
|
re-implemented the print tests.
|
||||||
|
|
||||||
|
- replaced go-str2duration with my own implementation `duration2int()`.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## [v1.0.10](https://github.com/TLINDEN/tablizer/tree/v1.0.10) - 2022-10-15
|
## [v1.0.10](https://github.com/TLINDEN/tablizer/tree/v1.0.10) - 2022-10-15
|
||||||
|
|
||||||
|
[Full Changelog](https://github.com/TLINDEN/tablizer/compare/v1.0.9...v1.0.10)
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
- Added various sort modes: sort by time, by duration, numerical (-a -t -i)
|
- Added various sort modes: sort by time, by duration, numerical (-a -t -i)
|
||||||
@@ -44,7 +70,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
|
|||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
- Added sort support with the new parameter -k (like sort(1).
|
- Added sort support with the new parameter -k (like sort(1)).
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
16
Makefile
16
Makefile
@@ -18,12 +18,12 @@
|
|||||||
#
|
#
|
||||||
# no need to modify anything below
|
# no need to modify anything below
|
||||||
tool = tablizer
|
tool = tablizer
|
||||||
version = $(shell egrep "= .v" lib/common.go | cut -d'=' -f2 | cut -d'"' -f 2)
|
version = $(shell egrep "= .v" cfg/config.go | cut -d'=' -f2 | cut -d'"' -f 2)
|
||||||
archs = android darwin freebsd linux netbsd openbsd windows
|
archs = android darwin freebsd linux netbsd openbsd windows
|
||||||
PREFIX = /usr/local
|
PREFIX = /usr/local
|
||||||
UID = root
|
UID = root
|
||||||
GID = 0
|
GID = 0
|
||||||
BRANCH = $(shell git describe --all | cut -d/ -f2)
|
BRANCH = $(shell git branch --show-current)
|
||||||
COMMIT = $(shell git rev-parse --short=8 HEAD)
|
COMMIT = $(shell git rev-parse --short=8 HEAD)
|
||||||
BUILD = $(shell date +%Y.%m.%d.%H%M%S)
|
BUILD = $(shell date +%Y.%m.%d.%H%M%S)
|
||||||
VERSION:= $(if $(filter $(BRANCH), development),$(version)-$(BRANCH)-$(COMMIT)-$(BUILD),$(version))
|
VERSION:= $(if $(filter $(BRANCH), development),$(version)-$(BRANCH)-$(COMMIT)-$(BUILD),$(version))
|
||||||
@@ -42,7 +42,7 @@ cmd/%.go: %.pod
|
|||||||
echo "\`" >> cmd/$*.go
|
echo "\`" >> cmd/$*.go
|
||||||
|
|
||||||
buildlocal:
|
buildlocal:
|
||||||
go build -ldflags "-X 'github.com/tlinden/tablizer/lib.VERSION=$(VERSION)'"
|
go build -ldflags "-X 'github.com/tlinden/tablizer/cfg.VERSION=$(VERSION)'"
|
||||||
|
|
||||||
release:
|
release:
|
||||||
./mkrel.sh $(tool) $(version)
|
./mkrel.sh $(tool) $(version)
|
||||||
@@ -55,7 +55,15 @@ install: buildlocal
|
|||||||
install -o $(UID) -g $(GID) -m 444 $(tool).1 $(PREFIX)/man/man1/
|
install -o $(UID) -g $(GID) -m 444 $(tool).1 $(PREFIX)/man/man1/
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(tool) releases
|
rm -rf $(tool) releases coverage.out
|
||||||
|
|
||||||
test:
|
test:
|
||||||
go test -v ./...
|
go test -v ./...
|
||||||
|
|
||||||
|
singletest:
|
||||||
|
@echo "Call like this: ''make singletest TEST=TestPrepareColumns MOD=lib"
|
||||||
|
go test -run $(TEST) github.com/tlinden/tablizer/$(MOD)
|
||||||
|
|
||||||
|
cover-report:
|
||||||
|
go test ./... -cover -coverprofile=coverage.out
|
||||||
|
go tool cover -html=coverage.out
|
||||||
|
|||||||
6
TODO.md
6
TODO.md
@@ -1,8 +1,12 @@
|
|||||||
## Fixes to be implemented
|
## Fixes to be implemented
|
||||||
|
|
||||||
|
- rm printYamlData() log.Fatal(), maybe return error on all printers?
|
||||||
|
|
||||||
|
- printShellData() checks Columns unnecessarily (compare to yaml or extended)
|
||||||
|
|
||||||
## Features to be implemented
|
## Features to be implemented
|
||||||
|
|
||||||
- add output modes yaml and csv
|
- add output mode csv
|
||||||
|
|
||||||
- add --no-headers option
|
- add --no-headers option
|
||||||
|
|
||||||
|
|||||||
144
cfg/config.go
Normal file
144
cfg/config.go
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2022 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
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package cfg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"github.com/gookit/color"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
const DefaultSeparator string = `(\s\s+|\t)`
|
||||||
|
const ValidOutputModes string = "(orgtbl|markdown|extended|ascii|yaml|shell)"
|
||||||
|
const Version string = "v1.0.11"
|
||||||
|
|
||||||
|
var VERSION string // maintained by -x
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Debug bool
|
||||||
|
NoNumbering bool
|
||||||
|
Columns string
|
||||||
|
UseColumns []int
|
||||||
|
Separator string
|
||||||
|
OutputMode string
|
||||||
|
InvertMatch bool
|
||||||
|
Pattern string
|
||||||
|
|
||||||
|
SortMode string
|
||||||
|
SortDescending bool
|
||||||
|
SortByColumn int
|
||||||
|
|
||||||
|
/*
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// maps outputmode short flags to output mode, ie. -O => -o orgtbl
|
||||||
|
type Modeflag struct {
|
||||||
|
X bool
|
||||||
|
O bool
|
||||||
|
M bool
|
||||||
|
S bool
|
||||||
|
Y bool
|
||||||
|
A bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// various sort types
|
||||||
|
type Sortmode struct {
|
||||||
|
Numeric bool
|
||||||
|
Time bool
|
||||||
|
Age bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func Colors() map[color.Level]map[string]string {
|
||||||
|
// default color schemes
|
||||||
|
return map[color.Level]map[string]string{
|
||||||
|
color.Level16: {
|
||||||
|
"bg": "green", "fg": "black",
|
||||||
|
},
|
||||||
|
color.Level256: {
|
||||||
|
"bg": "lightGreen", "fg": "black",
|
||||||
|
},
|
||||||
|
color.LevelRgb: {
|
||||||
|
// FIXME: maybe use something nicer
|
||||||
|
"bg": "lightGreen", "fg": "black",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Getversion() string {
|
||||||
|
// main program version
|
||||||
|
|
||||||
|
// generated version string, used by -v contains lib.Version on
|
||||||
|
// main branch, and lib.Version-$branch-$lastcommit-$date on
|
||||||
|
// development branch
|
||||||
|
|
||||||
|
return fmt.Sprintf("This is tablizer version %s", VERSION)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (conf *Config) PrepareModeFlags(flag Modeflag, mode string) error {
|
||||||
|
if len(mode) == 0 {
|
||||||
|
// associate short flags like -X with mode selector
|
||||||
|
switch {
|
||||||
|
case flag.X:
|
||||||
|
conf.OutputMode = "extended"
|
||||||
|
case flag.M:
|
||||||
|
conf.OutputMode = "markdown"
|
||||||
|
case flag.O:
|
||||||
|
conf.OutputMode = "orgtbl"
|
||||||
|
case flag.S:
|
||||||
|
conf.OutputMode = "shell"
|
||||||
|
conf.NoNumbering = true
|
||||||
|
case flag.Y:
|
||||||
|
conf.OutputMode = "yaml"
|
||||||
|
conf.NoNumbering = true
|
||||||
|
default:
|
||||||
|
conf.OutputMode = "ascii"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
r, _ := regexp.Compile(ValidOutputModes) // hardcoded, no fail expected
|
||||||
|
match := r.MatchString(mode)
|
||||||
|
|
||||||
|
if !match {
|
||||||
|
return errors.New("Invalid output mode!")
|
||||||
|
}
|
||||||
|
|
||||||
|
conf.OutputMode = mode
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (conf *Config) PrepareSortFlags(flag Sortmode) {
|
||||||
|
switch {
|
||||||
|
case flag.Numeric:
|
||||||
|
conf.SortMode = "numeric"
|
||||||
|
case flag.Age:
|
||||||
|
conf.SortMode = "duration"
|
||||||
|
case flag.Time:
|
||||||
|
conf.SortMode = "time"
|
||||||
|
default:
|
||||||
|
conf.SortMode = "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
98
cfg/config_test.go
Normal file
98
cfg/config_test.go
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2022 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
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package cfg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
// "reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPrepareModeFlags(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
flag Modeflag
|
||||||
|
mode string // input, if any
|
||||||
|
expect string // output
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
// short commandline flags like -M
|
||||||
|
{Modeflag{X: true}, "", "extended", false},
|
||||||
|
{Modeflag{S: true}, "", "shell", false},
|
||||||
|
{Modeflag{O: true}, "", "orgtbl", false},
|
||||||
|
{Modeflag{Y: true}, "", "yaml", false},
|
||||||
|
{Modeflag{M: true}, "", "markdown", false},
|
||||||
|
{Modeflag{}, "", "ascii", false},
|
||||||
|
|
||||||
|
// long flags like -o yaml
|
||||||
|
{Modeflag{}, "extended", "extended", false},
|
||||||
|
{Modeflag{}, "shell", "shell", false},
|
||||||
|
{Modeflag{}, "orgtbl", "orgtbl", false},
|
||||||
|
{Modeflag{}, "yaml", "yaml", false},
|
||||||
|
{Modeflag{}, "markdown", "markdown", false},
|
||||||
|
|
||||||
|
// failing
|
||||||
|
{Modeflag{}, "blah", "", true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
testname := fmt.Sprintf("PrepareModeFlags-flags-mode-%s-expect-%s-want-%t",
|
||||||
|
tt.mode, tt.expect, tt.want)
|
||||||
|
t.Run(testname, func(t *testing.T) {
|
||||||
|
c := Config{OutputMode: tt.mode}
|
||||||
|
|
||||||
|
// check either flag or pre filled mode, whatever is defined in tt
|
||||||
|
err := c.PrepareModeFlags(tt.flag, tt.mode)
|
||||||
|
if err != nil {
|
||||||
|
if !tt.want {
|
||||||
|
// expect to fail
|
||||||
|
t.Fatalf("PrepareModeFlags returned unexpected error: %s", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if c.OutputMode != tt.expect {
|
||||||
|
t.Errorf("got: %s, expect: %s", c.OutputMode, tt.expect)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrepareSortFlags(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
flag Sortmode
|
||||||
|
expect string // output
|
||||||
|
}{
|
||||||
|
// short commandline flags like -M
|
||||||
|
{Sortmode{Numeric: true}, "numeric"},
|
||||||
|
{Sortmode{Age: true}, "duration"},
|
||||||
|
{Sortmode{Time: true}, "time"},
|
||||||
|
{Sortmode{}, "string"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
testname := fmt.Sprintf("PrepareSortFlags-expect-%s", tt.expect)
|
||||||
|
t.Run(testname, func(t *testing.T) {
|
||||||
|
c := Config{}
|
||||||
|
|
||||||
|
c.PrepareSortFlags(tt.flag)
|
||||||
|
|
||||||
|
if c.SortMode != tt.expect {
|
||||||
|
t.Errorf("got: %s, expect: %s", c.SortMode, tt.expect)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
95
cmd/root.go
95
cmd/root.go
@@ -20,14 +20,13 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/tlinden/tablizer/cfg"
|
||||||
"github.com/tlinden/tablizer/lib"
|
"github.com/tlinden/tablizer/lib"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ShowManual = false
|
|
||||||
|
|
||||||
func man() {
|
func man() {
|
||||||
man := exec.Command("less", "-")
|
man := exec.Command("less", "-")
|
||||||
|
|
||||||
@@ -45,13 +44,23 @@ func man() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
func Execute() {
|
||||||
|
var (
|
||||||
|
conf cfg.Config
|
||||||
|
ShowManual bool
|
||||||
|
Outputmode string
|
||||||
|
ShowVersion bool
|
||||||
|
modeflag cfg.Modeflag
|
||||||
|
sortmode cfg.Sortmode
|
||||||
|
)
|
||||||
|
|
||||||
|
var rootCmd = &cobra.Command{
|
||||||
Use: "tablizer [regex] [file, ...]",
|
Use: "tablizer [regex] [file, ...]",
|
||||||
Short: "[Re-]tabularize tabular data",
|
Short: "[Re-]tabularize tabular data",
|
||||||
Long: `Manipulate tabular output of other programs`,
|
Long: `Manipulate tabular output of other programs`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if lib.ShowVersion {
|
if ShowVersion {
|
||||||
fmt.Printf("This is tablizer version %s\n", lib.VERSION)
|
fmt.Println(cfg.Getversion())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,52 +69,54 @@ var rootCmd = &cobra.Command{
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
err := lib.PrepareModeFlags()
|
// prepare flags
|
||||||
|
err := conf.PrepareModeFlags(modeflag, Outputmode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
lib.PrepareSortFlags()
|
conf.PrepareSortFlags(sortmode)
|
||||||
|
|
||||||
return lib.ProcessFiles(args)
|
// actual execution starts here
|
||||||
|
return lib.ProcessFiles(conf, args)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// options
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&conf.Debug, "debug", "d", false, "Enable debugging")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&conf.NoNumbering, "no-numbering", "n", false, "Disable header numbering")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&conf.NoColor, "no-color", "N", false, "Disable pattern highlighting")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&ShowVersion, "version", "V", false, "Print program version")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&conf.InvertMatch, "invert-match", "v", false, "select non-matching rows")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&ShowManual, "man", "m", false, "Display manual page")
|
||||||
|
rootCmd.PersistentFlags().StringVarP(&conf.Separator, "separator", "s", cfg.DefaultSeparator, "Custom field separator")
|
||||||
|
rootCmd.PersistentFlags().StringVarP(&conf.Columns, "columns", "c", "", "Only show the speficied columns (separated by ,)")
|
||||||
|
|
||||||
|
// sort options
|
||||||
|
rootCmd.PersistentFlags().IntVarP(&conf.SortByColumn, "sort-by", "k", 0, "Sort by column (default: 1)")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&conf.SortDescending, "sort-desc", "D", false, "Sort in descending order (default: ascending)")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&sortmode.Numeric, "sort-numeric", "i", false, "sort according to string numerical value")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&sortmode.Time, "sort-time", "t", false, "sort according to time string")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&sortmode.Age, "sort-age", "a", false, "sort according to age (duration) string")
|
||||||
|
|
||||||
|
// output flags, only 1 allowed, hidden, since just short cuts
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&modeflag.X, "extended", "X", false, "Enable extended output")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&modeflag.M, "markdown", "M", false, "Enable markdown table output")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&modeflag.O, "orgtbl", "O", false, "Enable org-mode table output")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&modeflag.S, "shell", "S", false, "Enable shell mode output")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&modeflag.Y, "yaml", "Y", false, "Enable yaml output")
|
||||||
|
rootCmd.MarkFlagsMutuallyExclusive("extended", "markdown", "orgtbl", "shell", "yaml")
|
||||||
|
_ = rootCmd.Flags().MarkHidden("extended")
|
||||||
|
_ = rootCmd.Flags().MarkHidden("orgtbl")
|
||||||
|
_ = rootCmd.Flags().MarkHidden("markdown")
|
||||||
|
_ = rootCmd.Flags().MarkHidden("shell")
|
||||||
|
_ = rootCmd.Flags().MarkHidden("yaml")
|
||||||
|
|
||||||
|
// same thing but more common, takes precedence over above group
|
||||||
|
rootCmd.PersistentFlags().StringVarP(&Outputmode, "output", "o", "", "Output mode - one of: orgtbl, markdown, extended, shell, ascii(default)")
|
||||||
|
|
||||||
func Execute() {
|
|
||||||
err := rootCmd.Execute()
|
err := rootCmd.Execute()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
|
||||||
rootCmd.PersistentFlags().BoolVarP(&lib.Debug, "debug", "d", false, "Enable debugging")
|
|
||||||
rootCmd.PersistentFlags().BoolVarP(&lib.NoNumbering, "no-numbering", "n", false, "Disable header numbering")
|
|
||||||
rootCmd.PersistentFlags().BoolVarP(&lib.NoColor, "no-color", "N", false, "Disable pattern highlighting")
|
|
||||||
rootCmd.PersistentFlags().BoolVarP(&lib.ShowVersion, "version", "V", false, "Print program version")
|
|
||||||
rootCmd.PersistentFlags().BoolVarP(&lib.InvertMatch, "invert-match", "v", false, "select non-matching rows")
|
|
||||||
rootCmd.PersistentFlags().BoolVarP(&ShowManual, "man", "m", false, "Display manual page")
|
|
||||||
rootCmd.PersistentFlags().StringVarP(&lib.Separator, "separator", "s", lib.DefaultSeparator, "Custom field separator")
|
|
||||||
rootCmd.PersistentFlags().StringVarP(&lib.Columns, "columns", "c", "", "Only show the speficied columns (separated by ,)")
|
|
||||||
|
|
||||||
// sort options
|
|
||||||
rootCmd.PersistentFlags().IntVarP(&lib.SortByColumn, "sort-by", "k", 0, "Sort by column (default: 1)")
|
|
||||||
rootCmd.PersistentFlags().BoolVarP(&lib.SortDescending, "sort-desc", "D", false, "Sort in descending order (default: ascending)")
|
|
||||||
rootCmd.PersistentFlags().BoolVarP(&lib.SortNumeric, "sort-numeric", "i", false, "sort according to string numerical value")
|
|
||||||
rootCmd.PersistentFlags().BoolVarP(&lib.SortTime, "sort-time", "t", false, "sort according to time string")
|
|
||||||
rootCmd.PersistentFlags().BoolVarP(&lib.SortAge, "sort-age", "a", false, "sort according to age (duration) string")
|
|
||||||
|
|
||||||
// output flags, only 1 allowed, hidden, since just short cuts
|
|
||||||
rootCmd.PersistentFlags().BoolVarP(&lib.OutflagExtended, "extended", "X", false, "Enable extended output")
|
|
||||||
rootCmd.PersistentFlags().BoolVarP(&lib.OutflagMarkdown, "markdown", "M", false, "Enable markdown table output")
|
|
||||||
rootCmd.PersistentFlags().BoolVarP(&lib.OutflagOrgtable, "orgtbl", "O", false, "Enable org-mode table output")
|
|
||||||
rootCmd.PersistentFlags().BoolVarP(&lib.OutflagShell, "shell", "S", false, "Enable shell mode output")
|
|
||||||
rootCmd.MarkFlagsMutuallyExclusive("extended", "markdown", "orgtbl", "shell")
|
|
||||||
rootCmd.Flags().MarkHidden("extended")
|
|
||||||
rootCmd.Flags().MarkHidden("orgtbl")
|
|
||||||
rootCmd.Flags().MarkHidden("markdown")
|
|
||||||
rootCmd.Flags().MarkHidden("shell")
|
|
||||||
|
|
||||||
// same thing but more common, takes precedence over above group
|
|
||||||
rootCmd.PersistentFlags().StringVarP(&lib.OutputMode, "output", "o", "", "Output mode - one of: orgtbl, markdown, extended, shell, ascii(default)")
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ SYNOPSIS
|
|||||||
-m, --man Display manual page
|
-m, --man Display manual page
|
||||||
-n, --no-numbering Disable header numbering
|
-n, --no-numbering Disable header numbering
|
||||||
-N, --no-color Disable pattern highlighting
|
-N, --no-color Disable pattern highlighting
|
||||||
-o, --output string Output mode - one of: orgtbl, markdown, extended, ascii(default)
|
-o, --output string Output mode - one of: orgtbl, markdown, extended, yaml, ascii(default)
|
||||||
-X, --extended Enable extended output
|
-X, --extended Enable extended output
|
||||||
-M, --markdown Enable markdown table output
|
-M, --markdown Enable markdown table output
|
||||||
-O, --orgtbl Enable org-mode table output
|
-O, --orgtbl Enable org-mode table output
|
||||||
@@ -178,7 +178,8 @@ DESCRIPTION
|
|||||||
|
|
||||||
Beside normal ascii mode (the default) and extended mode there are more
|
Beside normal ascii mode (the default) and extended mode there are more
|
||||||
output modes available: orgtbl which prints an Emacs org-mode table and
|
output modes available: orgtbl which prints an Emacs org-mode table and
|
||||||
markdown which prints a Markdown table.
|
markdown which prints a Markdown table and yaml, which prints yaml
|
||||||
|
encoding.
|
||||||
|
|
||||||
BUGS
|
BUGS
|
||||||
In order to report a bug, unexpected behavior, feature requests or to
|
In order to report a bug, unexpected behavior, feature requests or to
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -8,7 +8,7 @@ require (
|
|||||||
github.com/gookit/color v1.5.2
|
github.com/gookit/color v1.5.2
|
||||||
github.com/olekukonko/tablewriter v0.0.5
|
github.com/olekukonko/tablewriter v0.0.5
|
||||||
github.com/spf13/cobra v1.5.0
|
github.com/spf13/cobra v1.5.0
|
||||||
github.com/xhit/go-str2duration/v2 v2.0.0
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|||||||
3
go.sum
3
go.sum
@@ -31,12 +31,11 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
|
|||||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
github.com/xhit/go-str2duration/v2 v2.0.0 h1:uFtk6FWB375bP7ewQl+/1wBcn840GPhnySOdcz/okPE=
|
|
||||||
github.com/xhit/go-str2duration/v2 v2.0.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
|
|
||||||
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
|
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
|
||||||
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
|
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
|
||||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 h1:Bli41pIlzTzf3KEY06n+xnzK/BESIg2ze4Pgfh/aI8c=
|
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 h1:Bli41pIlzTzf3KEY06n+xnzK/BESIg2ze4Pgfh/aI8c=
|
||||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
@@ -17,74 +17,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
|
|
||||||
package lib
|
package lib
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gookit/color"
|
|
||||||
//"github.com/xo/terminfo"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
// command line flags
|
|
||||||
Debug bool
|
|
||||||
XtendedOut bool
|
|
||||||
NoNumbering bool
|
|
||||||
ShowVersion bool
|
|
||||||
Columns string
|
|
||||||
UseColumns []int
|
|
||||||
DefaultSeparator string = `(\s\s+|\t)`
|
|
||||||
Separator string = `(\s\s+|\t)`
|
|
||||||
OutflagExtended bool
|
|
||||||
OutflagMarkdown bool
|
|
||||||
OutflagOrgtable bool
|
|
||||||
OutflagShell bool
|
|
||||||
OutputMode string
|
|
||||||
InvertMatch bool
|
|
||||||
Pattern string
|
|
||||||
|
|
||||||
/*
|
|
||||||
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: {
|
|
||||||
"bg": "green", "fg": "black",
|
|
||||||
},
|
|
||||||
color.Level256: {
|
|
||||||
"bg": "lightGreen", "fg": "black",
|
|
||||||
},
|
|
||||||
color.LevelRgb: {
|
|
||||||
// FIXME: maybe use something nicer
|
|
||||||
"bg": "lightGreen", "fg": "black",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// used for validation
|
|
||||||
validOutputmodes = "(orgtbl|markdown|extended|ascii)"
|
|
||||||
|
|
||||||
// main program version
|
|
||||||
Version = "v1.0.10"
|
|
||||||
|
|
||||||
// generated version string, used by -v contains lib.Version on
|
|
||||||
// main branch, and lib.Version-$branch-$lastcommit-$date on
|
|
||||||
// development branch
|
|
||||||
VERSION string
|
|
||||||
|
|
||||||
// sorting
|
|
||||||
SortByColumn int
|
|
||||||
SortDescending bool
|
|
||||||
|
|
||||||
SortNumeric bool
|
|
||||||
SortTime bool
|
|
||||||
SortAge bool
|
|
||||||
SortMode string
|
|
||||||
)
|
|
||||||
|
|
||||||
// contains a whole parsed table
|
// contains a whole parsed table
|
||||||
type Tabdata struct {
|
type Tabdata struct {
|
||||||
maxwidthHeader int // longest header
|
maxwidthHeader int // longest header
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gookit/color"
|
"github.com/gookit/color"
|
||||||
|
"github.com/tlinden/tablizer/cfg"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
@@ -37,13 +38,13 @@ func contains(s []int, e int) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse columns list given with -c
|
// parse columns list given with -c, modifies config.UseColumns based
|
||||||
func PrepareColumns(data *Tabdata) error {
|
// on eventually given regex
|
||||||
UseColumns = nil
|
func PrepareColumns(c *cfg.Config, data *Tabdata) error {
|
||||||
if len(Columns) > 0 {
|
if len(c.Columns) > 0 {
|
||||||
for _, use := range strings.Split(Columns, ",") {
|
for _, use := range strings.Split(c.Columns, ",") {
|
||||||
if len(use) == 0 {
|
if len(use) == 0 {
|
||||||
msg := fmt.Sprintf("Could not parse columns list %s: empty column", Columns)
|
msg := fmt.Sprintf("Could not parse columns list %s: empty column", c.Columns)
|
||||||
return errors.New(msg)
|
return errors.New(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,14 +53,14 @@ func PrepareColumns(data *Tabdata) error {
|
|||||||
// might be a regexp
|
// might be a regexp
|
||||||
colPattern, err := regexp.Compile(use)
|
colPattern, err := regexp.Compile(use)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg := fmt.Sprintf("Could not parse columns list %s: %v", Columns, err)
|
msg := fmt.Sprintf("Could not parse columns list %s: %v", c.Columns, err)
|
||||||
return errors.New(msg)
|
return errors.New(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// find matching header fields
|
// find matching header fields
|
||||||
for i, head := range data.headers {
|
for i, head := range data.headers {
|
||||||
if colPattern.MatchString(head) {
|
if colPattern.MatchString(head) {
|
||||||
UseColumns = append(UseColumns, i+1)
|
c.UseColumns = append(c.UseColumns, i+1)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -68,41 +69,41 @@ func PrepareColumns(data *Tabdata) error {
|
|||||||
// a colum spec is not a number, we process them above
|
// a colum spec is not a number, we process them above
|
||||||
// inside the err handler for atoi(). so only add the
|
// inside the err handler for atoi(). so only add the
|
||||||
// number, if it's really just a number.
|
// number, if it's really just a number.
|
||||||
UseColumns = append(UseColumns, usenum)
|
c.UseColumns = append(c.UseColumns, usenum)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// deduplicate: put all values into a map (value gets map key)
|
// deduplicate: put all values into a map (value gets map key)
|
||||||
// thereby removing duplicates, extract keys into new slice
|
// thereby removing duplicates, extract keys into new slice
|
||||||
// and sort it
|
// and sort it
|
||||||
imap := make(map[int]int, len(UseColumns))
|
imap := make(map[int]int, len(c.UseColumns))
|
||||||
for _, i := range UseColumns {
|
for _, i := range c.UseColumns {
|
||||||
imap[i] = 0
|
imap[i] = 0
|
||||||
}
|
}
|
||||||
UseColumns = nil
|
c.UseColumns = nil
|
||||||
for k := range imap {
|
for k := range imap {
|
||||||
UseColumns = append(UseColumns, k)
|
c.UseColumns = append(c.UseColumns, k)
|
||||||
}
|
}
|
||||||
sort.Ints(UseColumns)
|
sort.Ints(c.UseColumns)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepare headers: add numbers to headers
|
// prepare headers: add numbers to headers
|
||||||
func numberizeHeaders(data *Tabdata) {
|
func numberizeAndReduceHeaders(c cfg.Config, data *Tabdata) {
|
||||||
numberedHeaders := []string{}
|
numberedHeaders := []string{}
|
||||||
maxwidth := 0 // start from scratch, so we only look at displayed column widths
|
maxwidth := 0 // start from scratch, so we only look at displayed column widths
|
||||||
|
|
||||||
for i, head := range data.headers {
|
for i, head := range data.headers {
|
||||||
headlen := 0
|
headlen := 0
|
||||||
if len(Columns) > 0 {
|
if len(c.Columns) > 0 {
|
||||||
// -c specified
|
// -c specified
|
||||||
if !contains(UseColumns, i+1) {
|
if !contains(c.UseColumns, i+1) {
|
||||||
// ignore this one
|
// ignore this one
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if NoNumbering {
|
if c.NoNumbering {
|
||||||
numberedHeaders = append(numberedHeaders, head)
|
numberedHeaders = append(numberedHeaders, head)
|
||||||
headlen = len(head)
|
headlen = len(head)
|
||||||
} else {
|
} else {
|
||||||
@@ -122,14 +123,14 @@ func numberizeHeaders(data *Tabdata) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// exclude columns, if any
|
// exclude columns, if any
|
||||||
func reduceColumns(data *Tabdata) {
|
func reduceColumns(c cfg.Config, data *Tabdata) {
|
||||||
if len(Columns) > 0 {
|
if len(c.Columns) > 0 {
|
||||||
reducedEntries := [][]string{}
|
reducedEntries := [][]string{}
|
||||||
var reducedEntry []string
|
var reducedEntry []string
|
||||||
for _, entry := range data.entries {
|
for _, entry := range data.entries {
|
||||||
reducedEntry = nil
|
reducedEntry = nil
|
||||||
for i, value := range entry {
|
for i, value := range entry {
|
||||||
if !contains(UseColumns, i+1) {
|
if !contains(c.UseColumns, i+1) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,52 +142,6 @@ func reduceColumns(data *Tabdata) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func PrepareModeFlags() error {
|
|
||||||
if len(OutputMode) == 0 {
|
|
||||||
// associate short flags like -X with mode selector
|
|
||||||
switch {
|
|
||||||
case OutflagExtended:
|
|
||||||
OutputMode = "extended"
|
|
||||||
case OutflagMarkdown:
|
|
||||||
OutputMode = "markdown"
|
|
||||||
case OutflagOrgtable:
|
|
||||||
OutputMode = "orgtbl"
|
|
||||||
case OutflagShell:
|
|
||||||
OutputMode = "shell"
|
|
||||||
NoNumbering = true
|
|
||||||
default:
|
|
||||||
OutputMode = "ascii"
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
r, err := regexp.Compile(validOutputmodes)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("Failed to validate output mode spec!")
|
|
||||||
}
|
|
||||||
|
|
||||||
match := r.MatchString(OutputMode)
|
|
||||||
|
|
||||||
if !match {
|
|
||||||
return errors.New("Invalid output mode!")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func PrepareSortFlags() {
|
|
||||||
switch {
|
|
||||||
case SortNumeric:
|
|
||||||
SortMode = "numeric"
|
|
||||||
case SortAge:
|
|
||||||
SortMode = "duration"
|
|
||||||
case SortTime:
|
|
||||||
SortMode = "time"
|
|
||||||
default:
|
|
||||||
SortMode = "string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func trimRow(row []string) []string {
|
func trimRow(row []string) []string {
|
||||||
// FIXME: remove this when we only use Tablewriter and strip in ParseFile()!
|
// FIXME: remove this when we only use Tablewriter and strip in ParseFile()!
|
||||||
var fixedrow []string
|
var fixedrow []string
|
||||||
@@ -197,10 +152,10 @@ func trimRow(row []string) []string {
|
|||||||
return fixedrow
|
return fixedrow
|
||||||
}
|
}
|
||||||
|
|
||||||
func colorizeData(output string) string {
|
func colorizeData(c cfg.Config, output string) string {
|
||||||
if len(Pattern) > 0 && !NoColor && color.IsConsole(os.Stdout) {
|
if len(c.Pattern) > 0 && !c.NoColor && color.IsConsole(os.Stdout) {
|
||||||
r := regexp.MustCompile("(" + Pattern + ")")
|
r := regexp.MustCompile("(" + c.Pattern + ")")
|
||||||
return r.ReplaceAllString(output, "<bg="+MatchBG+";fg="+MatchFG+">$1</>")
|
return r.ReplaceAllString(output, "<bg="+c.MatchBG+";fg="+c.MatchFG+">$1</>")
|
||||||
} else {
|
} else {
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,11 +19,12 @@ package lib
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/tlinden/tablizer/cfg"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Testcontains(t *testing.T) {
|
func TestContains(t *testing.T) {
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
list []int
|
list []int
|
||||||
search int
|
search int
|
||||||
@@ -62,6 +63,7 @@ func TestPrepareColumns(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
input string
|
input string
|
||||||
exp []int
|
exp []int
|
||||||
@@ -71,20 +73,21 @@ func TestPrepareColumns(t *testing.T) {
|
|||||||
{"1,2,", []int{}, true},
|
{"1,2,", []int{}, true},
|
||||||
{"T", []int{2, 3}, false},
|
{"T", []int{2, 3}, false},
|
||||||
{"T,2,3", []int{2, 3}, false},
|
{"T,2,3", []int{2, 3}, false},
|
||||||
|
{"[a-z,4,5", []int{4, 5}, true}, // invalid regexp
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
testname := fmt.Sprintf("PrepareColumns-%s-%t", tt.input, tt.wanterror)
|
testname := fmt.Sprintf("PrepareColumns-%s-%t", tt.input, tt.wanterror)
|
||||||
t.Run(testname, func(t *testing.T) {
|
t.Run(testname, func(t *testing.T) {
|
||||||
Columns = tt.input
|
c := cfg.Config{Columns: tt.input}
|
||||||
err := PrepareColumns(&data)
|
err := PrepareColumns(&c, &data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !tt.wanterror {
|
if !tt.wanterror {
|
||||||
t.Errorf("got error: %v", err)
|
t.Errorf("got error: %v", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if !reflect.DeepEqual(UseColumns, tt.exp) {
|
if !reflect.DeepEqual(c.UseColumns, tt.exp) {
|
||||||
t.Errorf("got: %v, expected: %v", UseColumns, tt.exp)
|
t.Errorf("got: %v, expected: %v", c.UseColumns, tt.exp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -116,20 +119,44 @@ func TestReduceColumns(t *testing.T) {
|
|||||||
|
|
||||||
input := [][]string{{"a", "b", "c"}}
|
input := [][]string{{"a", "b", "c"}}
|
||||||
|
|
||||||
Columns = "y" // used as a flag with len(Columns)...
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
testname := fmt.Sprintf("reduce-columns-by-%+v", tt.columns)
|
testname := fmt.Sprintf("reduce-columns-by-%+v", tt.columns)
|
||||||
t.Run(testname, func(t *testing.T) {
|
t.Run(testname, func(t *testing.T) {
|
||||||
UseColumns = tt.columns
|
c := cfg.Config{Columns: "x", UseColumns: tt.columns}
|
||||||
data := Tabdata{entries: input}
|
data := Tabdata{entries: input}
|
||||||
reduceColumns(&data)
|
reduceColumns(c, &data)
|
||||||
if !reflect.DeepEqual(data.entries, tt.expect) {
|
if !reflect.DeepEqual(data.entries, tt.expect) {
|
||||||
t.Errorf("reduceColumns returned invalid data:\ngot: %+v\nexp: %+v", data.entries, tt.expect)
|
t.Errorf("reduceColumns returned invalid data:\ngot: %+v\nexp: %+v", data.entries, tt.expect)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Columns = "" // reset for other tests
|
|
||||||
UseColumns = nil
|
func TestNumberizeHeaders(t *testing.T) {
|
||||||
|
data := Tabdata{
|
||||||
|
headers: []string{"ONE", "TWO", "THREE"},
|
||||||
|
}
|
||||||
|
|
||||||
|
var tests = []struct {
|
||||||
|
expect []string
|
||||||
|
columns []int
|
||||||
|
nonum bool
|
||||||
|
}{
|
||||||
|
{[]string{"ONE(1)", "TWO(2)", "THREE(3)"}, []int{1, 2, 3}, false},
|
||||||
|
{[]string{"ONE(1)", "TWO(2)"}, []int{1, 2}, false},
|
||||||
|
{[]string{"ONE", "TWO"}, []int{1, 2}, true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
testname := fmt.Sprintf("numberize-headers-columns-%+v-nonum-%t", tt.columns, tt.nonum)
|
||||||
|
t.Run(testname, func(t *testing.T) {
|
||||||
|
c := cfg.Config{Columns: "x", UseColumns: tt.columns, NoNumbering: tt.nonum}
|
||||||
|
usedata := data
|
||||||
|
numberizeAndReduceHeaders(c, &usedata)
|
||||||
|
if !reflect.DeepEqual(usedata.headers, tt.expect) {
|
||||||
|
t.Errorf("numberizeAndReduceHeaders returned invalid data:\ngot: %+v\nexp: %+v",
|
||||||
|
usedata.headers, tt.expect)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
37
lib/io.go
37
lib/io.go
@@ -20,43 +20,50 @@ package lib
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/gookit/color"
|
"github.com/gookit/color"
|
||||||
|
"github.com/tlinden/tablizer/cfg"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ProcessFiles(args []string) error {
|
func ProcessFiles(c cfg.Config, args []string) error {
|
||||||
fds, pattern, err := determineIO(args)
|
fds, pattern, err := determineIO(&c, args)
|
||||||
|
|
||||||
if !isTerminal(os.Stdout) {
|
|
||||||
color.Disable()
|
|
||||||
} else {
|
|
||||||
level := color.TermColorLevel()
|
|
||||||
MatchFG = Colors[level]["fg"]
|
|
||||||
MatchBG = Colors[level]["bg"]
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
determineColormode(&c)
|
||||||
|
|
||||||
for _, fd := range fds {
|
for _, fd := range fds {
|
||||||
data, err := parseFile(fd, pattern)
|
data, err := parseFile(c, fd, pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = PrepareColumns(&data)
|
err = PrepareColumns(&c, &data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
printData(&data)
|
printData(os.Stdout, c, &data)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func determineIO(args []string) ([]io.Reader, string, error) {
|
// find supported color mode, modifies config based on constants
|
||||||
|
func determineColormode(c *cfg.Config) {
|
||||||
|
if !isTerminal(os.Stdout) {
|
||||||
|
color.Disable()
|
||||||
|
} else {
|
||||||
|
level := color.TermColorLevel()
|
||||||
|
colors := cfg.Colors()
|
||||||
|
c.MatchFG = colors[level]["fg"]
|
||||||
|
c.MatchBG = colors[level]["bg"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func determineIO(c *cfg.Config, args []string) ([]io.Reader, string, error) {
|
||||||
var pattern string
|
var pattern string
|
||||||
var fds []io.Reader
|
var fds []io.Reader
|
||||||
var havefiles bool
|
var havefiles bool
|
||||||
@@ -67,7 +74,7 @@ func determineIO(args []string) ([]io.Reader, string, error) {
|
|||||||
// first one is not a file, consider it as regexp and
|
// first one is not a file, consider it as regexp and
|
||||||
// shift arg list
|
// shift arg list
|
||||||
pattern = args[0]
|
pattern = args[0]
|
||||||
Pattern = args[0] // FIXME
|
c.Pattern = args[0] // used for colorization by printData()
|
||||||
args = args[1:]
|
args = args[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/alecthomas/repr"
|
"github.com/alecthomas/repr"
|
||||||
|
"github.com/tlinden/tablizer/cfg"
|
||||||
"io"
|
"io"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -30,13 +31,13 @@ import (
|
|||||||
/*
|
/*
|
||||||
Parse tabular input.
|
Parse tabular input.
|
||||||
*/
|
*/
|
||||||
func parseFile(input io.Reader, pattern string) (Tabdata, error) {
|
func parseFile(c cfg.Config, input io.Reader, pattern string) (Tabdata, error) {
|
||||||
data := Tabdata{}
|
data := Tabdata{}
|
||||||
|
|
||||||
var scanner *bufio.Scanner
|
var scanner *bufio.Scanner
|
||||||
|
|
||||||
hadFirst := false
|
hadFirst := false
|
||||||
separate := regexp.MustCompile(Separator)
|
separate := regexp.MustCompile(c.Separator)
|
||||||
patternR, err := regexp.Compile(pattern)
|
patternR, err := regexp.Compile(pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return data, errors.Unwrap(fmt.Errorf("Regexp pattern %s is invalid: %w", pattern, err))
|
return data, errors.Unwrap(fmt.Errorf("Regexp pattern %s is invalid: %w", pattern, err))
|
||||||
@@ -76,7 +77,7 @@ func parseFile(input io.Reader, pattern string) (Tabdata, error) {
|
|||||||
} else {
|
} else {
|
||||||
// data processing
|
// data processing
|
||||||
if len(pattern) > 0 {
|
if len(pattern) > 0 {
|
||||||
if patternR.MatchString(line) == InvertMatch {
|
if patternR.MatchString(line) == c.InvertMatch {
|
||||||
// by default -v is false, so if a line does NOT
|
// by default -v is false, so if a line does NOT
|
||||||
// match the pattern, we will ignore it. However,
|
// match the pattern, we will ignore it. However,
|
||||||
// if the user specified -v, the matching is inverted,
|
// if the user specified -v, the matching is inverted,
|
||||||
@@ -119,7 +120,7 @@ func parseFile(input io.Reader, pattern string) (Tabdata, error) {
|
|||||||
return data, errors.Unwrap(fmt.Errorf("Failed to read from io.Reader: %w", scanner.Err()))
|
return data, errors.Unwrap(fmt.Errorf("Failed to read from io.Reader: %w", scanner.Err()))
|
||||||
}
|
}
|
||||||
|
|
||||||
if Debug {
|
if c.Debug {
|
||||||
repr.Print(data)
|
repr.Print(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ package lib
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/tlinden/tablizer/cfg"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -49,15 +50,15 @@ asd igig cxxxncnc
|
|||||||
19191 EDD 1 X`
|
19191 EDD 1 X`
|
||||||
|
|
||||||
readFd := strings.NewReader(table)
|
readFd := strings.NewReader(table)
|
||||||
gotdata, err := parseFile(readFd, "")
|
c := cfg.Config{Separator: cfg.DefaultSeparator}
|
||||||
Separator = DefaultSeparator
|
gotdata, err := parseFile(c, readFd, "")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, gotdata)
|
t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, gotdata)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !reflect.DeepEqual(data, gotdata) {
|
if !reflect.DeepEqual(data, gotdata) {
|
||||||
t.Errorf("Parser returned invalid data, Regex: %s\nExp: %+v\nGot: %+v\n", Separator, data, gotdata)
|
t.Errorf("Parser returned invalid data, Regex: %s\nExp: %+v\nGot: %+v\n", c.Separator, data, gotdata)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,6 +67,7 @@ func TestParserPatternmatching(t *testing.T) {
|
|||||||
entries [][]string
|
entries [][]string
|
||||||
pattern string
|
pattern string
|
||||||
invert bool
|
invert bool
|
||||||
|
want bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
entries: [][]string{
|
entries: [][]string{
|
||||||
@@ -85,6 +87,15 @@ func TestParserPatternmatching(t *testing.T) {
|
|||||||
pattern: "ig",
|
pattern: "ig",
|
||||||
invert: true,
|
invert: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
entries: [][]string{
|
||||||
|
{
|
||||||
|
"asd", "igig", "cxxxncnc",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
pattern: "[a-z",
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
table := `ONE TWO THREE
|
table := `ONE TWO THREE
|
||||||
@@ -94,19 +105,21 @@ asd igig cxxxncnc
|
|||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
testname := fmt.Sprintf("parse-with-pattern-%s-inverted-%t", tt.pattern, tt.invert)
|
testname := fmt.Sprintf("parse-with-pattern-%s-inverted-%t", tt.pattern, tt.invert)
|
||||||
t.Run(testname, func(t *testing.T) {
|
t.Run(testname, func(t *testing.T) {
|
||||||
InvertMatch = tt.invert
|
c := cfg.Config{InvertMatch: tt.invert, Pattern: tt.pattern, Separator: cfg.DefaultSeparator}
|
||||||
|
|
||||||
readFd := strings.NewReader(table)
|
readFd := strings.NewReader(table)
|
||||||
gotdata, err := parseFile(readFd, tt.pattern)
|
gotdata, err := parseFile(c, readFd, tt.pattern)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if !tt.want {
|
||||||
t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, gotdata)
|
t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, gotdata)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
if !reflect.DeepEqual(tt.entries, gotdata.entries) {
|
if !reflect.DeepEqual(tt.entries, gotdata.entries) {
|
||||||
t.Errorf("Parser returned invalid data (pattern: %s, invert: %t)\nExp: %+v\nGot: %+v\n",
|
t.Errorf("Parser returned invalid data (pattern: %s, invert: %t)\nExp: %+v\nGot: %+v\n",
|
||||||
tt.pattern, tt.invert, tt.entries, gotdata.entries)
|
tt.pattern, tt.invert, tt.entries, gotdata.entries)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -136,8 +149,8 @@ asd igig
|
|||||||
19191 EDD 1 X`
|
19191 EDD 1 X`
|
||||||
|
|
||||||
readFd := strings.NewReader(table)
|
readFd := strings.NewReader(table)
|
||||||
gotdata, err := parseFile(readFd, "")
|
c := cfg.Config{Separator: cfg.DefaultSeparator}
|
||||||
Separator = DefaultSeparator
|
gotdata, err := parseFile(c, readFd, "")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, gotdata)
|
t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, gotdata)
|
||||||
@@ -145,6 +158,6 @@ asd igig
|
|||||||
|
|
||||||
if !reflect.DeepEqual(data, gotdata) {
|
if !reflect.DeepEqual(data, gotdata) {
|
||||||
t.Errorf("Parser returned invalid data, Regex: %s\nExp: %+v\nGot: %+v\n",
|
t.Errorf("Parser returned invalid data, Regex: %s\nExp: %+v\nGot: %+v\n",
|
||||||
Separator, data, gotdata)
|
c.Separator, data, gotdata)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
123
lib/printer.go
123
lib/printer.go
@@ -21,44 +21,53 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gookit/color"
|
"github.com/gookit/color"
|
||||||
"github.com/olekukonko/tablewriter"
|
"github.com/olekukonko/tablewriter"
|
||||||
|
"github.com/tlinden/tablizer/cfg"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func printData(data *Tabdata) {
|
func printData(w io.Writer, c cfg.Config, data *Tabdata) {
|
||||||
// some output preparations:
|
// some output preparations:
|
||||||
|
|
||||||
if OutputMode != "shell" {
|
// add numbers to headers and remove this we're not interested in
|
||||||
// not needed in eval string
|
numberizeAndReduceHeaders(c, data)
|
||||||
numberizeHeaders(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
// remove unwanted columns, if any
|
// remove unwanted columns, if any
|
||||||
reduceColumns(data)
|
reduceColumns(c, data)
|
||||||
|
|
||||||
// sort the data
|
// sort the data
|
||||||
sortTable(data, SortByColumn)
|
sortTable(c, data)
|
||||||
|
|
||||||
switch OutputMode {
|
switch c.OutputMode {
|
||||||
case "extended":
|
case "extended":
|
||||||
printExtendedData(data)
|
printExtendedData(w, c, data)
|
||||||
case "ascii":
|
case "ascii":
|
||||||
printAsciiData(data)
|
printAsciiData(w, c, data)
|
||||||
case "orgtbl":
|
case "orgtbl":
|
||||||
printOrgmodeData(data)
|
printOrgmodeData(w, c, data)
|
||||||
case "markdown":
|
case "markdown":
|
||||||
printMarkdownData(data)
|
printMarkdownData(w, c, data)
|
||||||
case "shell":
|
case "shell":
|
||||||
printShellData(data)
|
printShellData(w, c, data)
|
||||||
|
case "yaml":
|
||||||
|
printYamlData(w, c, data)
|
||||||
default:
|
default:
|
||||||
printAsciiData(data)
|
printAsciiData(w, c, data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func output(w io.Writer, str string) {
|
||||||
|
fmt.Fprint(w, str)
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Emacs org-mode compatible table (also orgtbl-mode)
|
Emacs org-mode compatible table (also orgtbl-mode)
|
||||||
*/
|
*/
|
||||||
func printOrgmodeData(data *Tabdata) {
|
func printOrgmodeData(w io.Writer, c cfg.Config, data *Tabdata) {
|
||||||
tableString := &strings.Builder{}
|
tableString := &strings.Builder{}
|
||||||
table := tablewriter.NewWriter(tableString)
|
table := tablewriter.NewWriter(tableString)
|
||||||
|
|
||||||
@@ -81,19 +90,19 @@ func printOrgmodeData(data *Tabdata) {
|
|||||||
| cell | cell |
|
| cell | cell |
|
||||||
|------+------|
|
|------+------|
|
||||||
*/
|
*/
|
||||||
leftR := regexp.MustCompile("(?m)^\\+")
|
leftR := regexp.MustCompile(`(?m)^\\+`)
|
||||||
rightR := regexp.MustCompile("\\+(?m)$")
|
rightR := regexp.MustCompile(`\\+(?m)$`)
|
||||||
|
|
||||||
color.Print(
|
output(w, color.Sprint(
|
||||||
colorizeData(
|
colorizeData(c,
|
||||||
rightR.ReplaceAllString(
|
rightR.ReplaceAllString(
|
||||||
leftR.ReplaceAllString(tableString.String(), "|"), "|")))
|
leftR.ReplaceAllString(tableString.String(), "|"), "|"))))
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Markdown table
|
Markdown table
|
||||||
*/
|
*/
|
||||||
func printMarkdownData(data *Tabdata) {
|
func printMarkdownData(w io.Writer, c cfg.Config, data *Tabdata) {
|
||||||
tableString := &strings.Builder{}
|
tableString := &strings.Builder{}
|
||||||
table := tablewriter.NewWriter(tableString)
|
table := tablewriter.NewWriter(tableString)
|
||||||
|
|
||||||
@@ -107,13 +116,13 @@ func printMarkdownData(data *Tabdata) {
|
|||||||
table.SetCenterSeparator("|")
|
table.SetCenterSeparator("|")
|
||||||
|
|
||||||
table.Render()
|
table.Render()
|
||||||
color.Print(colorizeData(tableString.String()))
|
output(w, color.Sprint(colorizeData(c, tableString.String())))
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Simple ASCII table without any borders etc, just like the input we expect
|
Simple ASCII table without any borders etc, just like the input we expect
|
||||||
*/
|
*/
|
||||||
func printAsciiData(data *Tabdata) {
|
func printAsciiData(w io.Writer, c cfg.Config, data *Tabdata) {
|
||||||
tableString := &strings.Builder{}
|
tableString := &strings.Builder{}
|
||||||
table := tablewriter.NewWriter(tableString)
|
table := tablewriter.NewWriter(tableString)
|
||||||
|
|
||||||
@@ -137,47 +146,81 @@ func printAsciiData(data *Tabdata) {
|
|||||||
table.SetNoWhiteSpace(true)
|
table.SetNoWhiteSpace(true)
|
||||||
|
|
||||||
table.Render()
|
table.Render()
|
||||||
color.Print(colorizeData(tableString.String()))
|
output(w, color.Sprint(colorizeData(c, tableString.String())))
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
We simulate the \x command of psql (the PostgreSQL client)
|
We simulate the \x command of psql (the PostgreSQL client)
|
||||||
*/
|
*/
|
||||||
func printExtendedData(data *Tabdata) {
|
func printExtendedData(w io.Writer, c cfg.Config, data *Tabdata) {
|
||||||
// needed for data output
|
// needed for data output
|
||||||
format := fmt.Sprintf("%%%ds: %%s\n", data.maxwidthHeader)
|
format := fmt.Sprintf("%%%ds: %%s\n", data.maxwidthHeader)
|
||||||
|
out := ""
|
||||||
if len(data.entries) > 0 {
|
if len(data.entries) > 0 {
|
||||||
for _, entry := range data.entries {
|
for _, entry := range data.entries {
|
||||||
for i, value := range entry {
|
for i, value := range entry {
|
||||||
color.Printf(format, data.headers[i], value)
|
out += color.Sprintf(format, data.headers[i], value)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println()
|
out += "\n"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output(w, colorizeData(c, out))
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Shell output, ready to be eval'd. Just like FreeBSD stat(1)
|
Shell output, ready to be eval'd. Just like FreeBSD stat(1)
|
||||||
*/
|
*/
|
||||||
func printShellData(data *Tabdata) {
|
func printShellData(w io.Writer, c cfg.Config, data *Tabdata) {
|
||||||
|
out := ""
|
||||||
if len(data.entries) > 0 {
|
if len(data.entries) > 0 {
|
||||||
var idx int
|
|
||||||
for _, entry := range data.entries {
|
for _, entry := range data.entries {
|
||||||
idx = 0
|
|
||||||
shentries := []string{}
|
shentries := []string{}
|
||||||
for i, value := range entry {
|
for i, value := range entry {
|
||||||
if len(Columns) > 0 {
|
shentries = append(shentries, fmt.Sprintf("%s=\"%s\"",
|
||||||
if !contains(UseColumns, i+1) {
|
data.headers[i], value))
|
||||||
continue
|
}
|
||||||
|
out += fmt.Sprint(strings.Join(shentries, " ")) + "\n"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
shentries = append(shentries, fmt.Sprintf("%s=\"%s\"", data.headers[idx], value))
|
// no colrization here
|
||||||
idx++
|
output(w, out)
|
||||||
}
|
}
|
||||||
fmt.Println(strings.Join(shentries, " "))
|
|
||||||
}
|
func printYamlData(w io.Writer, c cfg.Config, data *Tabdata) {
|
||||||
}
|
type D struct {
|
||||||
|
Entries []map[string]interface{} `yaml:"entries"`
|
||||||
|
}
|
||||||
|
|
||||||
|
d := D{}
|
||||||
|
|
||||||
|
for _, entry := range data.entries {
|
||||||
|
ml := map[string]interface{}{}
|
||||||
|
|
||||||
|
for i, entry := range entry {
|
||||||
|
style := yaml.TaggedStyle
|
||||||
|
_, err := strconv.Atoi(entry)
|
||||||
|
if err != nil {
|
||||||
|
style = yaml.DoubleQuotedStyle
|
||||||
|
}
|
||||||
|
|
||||||
|
ml[strings.ToLower(data.headers[i])] =
|
||||||
|
&yaml.Node{
|
||||||
|
Kind: yaml.ScalarNode,
|
||||||
|
Style: style,
|
||||||
|
Value: entry}
|
||||||
|
}
|
||||||
|
|
||||||
|
d.Entries = append(d.Entries, ml)
|
||||||
|
}
|
||||||
|
|
||||||
|
yamlstr, err := yaml.Marshal(&d)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
output(w, string(yamlstr))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,218 +18,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
package lib
|
package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gookit/color"
|
//"github.com/alecthomas/repr"
|
||||||
"os"
|
"github.com/tlinden/tablizer/cfg"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func stdout2pipe(t *testing.T) (*os.File, *os.File) {
|
func newData() Tabdata {
|
||||||
reader, writer, err := os.Pipe()
|
return Tabdata{
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
origStdout := os.Stdout
|
|
||||||
os.Stdout = writer
|
|
||||||
|
|
||||||
// we need to tell the color mode the io.Writer, even if we don't usw colorization
|
|
||||||
color.SetOutput(writer)
|
|
||||||
|
|
||||||
return origStdout, reader
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPrinter(t *testing.T) {
|
|
||||||
startdata := Tabdata{
|
|
||||||
maxwidthHeader: 5,
|
|
||||||
maxwidthPerCol: []int{
|
|
||||||
5,
|
|
||||||
5,
|
|
||||||
8,
|
|
||||||
},
|
|
||||||
columns: 3,
|
|
||||||
headers: []string{
|
|
||||||
"ONE", "TWO", "THREE",
|
|
||||||
},
|
|
||||||
entries: [][]string{
|
|
||||||
{
|
|
||||||
"asd", "igig", "cxxxncnc",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"19191", "EDD 1", "X",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
expects := map[string]string{
|
|
||||||
"ascii": `ONE(1) TWO(2) THREE(3)
|
|
||||||
asd igig cxxxncnc
|
|
||||||
19191 EDD 1 X`,
|
|
||||||
|
|
||||||
"orgtbl": `|--------+--------+----------|
|
|
||||||
| ONE(1) | TWO(2) | THREE(3) |
|
|
||||||
|--------+--------+----------|
|
|
||||||
| asd | igig | cxxxncnc |
|
|
||||||
| 19191 | EDD 1 | X |
|
|
||||||
|--------+--------+----------|`,
|
|
||||||
|
|
||||||
"markdown": `| ONE(1) | TWO(2) | THREE(3) |
|
|
||||||
|--------|--------|----------|
|
|
||||||
| asd | igig | cxxxncnc |
|
|
||||||
| 19191 | EDD 1 | X |`,
|
|
||||||
|
|
||||||
"shell": `ONE="asd" TWO="igig" THREE="cxxxncnc"
|
|
||||||
ONE="19191" TWO="EDD 1" THREE="X"`,
|
|
||||||
|
|
||||||
"extended": `ONE(1): asd
|
|
||||||
TWO(2): igig
|
|
||||||
THREE(3): cxxxncnc
|
|
||||||
|
|
||||||
ONE(1): 19191
|
|
||||||
TWO(2): EDD 1
|
|
||||||
THREE(3): X`,
|
|
||||||
}
|
|
||||||
|
|
||||||
NoColor = true
|
|
||||||
SortByColumn = 0 // disable sorting
|
|
||||||
|
|
||||||
origStdout, reader := stdout2pipe(t)
|
|
||||||
|
|
||||||
for mode, expect := range expects {
|
|
||||||
testname := fmt.Sprintf("print-%s", mode)
|
|
||||||
t.Run(testname, func(t *testing.T) {
|
|
||||||
|
|
||||||
OutputMode = mode
|
|
||||||
// we need to reset our mock data, since it's being
|
|
||||||
// modified in printData()
|
|
||||||
data := startdata
|
|
||||||
printData(&data)
|
|
||||||
|
|
||||||
buf := make([]byte, 1024)
|
|
||||||
n, err := reader.Read(buf)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
buf = buf[:n]
|
|
||||||
output := strings.TrimSpace(string(buf))
|
|
||||||
|
|
||||||
if output != expect {
|
|
||||||
t.Errorf("output mode: %s, got:\n%s\nwant:\n%s\n (%d <=> %d)",
|
|
||||||
mode, output, expect, len(output), len(expect))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Restore
|
|
||||||
os.Stdout = origStdout
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSortPrinter(t *testing.T) {
|
|
||||||
startdata := Tabdata{
|
|
||||||
maxwidthHeader: 5,
|
|
||||||
maxwidthPerCol: []int{
|
|
||||||
3,
|
|
||||||
3,
|
|
||||||
2,
|
|
||||||
},
|
|
||||||
columns: 3,
|
|
||||||
headers: []string{
|
|
||||||
"ONE", "TWO", "THREE",
|
|
||||||
},
|
|
||||||
entries: [][]string{
|
|
||||||
{
|
|
||||||
"abc", "345", "b1",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"bcd", "234", "a2",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cde", "123", "c3",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
var tests = []struct {
|
|
||||||
data Tabdata
|
|
||||||
sortby int
|
|
||||||
desc bool
|
|
||||||
expect string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
data: startdata,
|
|
||||||
sortby: 1,
|
|
||||||
desc: false,
|
|
||||||
expect: `ONE(1) TWO(2) THREE(3)
|
|
||||||
abc 345 b1
|
|
||||||
bcd 234 a2
|
|
||||||
cde 123 c3`,
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
data: startdata,
|
|
||||||
sortby: 2,
|
|
||||||
desc: false,
|
|
||||||
expect: `ONE(1) TWO(2) THREE(3)
|
|
||||||
cde 123 c3
|
|
||||||
bcd 234 a2
|
|
||||||
abc 345 b1`,
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
data: startdata,
|
|
||||||
sortby: 3,
|
|
||||||
desc: false,
|
|
||||||
expect: `ONE(1) TWO(2) THREE(3)
|
|
||||||
bcd 234 a2
|
|
||||||
abc 345 b1
|
|
||||||
cde 123 c3`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
data: startdata,
|
|
||||||
sortby: 1,
|
|
||||||
desc: true,
|
|
||||||
expect: `ONE(1) TWO(2) THREE(3)
|
|
||||||
cde 123 c3
|
|
||||||
bcd 234 a2
|
|
||||||
abc 345 b1`,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
NoColor = true
|
|
||||||
OutputMode = "ascii"
|
|
||||||
origStdout, reader := stdout2pipe(t)
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
testname := fmt.Sprintf("print-sorted-table-by-column-%d-desc-%t",
|
|
||||||
tt.sortby, tt.desc)
|
|
||||||
t.Run(testname, func(t *testing.T) {
|
|
||||||
SortByColumn = tt.sortby
|
|
||||||
SortDescending = tt.desc
|
|
||||||
|
|
||||||
printData(&tt.data)
|
|
||||||
|
|
||||||
buf := make([]byte, 1024)
|
|
||||||
n, err := reader.Read(buf)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
buf = buf[:n]
|
|
||||||
output := strings.TrimSpace(string(buf))
|
|
||||||
|
|
||||||
if output != tt.expect {
|
|
||||||
t.Errorf("sort column: %d, got:\n%s\nwant:\n%s",
|
|
||||||
tt.sortby, output, tt.expect)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Restore
|
|
||||||
os.Stdout = origStdout
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSortByPrinter(t *testing.T) {
|
|
||||||
data := Tabdata{
|
|
||||||
maxwidthHeader: 8,
|
maxwidthHeader: 8,
|
||||||
maxwidthPerCol: []int{
|
maxwidthPerCol: []int{
|
||||||
5,
|
5,
|
||||||
@@ -265,73 +63,226 @@ func TestSortByPrinter(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
sortby string
|
name string // so we can identify which one fails, can be the same
|
||||||
column int
|
// for multiple tests, because flags will be appended to the name
|
||||||
desc bool
|
sortby string // empty == default
|
||||||
expect string
|
column int // sort by this column, 0 == default first or NO Sort
|
||||||
}{
|
desc bool // sort in descending order, default == ascending
|
||||||
|
nonum bool // hide numbering
|
||||||
|
mode string // shell, orgtbl, etc. empty == default: ascii
|
||||||
|
usecol []int // columns to display, empty == display all
|
||||||
|
usecolstr string // for testname, must match usecol
|
||||||
|
expect string // rendered output we expect
|
||||||
|
}{
|
||||||
|
// --------------------- Default settings mode tests ``
|
||||||
{
|
{
|
||||||
|
mode: "ascii",
|
||||||
|
name: "default",
|
||||||
|
expect: `
|
||||||
|
NAME(1) DURATION(2) COUNT(3) WHEN(4)
|
||||||
|
beta 1d10h5m1s 33 3/1/2014
|
||||||
|
alpha 4h35m 170 2013-Feb-03
|
||||||
|
ceta 33d12h 9 06/Jan/2008 15:04:05 -0700`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "default",
|
||||||
|
mode: "orgtbl",
|
||||||
|
expect: `
|
||||||
|
+---------+-------------+----------+----------------------------+
|
||||||
|
| NAME(1) | DURATION(2) | COUNT(3) | WHEN(4) |
|
||||||
|
+---------+-------------+----------+----------------------------+
|
||||||
|
| beta | 1d10h5m1s | 33 | 3/1/2014 |
|
||||||
|
| alpha | 4h35m | 170 | 2013-Feb-03 |
|
||||||
|
| ceta | 33d12h | 9 | 06/Jan/2008 15:04:05 -0700 |
|
||||||
|
+---------+-------------+----------+----------------------------+`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "default",
|
||||||
|
mode: "markdown",
|
||||||
|
expect: `
|
||||||
|
| NAME(1) | DURATION(2) | COUNT(3) | WHEN(4) |
|
||||||
|
|---------|-------------|----------|----------------------------|
|
||||||
|
| beta | 1d10h5m1s | 33 | 3/1/2014 |
|
||||||
|
| alpha | 4h35m | 170 | 2013-Feb-03 |
|
||||||
|
| ceta | 33d12h | 9 | 06/Jan/2008 15:04:05 -0700 |`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "default",
|
||||||
|
mode: "shell",
|
||||||
|
nonum: true,
|
||||||
|
expect: `
|
||||||
|
NAME="beta" DURATION="1d10h5m1s" COUNT="33" WHEN="3/1/2014"
|
||||||
|
NAME="alpha" DURATION="4h35m" COUNT="170" WHEN="2013-Feb-03"
|
||||||
|
NAME="ceta" DURATION="33d12h" COUNT="9" WHEN="06/Jan/2008 15:04:05 -0700"`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "default",
|
||||||
|
mode: "yaml",
|
||||||
|
nonum: true,
|
||||||
|
expect: `
|
||||||
|
entries:
|
||||||
|
- count: 33
|
||||||
|
duration: "1d10h5m1s"
|
||||||
|
name: "beta"
|
||||||
|
when: "3/1/2014"
|
||||||
|
- count: 170
|
||||||
|
duration: "4h35m"
|
||||||
|
name: "alpha"
|
||||||
|
when: "2013-Feb-03"
|
||||||
|
- count: 9
|
||||||
|
duration: "33d12h"
|
||||||
|
name: "ceta"
|
||||||
|
when: "06/Jan/2008 15:04:05 -0700"`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "default",
|
||||||
|
mode: "extended",
|
||||||
|
expect: `
|
||||||
|
NAME(1): beta
|
||||||
|
DURATION(2): 1d10h5m1s
|
||||||
|
COUNT(3): 33
|
||||||
|
WHEN(4): 3/1/2014
|
||||||
|
|
||||||
|
NAME(1): alpha
|
||||||
|
DURATION(2): 4h35m
|
||||||
|
COUNT(3): 170
|
||||||
|
WHEN(4): 2013-Feb-03
|
||||||
|
|
||||||
|
NAME(1): ceta
|
||||||
|
DURATION(2): 33d12h
|
||||||
|
COUNT(3): 9
|
||||||
|
WHEN(4): 06/Jan/2008 15:04:05 -0700`,
|
||||||
|
},
|
||||||
|
|
||||||
|
//------------------------ SORT TESTS
|
||||||
|
{
|
||||||
|
name: "sortbycolumn",
|
||||||
column: 3,
|
column: 3,
|
||||||
sortby: "numeric",
|
sortby: "numeric",
|
||||||
desc: false,
|
desc: false,
|
||||||
expect: `NAME(1) DURATION(2) COUNT(3) WHEN(4)
|
expect: `
|
||||||
|
NAME(1) DURATION(2) COUNT(3) WHEN(4)
|
||||||
ceta 33d12h 9 06/Jan/2008 15:04:05 -0700
|
ceta 33d12h 9 06/Jan/2008 15:04:05 -0700
|
||||||
beta 1d10h5m1s 33 3/1/2014
|
beta 1d10h5m1s 33 3/1/2014
|
||||||
alpha 4h35m 170 2013-Feb-03`,
|
alpha 4h35m 170 2013-Feb-03`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
column: 2,
|
name: "sortbycolumn",
|
||||||
sortby: "duration",
|
|
||||||
desc: false,
|
|
||||||
expect: `NAME(1) DURATION(2) COUNT(3) WHEN(4)
|
|
||||||
alpha 4h35m 170 2013-Feb-03
|
|
||||||
beta 1d10h5m1s 33 3/1/2014
|
|
||||||
ceta 33d12h 9 06/Jan/2008 15:04:05 -0700`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
column: 4,
|
column: 4,
|
||||||
sortby: "time",
|
sortby: "time",
|
||||||
desc: false,
|
desc: false,
|
||||||
expect: `NAME(1) DURATION(2) COUNT(3) WHEN(4)
|
expect: `
|
||||||
|
NAME(1) DURATION(2) COUNT(3) WHEN(4)
|
||||||
ceta 33d12h 9 06/Jan/2008 15:04:05 -0700
|
ceta 33d12h 9 06/Jan/2008 15:04:05 -0700
|
||||||
alpha 4h35m 170 2013-Feb-03
|
alpha 4h35m 170 2013-Feb-03
|
||||||
beta 1d10h5m1s 33 3/1/2014`,
|
beta 1d10h5m1s 33 3/1/2014`,
|
||||||
},
|
},
|
||||||
}
|
{
|
||||||
|
name: "sortbycolumn",
|
||||||
|
column: 2,
|
||||||
|
sortby: "duration",
|
||||||
|
desc: false,
|
||||||
|
expect: `
|
||||||
|
NAME(1) DURATION(2) COUNT(3) WHEN(4)
|
||||||
|
alpha 4h35m 170 2013-Feb-03
|
||||||
|
beta 1d10h5m1s 33 3/1/2014
|
||||||
|
ceta 33d12h 9 06/Jan/2008 15:04:05 -0700`,
|
||||||
|
},
|
||||||
|
|
||||||
NoColor = true
|
// ----------------------- UseColumns Tests
|
||||||
OutputMode = "ascii"
|
{
|
||||||
origStdout, reader := stdout2pipe(t)
|
name: "usecolumns",
|
||||||
|
usecol: []int{1, 4},
|
||||||
|
usecolstr: "1,4",
|
||||||
|
expect: `
|
||||||
|
NAME(1) WHEN(4)
|
||||||
|
beta 3/1/2014
|
||||||
|
alpha 2013-Feb-03
|
||||||
|
ceta 06/Jan/2008 15:04:05 -0700`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "usecolumns",
|
||||||
|
usecol: []int{2},
|
||||||
|
usecolstr: "2",
|
||||||
|
expect: `
|
||||||
|
DURATION(2)
|
||||||
|
1d10h5m1s
|
||||||
|
4h35m
|
||||||
|
33d12h`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "usecolumns",
|
||||||
|
usecol: []int{3},
|
||||||
|
usecolstr: "3",
|
||||||
|
expect: `
|
||||||
|
COUNT(3)
|
||||||
|
33
|
||||||
|
170
|
||||||
|
9`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "usecolumns",
|
||||||
|
column: 0,
|
||||||
|
usecol: []int{1, 3},
|
||||||
|
usecolstr: "1,3",
|
||||||
|
expect: `
|
||||||
|
NAME(1) COUNT(3)
|
||||||
|
beta 33
|
||||||
|
alpha 170
|
||||||
|
ceta 9`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "usecolumns",
|
||||||
|
usecol: []int{2, 4},
|
||||||
|
usecolstr: "2,4",
|
||||||
|
expect: `
|
||||||
|
DURATION(2) WHEN(4)
|
||||||
|
1d10h5m1s 3/1/2014
|
||||||
|
4h35m 2013-Feb-03
|
||||||
|
33d12h 06/Jan/2008 15:04:05 -0700`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrinter(t *testing.T) {
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
testname := fmt.Sprintf("print-sorted-table-by-column-%d-desc-%t-sort-by-%s",
|
testname := fmt.Sprintf("print-sortcol-%d-desc-%t-sortby-%s-mode-%s-usecolumns-%s",
|
||||||
tt.column, tt.desc, tt.sortby)
|
tt.column, tt.desc, tt.sortby, tt.mode, tt.usecolstr)
|
||||||
|
|
||||||
t.Run(testname, func(t *testing.T) {
|
t.Run(testname, func(t *testing.T) {
|
||||||
SortByColumn = tt.column
|
// replaces os.Stdout, but we ignore it
|
||||||
SortDescending = tt.desc
|
var w bytes.Buffer
|
||||||
SortMode = tt.sortby
|
|
||||||
|
|
||||||
testdata := data
|
// cmd flags
|
||||||
printData(&testdata)
|
c := cfg.Config{
|
||||||
|
SortByColumn: tt.column,
|
||||||
buf := make([]byte, 1024)
|
SortDescending: tt.desc,
|
||||||
n, err := reader.Read(buf)
|
SortMode: tt.sortby,
|
||||||
if err != nil {
|
OutputMode: tt.mode,
|
||||||
t.Fatal(err)
|
NoNumbering: tt.nonum,
|
||||||
|
UseColumns: tt.usecol,
|
||||||
|
NoColor: true,
|
||||||
}
|
}
|
||||||
buf = buf[:n]
|
|
||||||
output := strings.TrimSpace(string(buf))
|
|
||||||
|
|
||||||
if output != tt.expect {
|
// the test checks the len!
|
||||||
t.Errorf("sort column: %d, sortby: %s, got:\n%s\nwant:\n%s",
|
if len(tt.usecol) > 0 {
|
||||||
tt.column, tt.sortby, output, tt.expect)
|
c.Columns = "yes"
|
||||||
|
} else {
|
||||||
|
c.Columns = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
testdata := newData()
|
||||||
|
exp := strings.TrimSpace(tt.expect)
|
||||||
|
|
||||||
|
printData(&w, c, &testdata)
|
||||||
|
|
||||||
|
got := strings.TrimSpace(w.String())
|
||||||
|
|
||||||
|
if got != exp {
|
||||||
|
t.Errorf("not rendered correctly:\n+++ got:\n%s\n+++ want:\n%s",
|
||||||
|
got, exp)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restore
|
|
||||||
os.Stdout = origStdout
|
|
||||||
}
|
}
|
||||||
|
|||||||
65
lib/sort.go
65
lib/sort.go
@@ -19,17 +19,21 @@ package lib
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/araddon/dateparse"
|
"github.com/araddon/dateparse"
|
||||||
str2duration "github.com/xhit/go-str2duration/v2"
|
"github.com/tlinden/tablizer/cfg"
|
||||||
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func sortTable(data *Tabdata, col int) {
|
func sortTable(c cfg.Config, data *Tabdata) {
|
||||||
if col <= 0 {
|
if c.SortByColumn <= 0 {
|
||||||
// no sorting wanted
|
// no sorting wanted
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// slightly modified here to match internal array indicies
|
||||||
|
col := c.SortByColumn
|
||||||
|
|
||||||
col-- // ui starts counting by 1, but use 0 internally
|
col-- // ui starts counting by 1, but use 0 internally
|
||||||
|
|
||||||
// sanity checks
|
// sanity checks
|
||||||
@@ -44,14 +48,15 @@ func sortTable(data *Tabdata, col int) {
|
|||||||
|
|
||||||
// actual sorting
|
// actual sorting
|
||||||
sort.SliceStable(data.entries, func(i, j int) bool {
|
sort.SliceStable(data.entries, func(i, j int) bool {
|
||||||
return compare(data.entries[i][col], data.entries[j][col])
|
return compare(&c, data.entries[i][col], data.entries[j][col])
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func compare(a string, b string) bool {
|
// config is not modified here, but it would be inefficient to copy it every loop
|
||||||
|
func compare(c *cfg.Config, a string, b string) bool {
|
||||||
var comp bool
|
var comp bool
|
||||||
|
|
||||||
switch SortMode {
|
switch c.SortMode {
|
||||||
case "numeric":
|
case "numeric":
|
||||||
left, err := strconv.Atoi(a)
|
left, err := strconv.Atoi(a)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -63,15 +68,9 @@ func compare(a string, b string) bool {
|
|||||||
}
|
}
|
||||||
comp = left < right
|
comp = left < right
|
||||||
case "duration":
|
case "duration":
|
||||||
left, err := str2duration.ParseDuration(a)
|
left := duration2int(a)
|
||||||
if err != nil {
|
right := duration2int(b)
|
||||||
left = 0
|
comp = left < right
|
||||||
}
|
|
||||||
right, err := str2duration.ParseDuration(b)
|
|
||||||
if err != nil {
|
|
||||||
right = 0
|
|
||||||
}
|
|
||||||
comp = left.Seconds() < right.Seconds()
|
|
||||||
case "time":
|
case "time":
|
||||||
left, _ := dateparse.ParseAny(a)
|
left, _ := dateparse.ParseAny(a)
|
||||||
right, _ := dateparse.ParseAny(b)
|
right, _ := dateparse.ParseAny(b)
|
||||||
@@ -80,9 +79,43 @@ func compare(a string, b string) bool {
|
|||||||
comp = a < b
|
comp = a < b
|
||||||
}
|
}
|
||||||
|
|
||||||
if SortDescending {
|
if c.SortDescending {
|
||||||
comp = !comp
|
comp = !comp
|
||||||
}
|
}
|
||||||
|
|
||||||
return comp
|
return comp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
We could use time.ParseDuration(), but this doesn't support days.
|
||||||
|
|
||||||
|
We could also use github.com/xhit/go-str2duration/v2, which does
|
||||||
|
the job, but it's just another dependency, just for this little
|
||||||
|
gem. And we don't need a time.Time value. And int is good enough
|
||||||
|
for duration comparision.
|
||||||
|
|
||||||
|
Convert a durartion into an integer. Valid time units are "s",
|
||||||
|
"m", "h" and "d".
|
||||||
|
*/
|
||||||
|
func duration2int(duration string) int {
|
||||||
|
re := regexp.MustCompile(`(\d+)([dhms])`)
|
||||||
|
seconds := 0
|
||||||
|
|
||||||
|
for _, match := range re.FindAllStringSubmatch(duration, -1) {
|
||||||
|
if len(match) == 3 {
|
||||||
|
v, _ := strconv.Atoi(match[1])
|
||||||
|
switch match[2][0] {
|
||||||
|
case 'd':
|
||||||
|
seconds += v * 86400
|
||||||
|
case 'h':
|
||||||
|
seconds += v * 3600
|
||||||
|
case 'm':
|
||||||
|
seconds += v * 60
|
||||||
|
case 's':
|
||||||
|
seconds += v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return seconds
|
||||||
|
}
|
||||||
|
|||||||
79
lib/sort_test.go
Normal file
79
lib/sort_test.go
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2022 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
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package lib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/tlinden/tablizer/cfg"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDuration2Seconds(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
dur string
|
||||||
|
expect int
|
||||||
|
}{
|
||||||
|
{"1d", 60 * 60 * 24},
|
||||||
|
{"1h", 60 * 60},
|
||||||
|
{"10m", 60 * 10},
|
||||||
|
{"2h4m10s", (60 * 120) + (4 * 60) + 10},
|
||||||
|
{"88u", 0},
|
||||||
|
{"19t77X what?4s", 4},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
testname := fmt.Sprintf("duration-%s", tt.dur)
|
||||||
|
t.Run(testname, func(t *testing.T) {
|
||||||
|
seconds := duration2int(tt.dur)
|
||||||
|
if seconds != tt.expect {
|
||||||
|
t.Errorf("got %d, want %d", seconds, tt.expect)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCompare(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
mode string
|
||||||
|
a string
|
||||||
|
b string
|
||||||
|
want bool
|
||||||
|
desc bool
|
||||||
|
}{
|
||||||
|
// ascending
|
||||||
|
{"numeric", "10", "20", true, false},
|
||||||
|
{"duration", "2d4h5m", "45m", false, false},
|
||||||
|
{"time", "12/24/2022", "1/1/1970", false, false},
|
||||||
|
|
||||||
|
// descending
|
||||||
|
{"numeric", "10", "20", false, true},
|
||||||
|
{"duration", "2d4h5m", "45m", true, true},
|
||||||
|
{"time", "12/24/2022", "1/1/1970", true, true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
testname := fmt.Sprintf("compare-mode-%s-a-%s-b-%s-desc-%t", tt.mode, tt.a, tt.b, tt.desc)
|
||||||
|
t.Run(testname, func(t *testing.T) {
|
||||||
|
c := cfg.Config{SortMode: tt.mode, SortDescending: tt.desc}
|
||||||
|
got := compare(&c, tt.a, tt.b)
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("got %t, want %t", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -133,7 +133,7 @@
|
|||||||
.\" ========================================================================
|
.\" ========================================================================
|
||||||
.\"
|
.\"
|
||||||
.IX Title "TABLIZER 1"
|
.IX Title "TABLIZER 1"
|
||||||
.TH TABLIZER 1 "2022-10-15" "1" "User Commands"
|
.TH TABLIZER 1 "2022-10-16" "1" "User Commands"
|
||||||
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
|
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
|
||||||
.\" way too many mistakes in technical documents.
|
.\" way too many mistakes in technical documents.
|
||||||
.if n .ad l
|
.if n .ad l
|
||||||
@@ -154,7 +154,7 @@ tablizer \- Manipulate tabular output of other programs
|
|||||||
\& \-m, \-\-man Display manual page
|
\& \-m, \-\-man Display manual page
|
||||||
\& \-n, \-\-no\-numbering Disable header numbering
|
\& \-n, \-\-no\-numbering Disable header numbering
|
||||||
\& \-N, \-\-no\-color Disable pattern highlighting
|
\& \-N, \-\-no\-color Disable pattern highlighting
|
||||||
\& \-o, \-\-output string Output mode \- one of: orgtbl, markdown, extended, ascii(default)
|
\& \-o, \-\-output string Output mode \- one of: orgtbl, markdown, extended, yaml, ascii(default)
|
||||||
\& \-X, \-\-extended Enable extended output
|
\& \-X, \-\-extended Enable extended output
|
||||||
\& \-M, \-\-markdown Enable markdown table output
|
\& \-M, \-\-markdown Enable markdown table output
|
||||||
\& \-O, \-\-orgtbl Enable org\-mode table output
|
\& \-O, \-\-orgtbl Enable org\-mode table output
|
||||||
@@ -345,7 +345,8 @@ You can use this in an eval loop.
|
|||||||
.PP
|
.PP
|
||||||
Beside normal ascii mode (the default) and extended mode there are
|
Beside normal ascii mode (the default) and extended mode there are
|
||||||
more output modes available: \fBorgtbl\fR which prints an Emacs org-mode
|
more output modes available: \fBorgtbl\fR which prints an Emacs org-mode
|
||||||
table and \fBmarkdown\fR which prints a Markdown table.
|
table and \fBmarkdown\fR which prints a Markdown table and \fByaml\fR, which
|
||||||
|
prints yaml encoding.
|
||||||
.SH "BUGS"
|
.SH "BUGS"
|
||||||
.IX Header "BUGS"
|
.IX Header "BUGS"
|
||||||
In order to report a bug, unexpected behavior, feature requests
|
In order to report a bug, unexpected behavior, feature requests
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ tablizer - Manipulate tabular output of other programs
|
|||||||
-m, --man Display manual page
|
-m, --man Display manual page
|
||||||
-n, --no-numbering Disable header numbering
|
-n, --no-numbering Disable header numbering
|
||||||
-N, --no-color Disable pattern highlighting
|
-N, --no-color Disable pattern highlighting
|
||||||
-o, --output string Output mode - one of: orgtbl, markdown, extended, ascii(default)
|
-o, --output string Output mode - one of: orgtbl, markdown, extended, yaml, ascii(default)
|
||||||
-X, --extended Enable extended output
|
-X, --extended Enable extended output
|
||||||
-M, --markdown Enable markdown table output
|
-M, --markdown Enable markdown table output
|
||||||
-O, --orgtbl Enable org-mode table output
|
-O, --orgtbl Enable org-mode table output
|
||||||
@@ -198,7 +198,8 @@ You can use this in an eval loop.
|
|||||||
|
|
||||||
Beside normal ascii mode (the default) and extended mode there are
|
Beside normal ascii mode (the default) and extended mode there are
|
||||||
more output modes available: B<orgtbl> which prints an Emacs org-mode
|
more output modes available: B<orgtbl> which prints an Emacs org-mode
|
||||||
table and B<markdown> which prints a Markdown table.
|
table and B<markdown> which prints a Markdown table and B<yaml>, which
|
||||||
|
prints yaml encoding.
|
||||||
|
|
||||||
=head1 BUGS
|
=head1 BUGS
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user