mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-18 13:01:11 +01:00
Compare commits
46 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8e2ba58ddb | |||
| 6eedb60a6a | |||
| 81fac864f1 | |||
| e868b50c0f | |||
| b9ed7d8cb7 | |||
| 6ae4a1b6d9 | |||
| f890596b4c | |||
| 22ee24cfdf | |||
| 34e2b8d855 | |||
| 196833ed3c | |||
| 85277bbf5e | |||
| 26e50cf908 | |||
| 5be18e27c9 | |||
| 2c410e1cb3 | |||
| 1b622284a1 | |||
| 404481c3dc | |||
| 15f437314a | |||
| 3746c7f326 | |||
| b7b638636d | |||
| dd13300c8b | |||
| a59a6cb7d8 | |||
| d7ea0017b7 | |||
| 09dc1f3e60 | |||
| 43dc4ff031 | |||
| 4596d9d589 | |||
|
|
f2acd2c1b1 | ||
| 76f49a532f | |||
| 3fd2e6ac2f | |||
| 65cbaddd5f | |||
| 9f5fc6924e | |||
| 07b65bcff5 | |||
|
|
2f46716a7a | ||
| e6723a6951 | |||
| 66c4b68036 | |||
| 4ca3a56280 | |||
|
|
487470818c | ||
| 1b1b63caa3 | |||
| d38bae0dd1 | |||
| 282e87d8cc | |||
| a9979714ba | |||
| f4dc6c62e6 | |||
| c8ebf7fde2 | |||
| eda702c914 | |||
| 19dabb7385 | |||
| e617e52127 | |||
| a09f7b59c2 |
25
.github/workflows/ci.yaml
vendored
Normal file
25
.github/workflows/ci.yaml
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
name: build-and-test-tablizer
|
||||
on: [push, pull_request]
|
||||
jobs:
|
||||
build:
|
||||
strategy:
|
||||
matrix:
|
||||
version: [1.17, 1.18, 1.19]
|
||||
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||
name: Build
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Set up Go 1.18
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: ${{ matrix.version }}
|
||||
id: go
|
||||
|
||||
- name: checkout
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: build
|
||||
run: make
|
||||
|
||||
- name: test
|
||||
run: make test
|
||||
30
Makefile
30
Makefile
@@ -18,23 +18,34 @@
|
||||
#
|
||||
# no need to modify anything below
|
||||
tool = tablizer
|
||||
version = $(shell egrep "^var version = " cmd/root.go | cut -d'=' -f2 | cut -d'"' -f 2)
|
||||
version = $(shell egrep "= .v" lib/common.go | cut -d'=' -f2 | cut -d'"' -f 2)
|
||||
archs = android darwin freebsd linux netbsd openbsd windows
|
||||
PREFIX = /usr/local
|
||||
UID = root
|
||||
GID = 0
|
||||
BRANCH = $(shell git describe --all | cut -d/ -f2)
|
||||
COMMIT = $(shell git rev-parse --short=8 HEAD)
|
||||
BUILD = $(shell date +%Y.%m.%d.%H%M%S)
|
||||
VERSION:= $(if $(filter $(BRANCH), development),$(version)-$(BRANCH)-$(COMMIT)-$(BUILD),$(version))
|
||||
|
||||
all: buildlocal man
|
||||
|
||||
man:
|
||||
pod2man -c "User Commands" -r 1 -s 1 $(tool).pod > $(tool).1
|
||||
all: $(tool).1 cmd/$(tool).go buildlocal
|
||||
|
||||
%.1: %.pod
|
||||
pod2man -c "User Commands" -r 1 -s 1 $*.pod > $*.1
|
||||
|
||||
cmd/%.go: %.pod
|
||||
echo "package cmd" > cmd/$*.go
|
||||
echo "var manpage = \`" >> cmd/$*.go
|
||||
pod2text $*.pod >> cmd/$*.go
|
||||
echo "\`" >> cmd/$*.go
|
||||
|
||||
buildlocal:
|
||||
go build
|
||||
go build -ldflags "-X 'github.com/tlinden/tablizer/lib.VERSION=$(VERSION)'"
|
||||
|
||||
release:
|
||||
mkdir -p releases
|
||||
$(foreach arch,$(archs), GOOS=$(arch) GOARCH=amd64 go build -x -o releases/$(tool)-$(arch)-amd64-$(version); sha256sum releases/$(tool)-$(arch)-amd64-$(version) | cut -d' ' -f1 > releases/$(tool)-$(arch)-amd64-$(version).sha256sum;)
|
||||
./mkrel.sh $(tool) $(version)
|
||||
gh release create $(version) --generate-notes releases/*
|
||||
|
||||
install: buildlocal
|
||||
install -d -o $(UID) -g $(GID) $(PREFIX)/bin
|
||||
@@ -43,4 +54,7 @@ install: buildlocal
|
||||
install -o $(UID) -g $(GID) -m 444 $(tool).1 $(PREFIX)/man/man1/
|
||||
|
||||
clean:
|
||||
rm -f $(tool) $(tool).1
|
||||
rm -rf $(tool) releases
|
||||
|
||||
test:
|
||||
go test -v ./...
|
||||
|
||||
19
README.md
19
README.md
@@ -1,3 +1,6 @@
|
||||
[](https://github.com/tlinden/tablizer/actions)
|
||||
[](https://github.com/tlinden/tablizer/blob/master/LICENSE)
|
||||
|
||||
## tablizer - Manipulate tabular output of other programs
|
||||
|
||||
Tablizer can be used to re-format tabular output of other
|
||||
@@ -17,13 +20,13 @@ But you're only interested in the NAME and STATUS columns. Here's how
|
||||
to do this with tablizer:
|
||||
|
||||
```
|
||||
% kubectl get pods | ./tablizer
|
||||
% kubectl get pods | tablizer
|
||||
NAME(1) READY(2) STATUS(3) RESTARTS(4) AGE(5)
|
||||
repldepl-7bcd8d5b64-7zq4l 1/1 Running 1 (69m ago) 5h26m
|
||||
repldepl-7bcd8d5b64-m48n8 1/1 Running 1 (69m ago) 5h26m
|
||||
repldepl-7bcd8d5b64-q2bf4 1/1 Running 1 (69m ago) 5h26m
|
||||
|
||||
% kubectl get pods | ./tablizer -c 1,3
|
||||
% kubectl get pods | tablizer -c 1,3
|
||||
NAME(1) STATUS(3)
|
||||
repldepl-7bcd8d5b64-7zq4l Running
|
||||
repldepl-7bcd8d5b64-m48n8 Running
|
||||
@@ -32,10 +35,11 @@ repldepl-7bcd8d5b64-q2bf4 Running
|
||||
|
||||
Another use case is when the tabular output is so wide that lines are
|
||||
being broken and the whole output is completely distorted. In such a
|
||||
case you can use the `-x` flag to get an output similar to `\x` in `psql`:
|
||||
case you can use the `-o extended | -X` flag to get an output similar
|
||||
to `\x` in `psql`:
|
||||
|
||||
```
|
||||
% kubectl get pods | ./tablizer -x
|
||||
% kubectl get pods | tablizer -X
|
||||
NAME: repldepl-7bcd8d5b64-7zq4l
|
||||
READY: 1/1
|
||||
STATUS: Running
|
||||
@@ -60,11 +64,12 @@ Tablize can read one or more files or - if none specified - from STDIN.
|
||||
You can also specify a regex pattern to reduce the output:
|
||||
|
||||
```
|
||||
% kubectl get pods | ./tablizer q2bf4
|
||||
% kubectl get pods | tablizer q2bf4
|
||||
NAME(1) READY(2) STATUS(3) RESTARTS(4) AGE(5)
|
||||
repldepl-7bcd8d5b64-q2bf4 1/1 Running 1 (69m ago) 5h26m
|
||||
```
|
||||
|
||||
There are more output modes like org-mode (orgtbl) and markdown.
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -103,6 +108,10 @@ https://github.com/TLINDEN/tablizer/blob/main/tablizer.pod
|
||||
Or if you cloned the repository you can read it this way (perl needs
|
||||
to be installed though): `perldoc tablizer.pod`.
|
||||
|
||||
If you have the binary installed, you can also read the man page with
|
||||
this command:
|
||||
|
||||
tablizer --man
|
||||
|
||||
## Getting help
|
||||
|
||||
|
||||
5
TODO
5
TODO
@@ -1,6 +1 @@
|
||||
Add a mode like FreeBSD stat(1):
|
||||
|
||||
stat -s dead.letter
|
||||
st_dev=170671546954750497 st_ino=159667 st_mode=0100644 st_nlink=1 st_uid=1001 st_gid=1001 st_rdev=18446744073709551615 st_size=573 st_atime=1661994007 st_mtime=1661961878 st_ctime=1661961878 st_birthtime=1658394900 st_blksize=4096 st_blocks=3 st_flags=2048
|
||||
|
||||
Add reasoning about non-csv output of kubectl
|
||||
|
||||
169
cmd/parser.go
169
cmd/parser.go
@@ -1,169 +0,0 @@
|
||||
/*
|
||||
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 cmd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// contains a whole parsed table
|
||||
type Tabdata struct {
|
||||
maxwidthHeader int // longest header
|
||||
maxwidthPerCol []int // max width per column
|
||||
columns int
|
||||
headerIndices []map[string]int // [ {beg=>0, end=>17}, ... ]
|
||||
headers []string // [ "ID", "NAME", ...]
|
||||
entries [][]string
|
||||
}
|
||||
|
||||
func die(v ...interface{}) {
|
||||
fmt.Fprintln(os.Stderr, v...)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
/*
|
||||
Parse tabular input. We split the header (first line) by 2 or more
|
||||
spaces, remember the positions of the header fields. We then split
|
||||
the data (everything after the first line) by those positions. That
|
||||
way we can turn "tabular data" (with fields containing whitespaces)
|
||||
into real tabular data. We re-tabulate our input if you will.
|
||||
*/
|
||||
func parseFile(input io.Reader, pattern string) Tabdata {
|
||||
data := Tabdata{}
|
||||
|
||||
var scanner *bufio.Scanner
|
||||
var spaces = `\s\s+|$`
|
||||
|
||||
if len(Separator) > 0 {
|
||||
spaces = Separator
|
||||
}
|
||||
|
||||
hadFirst := false
|
||||
spacefinder := regexp.MustCompile(spaces)
|
||||
beg := 0
|
||||
|
||||
scanner = bufio.NewScanner(input)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
values := []string{}
|
||||
|
||||
patternR, err := regexp.Compile(pattern)
|
||||
if err != nil {
|
||||
die(err)
|
||||
}
|
||||
|
||||
if !hadFirst {
|
||||
// header processing
|
||||
parts := spacefinder.FindAllStringIndex(line, -1)
|
||||
data.columns = len(parts)
|
||||
// if Debug {
|
||||
// fmt.Println(parts)
|
||||
// }
|
||||
|
||||
// process all header fields
|
||||
for _, part := range parts {
|
||||
// if Debug {
|
||||
// fmt.Printf("Part: <%s>\n", string(line[beg:part[0]]))
|
||||
//}
|
||||
|
||||
// current field
|
||||
head := string(line[beg:part[0]])
|
||||
|
||||
// register begin and end of field within line
|
||||
indices := make(map[string]int)
|
||||
indices["beg"] = beg
|
||||
if part[0] == part[1] {
|
||||
indices["end"] = 0
|
||||
} else {
|
||||
indices["end"] = part[1] - 1
|
||||
}
|
||||
|
||||
// register widest header field
|
||||
headerlen := len(head)
|
||||
if headerlen > data.maxwidthHeader {
|
||||
data.maxwidthHeader = headerlen
|
||||
}
|
||||
|
||||
// register fields data
|
||||
data.headerIndices = append(data.headerIndices, indices)
|
||||
data.headers = append(data.headers, head)
|
||||
|
||||
// end of current field == begin of next one
|
||||
beg = part[1]
|
||||
|
||||
// done
|
||||
hadFirst = true
|
||||
}
|
||||
// if Debug {
|
||||
// fmt.Println(data.headerIndices)
|
||||
// }
|
||||
} else {
|
||||
// data processing
|
||||
if len(pattern) > 0 {
|
||||
//fmt.Println(patternR.MatchString(line))
|
||||
if !patternR.MatchString(line) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
idx := 0 // we cannot use the header index, because we could exclude columns
|
||||
|
||||
for _, index := range data.headerIndices {
|
||||
value := ""
|
||||
if index["end"] == 0 {
|
||||
value = string(line[index["beg"]:])
|
||||
} else {
|
||||
value = string(line[index["beg"]:index["end"]])
|
||||
}
|
||||
|
||||
width := len(strings.TrimSpace(value))
|
||||
|
||||
if len(data.maxwidthPerCol)-1 < idx {
|
||||
data.maxwidthPerCol = append(data.maxwidthPerCol, width)
|
||||
} else {
|
||||
if width > data.maxwidthPerCol[idx] {
|
||||
data.maxwidthPerCol[idx] = width
|
||||
}
|
||||
}
|
||||
|
||||
// if Debug {
|
||||
// fmt.Printf("<%s> ", value)
|
||||
// }
|
||||
values = append(values, value)
|
||||
|
||||
idx++
|
||||
}
|
||||
if Debug {
|
||||
fmt.Println()
|
||||
}
|
||||
data.entries = append(data.entries, values)
|
||||
}
|
||||
}
|
||||
|
||||
if scanner.Err() != nil {
|
||||
die(scanner.Err())
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
126
cmd/printer.go
126
cmd/printer.go
@@ -1,126 +0,0 @@
|
||||
/*
|
||||
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 cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func printTable(data Tabdata) {
|
||||
if XtendedOut {
|
||||
printExtended(data)
|
||||
return
|
||||
}
|
||||
|
||||
// needed for data output
|
||||
var formats []string
|
||||
|
||||
if len(data.entries) > 0 {
|
||||
// headers
|
||||
for i, head := range data.headers {
|
||||
if len(Columns) > 0 {
|
||||
if !contains(UseColumns, i+1) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// calculate column width
|
||||
var width int
|
||||
var iwidth int
|
||||
var format string
|
||||
|
||||
// generate format string
|
||||
if len(head) > data.maxwidthPerCol[i] {
|
||||
width = len(head)
|
||||
} else {
|
||||
width = data.maxwidthPerCol[i]
|
||||
}
|
||||
|
||||
if NoNumbering {
|
||||
iwidth = 0
|
||||
} else {
|
||||
iwidth = len(fmt.Sprintf("%d", i)) // in case i > 9
|
||||
}
|
||||
|
||||
format = fmt.Sprintf("%%-%ds", 3+iwidth+width)
|
||||
|
||||
if NoNumbering {
|
||||
fmt.Printf(format, fmt.Sprintf("%s ", head))
|
||||
} else {
|
||||
fmt.Printf(format, fmt.Sprintf("%s(%d) ", head, i+1))
|
||||
}
|
||||
|
||||
// register
|
||||
formats = append(formats, format)
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
// entries
|
||||
var idx int
|
||||
for _, entry := range data.entries {
|
||||
idx = 0
|
||||
//fmt.Println(entry)
|
||||
for i, value := range entry {
|
||||
if len(Columns) > 0 {
|
||||
if !contains(UseColumns, i+1) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
fmt.Printf(formats[idx], strings.TrimSpace(value))
|
||||
idx++
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
We simulate the \x command of psql (the PostgreSQL client)
|
||||
*/
|
||||
func printExtended(data Tabdata) {
|
||||
// needed for data output
|
||||
format := fmt.Sprintf("%%%ds: %%s\n", data.maxwidthHeader) // FIXME: re-calculate if -c has been set
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf(format, data.headers[idx], value)
|
||||
idx++
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func contains(s []int, e int) bool {
|
||||
for _, a := range s {
|
||||
if a == e {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
81
cmd/root.go
81
cmd/root.go
@@ -17,34 +17,62 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/tlinden/tablizer/lib"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
var version = "v1.0.1"
|
||||
var ShowManual = false
|
||||
|
||||
func man() {
|
||||
man := exec.Command("less", "-")
|
||||
|
||||
var b bytes.Buffer
|
||||
b.Write([]byte(manpage))
|
||||
|
||||
man.Stdout = os.Stdout
|
||||
man.Stdin = &b
|
||||
man.Stderr = os.Stderr
|
||||
|
||||
err := man.Run()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "tablizer [regex] [file, ...]",
|
||||
Short: "[Re-]tabularize tabular data",
|
||||
Long: `Manipulate tabular output of other programs`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if Version {
|
||||
fmt.Printf("This is tablizer version %s\n", version)
|
||||
if lib.ShowVersion {
|
||||
fmt.Printf("This is tablizer version %s\n", lib.VERSION)
|
||||
return nil
|
||||
}
|
||||
|
||||
return process(args)
|
||||
},
|
||||
if ShowManual {
|
||||
man()
|
||||
return nil
|
||||
}
|
||||
|
||||
var Debug bool
|
||||
var XtendedOut bool
|
||||
var NoNumbering bool
|
||||
var Version bool
|
||||
var Columns string
|
||||
var UseColumns []int
|
||||
var Separator string
|
||||
err := lib.PrepareColumns()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = lib.PrepareModeFlags()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return lib.ProcessFiles(args)
|
||||
},
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
err := rootCmd.Execute()
|
||||
@@ -54,10 +82,27 @@ func Execute() {
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.PersistentFlags().BoolVarP(&Debug, "debug", "d", false, "Enable debugging")
|
||||
rootCmd.PersistentFlags().BoolVarP(&XtendedOut, "extended", "x", false, "Enable extended output")
|
||||
rootCmd.PersistentFlags().BoolVarP(&NoNumbering, "no-numbering", "n", false, "Disable header numbering")
|
||||
rootCmd.PersistentFlags().BoolVarP(&Version, "version", "v", false, "Print program version")
|
||||
rootCmd.PersistentFlags().StringVarP(&Separator, "separator", "s", "", "Custom field separator")
|
||||
rootCmd.PersistentFlags().StringVarP(&Columns, "columns", "c", "", "Only show the speficied columns (separated by ,)")
|
||||
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 ,)")
|
||||
rootCmd.PersistentFlags().IntVarP(&lib.SortByColumn, "sort-by", "k", 0, "Sort by column (default: 1)")
|
||||
|
||||
// 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)")
|
||||
}
|
||||
|
||||
164
cmd/tablizer.go
Normal file
164
cmd/tablizer.go
Normal file
@@ -0,0 +1,164 @@
|
||||
package cmd
|
||||
var manpage = `
|
||||
NAME
|
||||
tablizer - Manipulate tabular output of other programs
|
||||
|
||||
SYNOPSIS
|
||||
Usage:
|
||||
tablizer [regex] [file, ...] [flags]
|
||||
|
||||
Flags:
|
||||
-c, --columns string Only show the speficied columns (separated by ,)
|
||||
-d, --debug Enable debugging
|
||||
-h, --help help for tablizer
|
||||
-v, --invert-match select non-matching rows
|
||||
-m, --man Display manual page
|
||||
-n, --no-numbering Disable header numbering
|
||||
-N, --no-color Disable pattern highlighting
|
||||
-o, --output string Output mode - one of: orgtbl, markdown, extended, ascii(default)
|
||||
-X, --extended Enable extended output
|
||||
-M, --markdown Enable markdown table output
|
||||
-O, --orgtbl Enable org-mode table output
|
||||
-s, --separator string Custom field separator
|
||||
-k, --sort-by int Sort by column (default: 1)
|
||||
-v, --version Print program version
|
||||
|
||||
DESCRIPTION
|
||||
Many programs generate tabular output. But sometimes you need to
|
||||
post-process these tables, you may need to remove one or more columns or
|
||||
you may want to filter for some pattern (See PATTERNS) or you may need
|
||||
the output in another program and need to parse it somehow. Standard
|
||||
unix tools such as awk(1), grep(1) or column(1) may help, but sometimes
|
||||
it's a tedious business.
|
||||
|
||||
Let's take the output of the tool kubectl. It contains cells with
|
||||
withespace and they do not separate columns by TAB characters. This is
|
||||
not easy to process.
|
||||
|
||||
You can use tablizer to do these and more things.
|
||||
|
||||
tablizer analyses the header fiels of a table, registers the column
|
||||
positions of each header field and separates columns by those positions.
|
||||
|
||||
Without any options it reads its input from "STDIN", but you can also
|
||||
specify a file as a parameter. If you want to reduce the output by some
|
||||
regular expression, just specify it as its first parameter. You may also
|
||||
use the -v option to exclude all rows which match the pattern. Hence:
|
||||
|
||||
# read from STDIN
|
||||
kubectl get pods | tablizer
|
||||
|
||||
# read a file
|
||||
tablizer filename
|
||||
|
||||
# search for pattern in a file (works like grep)
|
||||
tablizer regex filename
|
||||
|
||||
# search for pattern in STDIN
|
||||
kubectl get pods | tablizer regex
|
||||
|
||||
The output looks like the original one but every header field will have
|
||||
a numer associated with it, e.g.:
|
||||
|
||||
NAME(1) READY(2) STATUS(3) RESTARTS(4) AGE(5)
|
||||
|
||||
These numbers denote the column and you can use them to specify which
|
||||
columns you want to have in your output:
|
||||
|
||||
kubectl get pods | tablizer -c1,3
|
||||
|
||||
You can specify the numbers in any order but output will always follow
|
||||
the original order.
|
||||
|
||||
The numbering can be suppressed by using the -n option.
|
||||
|
||||
By default, if a pattern has been speficied, matches will be
|
||||
highlighted. You can disable this behavior with the -N option.
|
||||
|
||||
Use the -k option to specify by which column to sort the tabular data
|
||||
(as in GNU sort(1)). The default sort column is the first one. To
|
||||
disable sorting at all, supply 0 (Zero) to -k.
|
||||
|
||||
Finally the -d option enables debugging output which is mostly usefull
|
||||
for the developer.
|
||||
|
||||
PATTERNS
|
||||
You can reduce the rows being displayed by using a regular expression
|
||||
pattern. The regexp is PCRE compatible, refer to the syntax cheat sheet
|
||||
here: <https://github.com/google/re2/wiki/Syntax>. If you want to read a
|
||||
more comprehensive documentation about the topic and have perl installed
|
||||
you can read it with:
|
||||
|
||||
perldoc perlre
|
||||
|
||||
Or read it online: <https://perldoc.perl.org/perlre>.
|
||||
|
||||
A note on modifiers: the regexp engine used in tablizer uses another
|
||||
modifier syntax:
|
||||
|
||||
(?MODIFIER)
|
||||
|
||||
The most important modifiers are:
|
||||
|
||||
"i" ignore case "m" multiline mode "s" single line mode
|
||||
|
||||
Example for a case insensitve search:
|
||||
|
||||
kubectl get pods -A | tablizer "(?i)account"
|
||||
|
||||
OUTPUT MODES
|
||||
There might be cases when the tabular output of a program is way too
|
||||
large for your current terminal but you still need to see every column.
|
||||
In such cases the -o extended or -X option can be usefull which enables
|
||||
*extended mode*. In this mode, each row will be printed vertically,
|
||||
header left, value right, aligned by the field widths. Here's an
|
||||
example:
|
||||
|
||||
kubectl get pods | ./tablizer -o extended
|
||||
NAME: repldepl-7bcd8d5b64-7zq4l
|
||||
READY: 1/1
|
||||
STATUS: Running
|
||||
RESTARTS: 1 (71m ago)
|
||||
AGE: 5h28m
|
||||
|
||||
You can of course still use a regex to reduce the number of rows
|
||||
displayed.
|
||||
|
||||
The option -o shell can be used if the output has to be processed by the
|
||||
shell, it prints variable assignments for each cell, one line per row:
|
||||
|
||||
kubectl get pods | ./tablizer -o extended ./tablizer -o shell
|
||||
NAME="repldepl-7bcd8d5b64-7zq4l" READY="1/1" STATUS="Running" RESTARTS="9 (47m ago)" AGE="4d23h"
|
||||
NAME="repldepl-7bcd8d5b64-m48n8" READY="1/1" STATUS="Running" RESTARTS="9 (47m ago)" AGE="4d23h"
|
||||
NAME="repldepl-7bcd8d5b64-q2bf4" READY="1/1" STATUS="Running" RESTARTS="9 (47m ago)" AGE="4d23h"
|
||||
|
||||
You can use this in an eval loop.
|
||||
|
||||
Beside normal ascii mode (the default) and extended mode there are more
|
||||
output modes available: orgtbl which prints an Emacs org-mode table and
|
||||
markdown which prints a Markdown table.
|
||||
|
||||
BUGS
|
||||
In order to report a bug, unexpected behavior, feature requests or to
|
||||
submit a patch, please open an issue on github:
|
||||
<https://github.com/TLINDEN/tablizer/issues>.
|
||||
|
||||
LICENSE
|
||||
This software is licensed under the GNU GENERAL PUBLIC LICENSE version
|
||||
3.
|
||||
|
||||
Copyright (c) 2022 by Thomas von Dein
|
||||
|
||||
This software uses the following GO libraries:
|
||||
|
||||
repr (https://github.com/alecthomas/repr)
|
||||
Released under the MIT License, Copyright (c) 2016 Alec Thomas
|
||||
|
||||
cobra (https://github.com/spf13/cobra)
|
||||
Released under the Apache 2.0 license, Copyright 2013-2022 The Cobra
|
||||
Authors
|
||||
|
||||
AUTHORS
|
||||
Thomas von Dein tom AT vondein DOT org
|
||||
|
||||
`
|
||||
14
go.mod
14
go.mod
@@ -1,12 +1,18 @@
|
||||
module daemon.de/tablizer
|
||||
module github.com/tlinden/tablizer
|
||||
|
||||
go 1.18
|
||||
|
||||
require github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897
|
||||
require (
|
||||
github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897
|
||||
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/spf13/cobra v1.5.0 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.9 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/stretchr/testify v1.8.0 // indirect
|
||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 // indirect
|
||||
)
|
||||
|
||||
10
go.sum
10
go.sum
@@ -4,8 +4,14 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gookit/color v1.5.2 h1:uLnfXcaFjlrDnQDT+NCBcfhrXqYTx/rcCa6xn01Y8yI=
|
||||
github.com/gookit/color v1.5.2/go.mod h1:w8h4bGiHeeBpvQVePTutdbERIUf3oJE5lZ8HM0UgXyg=
|
||||
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
|
||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
|
||||
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
@@ -18,6 +24,10 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
|
||||
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/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
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=
|
||||
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=
|
||||
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.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
89
lib/common.go
Normal file
89
lib/common.go
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
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 (
|
||||
"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: 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)"
|
||||
|
||||
// main program version
|
||||
Version = "v1.0.8"
|
||||
|
||||
// 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
|
||||
)
|
||||
|
||||
// contains a whole parsed table
|
||||
type Tabdata struct {
|
||||
maxwidthHeader int // longest header
|
||||
maxwidthPerCol []int // max width per column
|
||||
columns int // count
|
||||
headers []string // [ "ID", "NAME", ...]
|
||||
entries [][]string
|
||||
}
|
||||
165
lib/helpers.go
Normal file
165
lib/helpers.go
Normal file
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
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 (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gookit/color"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func contains(s []int, e int) bool {
|
||||
for _, a := range s {
|
||||
if a == e {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func PrepareColumns() error {
|
||||
if len(Columns) > 0 {
|
||||
for _, use := range strings.Split(Columns, ",") {
|
||||
usenum, err := strconv.Atoi(use)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("Could not parse columns list %s: %v", Columns, err)
|
||||
return errors.New(msg)
|
||||
}
|
||||
UseColumns = append(UseColumns, usenum)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
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) {
|
||||
// ignore this one
|
||||
continue
|
||||
}
|
||||
}
|
||||
if NoNumbering {
|
||||
numberedHeaders = append(numberedHeaders, head)
|
||||
headlen = len(head)
|
||||
} else {
|
||||
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) {
|
||||
// exclude columns, if any
|
||||
if len(Columns) > 0 {
|
||||
reducedEntries := [][]string{}
|
||||
reducedEntry := []string{}
|
||||
for _, entry := range data.entries {
|
||||
reducedEntry = nil
|
||||
for i, value := range entry {
|
||||
if !contains(UseColumns, i+1) {
|
||||
continue
|
||||
}
|
||||
|
||||
reducedEntry = append(reducedEntry, value)
|
||||
}
|
||||
reducedEntries = append(reducedEntries, reducedEntry)
|
||||
}
|
||||
data.entries = reducedEntries
|
||||
}
|
||||
}
|
||||
|
||||
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 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
|
||||
}
|
||||
|
||||
func colorizeData(output string) string {
|
||||
if len(Pattern) > 0 && !NoColor && color.IsConsole(os.Stdout) {
|
||||
r := regexp.MustCompile("(" + Pattern + ")")
|
||||
return r.ReplaceAllString(output, "<bg="+MatchBG+";fg="+MatchFG+">$1</>")
|
||||
} else {
|
||||
return output
|
||||
}
|
||||
}
|
||||
|
||||
func isTerminal(f *os.File) bool {
|
||||
o, _ := f.Stat()
|
||||
if (o.Mode() & os.ModeCharDevice) == os.ModeCharDevice {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
117
lib/helpers_test.go
Normal file
117
lib/helpers_test.go
Normal file
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
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"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Testcontains(t *testing.T) {
|
||||
var tests = []struct {
|
||||
list []int
|
||||
search int
|
||||
want bool
|
||||
}{
|
||||
{[]int{1, 2, 3}, 2, true},
|
||||
{[]int{2, 3, 4}, 5, false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
testname := fmt.Sprintf("contains-%d,%d,%t", tt.list, tt.search, tt.want)
|
||||
t.Run(testname, func(t *testing.T) {
|
||||
answer := contains(tt.list, tt.search)
|
||||
if answer != tt.want {
|
||||
t.Errorf("got %t, want %t", answer, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareColumns(t *testing.T) {
|
||||
var tests = []struct {
|
||||
input string
|
||||
exp []int
|
||||
wanterror bool // expect error
|
||||
}{
|
||||
{"1,2,3", []int{1, 2, 3}, false},
|
||||
{"1,2,", []int{}, true},
|
||||
{"a,b", []int{}, true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
testname := fmt.Sprintf("PrepareColumns-%s-%t", tt.input, tt.wanterror)
|
||||
t.Run(testname, func(t *testing.T) {
|
||||
Columns = tt.input
|
||||
err := PrepareColumns()
|
||||
if err != nil {
|
||||
if !tt.wanterror {
|
||||
t.Errorf("got error: %v", err)
|
||||
}
|
||||
} else {
|
||||
if !reflect.DeepEqual(UseColumns, tt.exp) {
|
||||
t.Errorf("got: %v, expected: %v", UseColumns, tt.exp)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReduceColumns(t *testing.T) {
|
||||
var tests = []struct {
|
||||
expect [][]string
|
||||
columns []int
|
||||
}{
|
||||
{
|
||||
expect: [][]string{[]string{"a", "b"}},
|
||||
columns: []int{1, 2},
|
||||
},
|
||||
{
|
||||
expect: [][]string{[]string{"a", "c"}},
|
||||
columns: []int{1, 3},
|
||||
},
|
||||
{
|
||||
expect: [][]string{[]string{"a"}},
|
||||
columns: []int{1},
|
||||
},
|
||||
{
|
||||
expect: [][]string{nil},
|
||||
columns: []int{4},
|
||||
},
|
||||
}
|
||||
|
||||
input := [][]string{[]string{"a", "b", "c"}}
|
||||
|
||||
Columns = "y" // used as a flag with len(Columns)...
|
||||
|
||||
for _, tt := range tests {
|
||||
testname := fmt.Sprintf("reduce-columns-by-%+v", tt.columns)
|
||||
t.Run(testname, func(t *testing.T) {
|
||||
UseColumns = tt.columns
|
||||
data := Tabdata{entries: input}
|
||||
reduceColumns(&data)
|
||||
if !reflect.DeepEqual(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
|
||||
}
|
||||
@@ -15,48 +15,66 @@ 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 cmd
|
||||
package lib
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/alecthomas/repr"
|
||||
"github.com/gookit/color"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func process(args []string) error {
|
||||
var pattern string
|
||||
havefiles := false
|
||||
func ProcessFiles(args []string) error {
|
||||
fds, pattern, err := determineIO(args)
|
||||
|
||||
if !isTerminal(os.Stdout) {
|
||||
color.Disable()
|
||||
} else {
|
||||
level := color.TermColorLevel()
|
||||
MatchFG = Colors[level]["fg"]
|
||||
MatchBG = Colors[level]["bg"]
|
||||
}
|
||||
|
||||
if len(Columns) > 0 {
|
||||
for _, use := range strings.Split(Columns, ",") {
|
||||
usenum, err := strconv.Atoi(use)
|
||||
if err != nil {
|
||||
die(err)
|
||||
return err
|
||||
}
|
||||
UseColumns = append(UseColumns, usenum)
|
||||
|
||||
for _, fd := range fds {
|
||||
data, err := parseFile(fd, pattern)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printData(&data)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func determineIO(args []string) ([]io.Reader, string, error) {
|
||||
var pattern string
|
||||
var fds []io.Reader
|
||||
var havefiles bool
|
||||
|
||||
if len(args) > 0 {
|
||||
// threre were args left, take a look
|
||||
if _, err := os.Stat(args[0]); err != nil {
|
||||
// first one is not a file, consider it as regexp and
|
||||
// shift arg list
|
||||
pattern = args[0]
|
||||
Pattern = args[0] // FIXME
|
||||
args = args[1:]
|
||||
}
|
||||
|
||||
if len(args) > 0 {
|
||||
// only files
|
||||
for _, file := range args {
|
||||
fd, err := os.OpenFile(file, os.O_RDONLY, 0755)
|
||||
|
||||
if err != nil {
|
||||
die(err)
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
data := parseFile(fd, pattern)
|
||||
if Debug {
|
||||
repr.Print(data)
|
||||
}
|
||||
printTable(data)
|
||||
fds = append(fds, fd)
|
||||
}
|
||||
havefiles = true
|
||||
}
|
||||
@@ -65,15 +83,11 @@ func process(args []string) error {
|
||||
if !havefiles {
|
||||
stat, _ := os.Stdin.Stat()
|
||||
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
||||
data := parseFile(os.Stdin, pattern)
|
||||
if Debug {
|
||||
repr.Print(data)
|
||||
}
|
||||
printTable(data)
|
||||
fds = append(fds, os.Stdin)
|
||||
} else {
|
||||
return errors.New("No file specified and nothing to read on stdin!")
|
||||
return nil, "", errors.New("No file specified and nothing to read on stdin!")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return fds, pattern, nil
|
||||
}
|
||||
121
lib/parser.go
Normal file
121
lib/parser.go
Normal file
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
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 (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/alecthomas/repr"
|
||||
"io"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
/*
|
||||
Parse tabular input.
|
||||
*/
|
||||
func parseFile(input io.Reader, pattern string) (Tabdata, error) {
|
||||
data := Tabdata{}
|
||||
|
||||
var scanner *bufio.Scanner
|
||||
|
||||
hadFirst := false
|
||||
separate := regexp.MustCompile(Separator)
|
||||
patternR, err := regexp.Compile(pattern)
|
||||
if err != nil {
|
||||
return data, errors.Unwrap(fmt.Errorf("Regexp pattern %s is invalid: %w", pattern, err))
|
||||
}
|
||||
|
||||
scanner = bufio.NewScanner(input)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
parts := separate.Split(line, -1)
|
||||
|
||||
if !hadFirst {
|
||||
// header processing
|
||||
data.columns = len(parts)
|
||||
// if Debug {
|
||||
// fmt.Println(parts)
|
||||
// }
|
||||
|
||||
// process all header fields
|
||||
for _, part := range parts {
|
||||
// if Debug {
|
||||
// fmt.Printf("Part: <%s>\n", string(line[beg:part[0]]))
|
||||
//}
|
||||
|
||||
// register widest header field
|
||||
headerlen := len(part)
|
||||
if headerlen > data.maxwidthHeader {
|
||||
data.maxwidthHeader = headerlen
|
||||
}
|
||||
|
||||
// register fields data
|
||||
data.headers = append(data.headers, strings.TrimSpace(part))
|
||||
|
||||
// done
|
||||
hadFirst = true
|
||||
}
|
||||
} else {
|
||||
// data processing
|
||||
if len(pattern) > 0 {
|
||||
if patternR.MatchString(line) == InvertMatch {
|
||||
// by default -v is false, so if a line does NOT
|
||||
// match the pattern, we will ignore it. However,
|
||||
// if the user specified -v, the matching is inverted,
|
||||
// so we ignore all lines, which DO match.
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
idx := 0 // we cannot use the header index, because we could exclude columns
|
||||
values := []string{}
|
||||
for _, part := range parts {
|
||||
width := len(strings.TrimSpace(part))
|
||||
|
||||
if len(data.maxwidthPerCol)-1 < idx {
|
||||
data.maxwidthPerCol = append(data.maxwidthPerCol, width)
|
||||
} else {
|
||||
if width > data.maxwidthPerCol[idx] {
|
||||
data.maxwidthPerCol[idx] = width
|
||||
}
|
||||
}
|
||||
|
||||
// if Debug {
|
||||
// fmt.Printf("<%s> ", value)
|
||||
// }
|
||||
values = append(values, strings.TrimSpace(part))
|
||||
|
||||
idx++
|
||||
}
|
||||
data.entries = append(data.entries, values)
|
||||
}
|
||||
}
|
||||
|
||||
if scanner.Err() != nil {
|
||||
return data, errors.Unwrap(fmt.Errorf("Failed to read from io.Reader: %w", scanner.Err()))
|
||||
}
|
||||
|
||||
if Debug {
|
||||
repr.Print(data)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
112
lib/parser_test.go
Normal file
112
lib/parser_test.go
Normal file
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
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"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParser(t *testing.T) {
|
||||
data := Tabdata{
|
||||
maxwidthHeader: 5,
|
||||
maxwidthPerCol: []int{
|
||||
5, 5, 8,
|
||||
},
|
||||
columns: 3,
|
||||
headers: []string{
|
||||
"ONE", "TWO", "THREE",
|
||||
},
|
||||
entries: [][]string{
|
||||
[]string{
|
||||
"asd", "igig", "cxxxncnc",
|
||||
},
|
||||
[]string{
|
||||
"19191", "EDD 1", "X",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
table := `ONE TWO THREE
|
||||
asd igig cxxxncnc
|
||||
19191 EDD 1 X`
|
||||
|
||||
readFd := strings.NewReader(table)
|
||||
gotdata, err := parseFile(readFd, "")
|
||||
Separator = DefaultSeparator
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, gotdata)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(data, gotdata) {
|
||||
t.Errorf("Parser returned invalid data, Regex: %s\nExp: %+v\nGot: %+v\n", Separator, data, gotdata)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParserPatternmatching(t *testing.T) {
|
||||
var tests = []struct {
|
||||
entries [][]string
|
||||
pattern string
|
||||
invert bool
|
||||
}{
|
||||
{
|
||||
entries: [][]string{
|
||||
[]string{
|
||||
"asd", "igig", "cxxxncnc",
|
||||
},
|
||||
},
|
||||
pattern: "ig",
|
||||
invert: false,
|
||||
},
|
||||
{
|
||||
entries: [][]string{
|
||||
[]string{
|
||||
"19191", "EDD 1", "X",
|
||||
},
|
||||
},
|
||||
pattern: "ig",
|
||||
invert: true,
|
||||
},
|
||||
}
|
||||
|
||||
table := `ONE TWO THREE
|
||||
asd igig cxxxncnc
|
||||
19191 EDD 1 X`
|
||||
|
||||
for _, tt := range tests {
|
||||
testname := fmt.Sprintf("parse-with-inverted-pattern-%t", tt.invert)
|
||||
t.Run(testname, func(t *testing.T) {
|
||||
InvertMatch = tt.invert
|
||||
|
||||
readFd := strings.NewReader(table)
|
||||
gotdata, err := parseFile(readFd, tt.pattern)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, gotdata)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(tt.entries, gotdata.entries) {
|
||||
t.Errorf("Parser returned invalid data (pattern: %s, invert: %t)\nExp: %+v\nGot: %+v\n",
|
||||
tt.pattern, tt.invert, tt.entries, gotdata.entries)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
183
lib/printer.go
Normal file
183
lib/printer.go
Normal file
@@ -0,0 +1,183 @@
|
||||
/*
|
||||
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/gookit/color"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func printData(data *Tabdata) {
|
||||
// some output preparations:
|
||||
|
||||
if OutputMode != "shell" {
|
||||
// not needed in eval string
|
||||
numberizeHeaders(data)
|
||||
}
|
||||
|
||||
// remove unwanted columns, if any
|
||||
reduceColumns(data)
|
||||
|
||||
// sort the data
|
||||
sortTable(data, SortByColumn)
|
||||
|
||||
switch OutputMode {
|
||||
case "extended":
|
||||
printExtendedData(data)
|
||||
case "ascii":
|
||||
printAsciiData(data)
|
||||
case "orgtbl":
|
||||
printOrgmodeData(data)
|
||||
case "markdown":
|
||||
printMarkdownData(data)
|
||||
case "shell":
|
||||
printShellData(data)
|
||||
default:
|
||||
printAsciiData(data)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Emacs org-mode compatible table (also orgtbl-mode)
|
||||
*/
|
||||
func printOrgmodeData(data *Tabdata) {
|
||||
tableString := &strings.Builder{}
|
||||
table := tablewriter.NewWriter(tableString)
|
||||
|
||||
table.SetHeader(data.headers)
|
||||
|
||||
for _, row := range data.entries {
|
||||
table.Append(trimRow(row))
|
||||
}
|
||||
|
||||
table.Render()
|
||||
|
||||
/* fix output for org-mode (orgtbl)
|
||||
tableWriter output:
|
||||
+------+------+
|
||||
| cell | cell |
|
||||
+------+------+
|
||||
|
||||
Needed for org-mode compatibility:
|
||||
|------+------|
|
||||
| cell | cell |
|
||||
|------+------|
|
||||
*/
|
||||
leftR := regexp.MustCompile("(?m)^\\+")
|
||||
rightR := regexp.MustCompile("\\+(?m)$")
|
||||
|
||||
color.Print(
|
||||
colorizeData(
|
||||
rightR.ReplaceAllString(
|
||||
leftR.ReplaceAllString(tableString.String(), "|"), "|")))
|
||||
}
|
||||
|
||||
/*
|
||||
Markdown table
|
||||
*/
|
||||
func printMarkdownData(data *Tabdata) {
|
||||
tableString := &strings.Builder{}
|
||||
table := tablewriter.NewWriter(tableString)
|
||||
|
||||
table.SetHeader(data.headers)
|
||||
|
||||
for _, row := range data.entries {
|
||||
table.Append(trimRow(row))
|
||||
}
|
||||
|
||||
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
|
||||
table.SetCenterSeparator("|")
|
||||
|
||||
table.Render()
|
||||
color.Print(colorizeData(tableString.String()))
|
||||
}
|
||||
|
||||
/*
|
||||
Simple ASCII table without any borders etc, just like the input we expect
|
||||
*/
|
||||
func printAsciiData(data *Tabdata) {
|
||||
tableString := &strings.Builder{}
|
||||
table := tablewriter.NewWriter(tableString)
|
||||
|
||||
table.SetHeader(data.headers)
|
||||
table.AppendBulk(data.entries)
|
||||
|
||||
// for _, row := range data.entries {
|
||||
// table.Append(trimRow(row))
|
||||
// }
|
||||
|
||||
table.SetAutoWrapText(false)
|
||||
table.SetAutoFormatHeaders(true)
|
||||
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
|
||||
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
||||
table.SetCenterSeparator("")
|
||||
table.SetColumnSeparator("")
|
||||
table.SetRowSeparator("")
|
||||
table.SetHeaderLine(false)
|
||||
table.SetBorder(false)
|
||||
table.SetTablePadding("\t") // pad with tabs
|
||||
table.SetNoWhiteSpace(true)
|
||||
|
||||
table.Render()
|
||||
color.Print(colorizeData(tableString.String()))
|
||||
}
|
||||
|
||||
/*
|
||||
We simulate the \x command of psql (the PostgreSQL client)
|
||||
*/
|
||||
func printExtendedData(data *Tabdata) {
|
||||
// needed for data output
|
||||
format := fmt.Sprintf("%%%ds: %%s\n", data.maxwidthHeader)
|
||||
|
||||
if len(data.entries) > 0 {
|
||||
for _, entry := range data.entries {
|
||||
for i, value := range entry {
|
||||
color.Printf(format, data.headers[i], value)
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Shell output, ready to be eval'd. Just like FreeBSD stat(1)
|
||||
*/
|
||||
func printShellData(data *Tabdata) {
|
||||
if len(data.entries) > 0 {
|
||||
var idx int
|
||||
for _, entry := range data.entries {
|
||||
idx = 0
|
||||
shentries := []string{}
|
||||
for i, value := range entry {
|
||||
if len(Columns) > 0 {
|
||||
if !contains(UseColumns, i+1) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
shentries = append(shentries, fmt.Sprintf("%s=\"%s\"", data.headers[idx], value))
|
||||
idx++
|
||||
}
|
||||
fmt.Println(strings.Join(shentries, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
212
lib/printer_test.go
Normal file
212
lib/printer_test.go
Normal file
@@ -0,0 +1,212 @@
|
||||
/*
|
||||
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/gookit/color"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func stdout2pipe(t *testing.T) (*os.File, *os.File) {
|
||||
reader, writer, err := os.Pipe()
|
||||
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{
|
||||
[]string{
|
||||
"asd", "igig", "cxxxncnc",
|
||||
},
|
||||
[]string{
|
||||
"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
|
||||
data := startdata // we need to reset our mock data, since it's being modified in printData()
|
||||
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{
|
||||
[]string{
|
||||
"abc", "345", "b1",
|
||||
},
|
||||
[]string{
|
||||
"bcd", "234", "a2",
|
||||
},
|
||||
[]string{
|
||||
"cde", "123", "c3",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
var tests = []struct {
|
||||
data Tabdata
|
||||
sortby int
|
||||
expect string
|
||||
}{
|
||||
{
|
||||
data: startdata,
|
||||
sortby: 1,
|
||||
expect: `ONE(1) TWO(2) THREE(3)
|
||||
abc 345 b1
|
||||
bcd 234 a2
|
||||
cde 123 c3`,
|
||||
},
|
||||
|
||||
{
|
||||
data: startdata,
|
||||
sortby: 2,
|
||||
expect: `ONE(1) TWO(2) THREE(3)
|
||||
cde 123 c3
|
||||
bcd 234 a2
|
||||
abc 345 b1`,
|
||||
},
|
||||
|
||||
{
|
||||
data: startdata,
|
||||
sortby: 3,
|
||||
expect: `ONE(1) TWO(2) THREE(3)
|
||||
bcd 234 a2
|
||||
abc 345 b1
|
||||
cde 123 c3`,
|
||||
},
|
||||
}
|
||||
|
||||
NoColor = true
|
||||
OutputMode = "ascii"
|
||||
origStdout, reader := stdout2pipe(t)
|
||||
|
||||
for _, tt := range tests {
|
||||
testname := fmt.Sprintf("print-sorted-table-%d", tt.sortby)
|
||||
t.Run(testname, func(t *testing.T) {
|
||||
SortByColumn = tt.sortby
|
||||
|
||||
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
|
||||
}
|
||||
46
lib/sort.go
Normal file
46
lib/sort.go
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
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 (
|
||||
"sort"
|
||||
)
|
||||
|
||||
func sortTable(data *Tabdata, col int) {
|
||||
if col <= 0 {
|
||||
// no sorting wanted
|
||||
return
|
||||
}
|
||||
|
||||
col-- // ui starts counting by 1, but use 0 internally
|
||||
|
||||
// sanity checks
|
||||
if len(data.entries) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if col >= len(data.headers) {
|
||||
// fall back to default column
|
||||
col = 0
|
||||
}
|
||||
|
||||
// actual sorting
|
||||
sort.SliceStable(data.entries, func(i, j int) bool {
|
||||
return data.entries[i][col] < data.entries[j][col]
|
||||
})
|
||||
}
|
||||
2
main.go
2
main.go
@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package main
|
||||
|
||||
import (
|
||||
"daemon.de/tablizer/cmd"
|
||||
"github.com/tlinden/tablizer/cmd"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
7
mkrel.sh
7
mkrel.sh
@@ -27,6 +27,11 @@ windows/amd64"
|
||||
tool="$1"
|
||||
version="$2"
|
||||
|
||||
if test -z "$version"; then
|
||||
echo "Usage: $0 <tool name> <release version>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
rm -rf releases
|
||||
mkdir -p releases
|
||||
|
||||
@@ -38,7 +43,7 @@ for D in $DIST; do
|
||||
tardir="${tool}-${os}-${arch}-${version}"
|
||||
tarfile="releases/${tool}-${os}-${arch}-${version}.tar.gz"
|
||||
set -x
|
||||
GOOS=${os} GOARCH=${arch} go build -o ${binfile}
|
||||
GOOS=${os} GOARCH=${arch} go build -o ${binfile} -ldflags "-X 'github.com/tlinden/tablizer/lib.VERSION=${version}'"
|
||||
mkdir -p ${tardir}
|
||||
cp ${binfile} README.md LICENSE ${tardir}/
|
||||
echo 'tool = tablizer
|
||||
|
||||
321
tablizer.1
Normal file
321
tablizer.1
Normal file
@@ -0,0 +1,321 @@
|
||||
.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42)
|
||||
.\"
|
||||
.\" Standard preamble:
|
||||
.\" ========================================================================
|
||||
.de Sp \" Vertical space (when we can't use .PP)
|
||||
.if t .sp .5v
|
||||
.if n .sp
|
||||
..
|
||||
.de Vb \" Begin verbatim text
|
||||
.ft CW
|
||||
.nf
|
||||
.ne \\$1
|
||||
..
|
||||
.de Ve \" End verbatim text
|
||||
.ft R
|
||||
.fi
|
||||
..
|
||||
.\" Set up some character translations and predefined strings. \*(-- will
|
||||
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
|
||||
.\" double quote, and \*(R" will give a right double quote. \*(C+ will
|
||||
.\" give a nicer C++. Capital omega is used to do unbreakable dashes and
|
||||
.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
|
||||
.\" nothing in troff, for use with C<>.
|
||||
.tr \(*W-
|
||||
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
|
||||
.ie n \{\
|
||||
. ds -- \(*W-
|
||||
. ds PI pi
|
||||
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
|
||||
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
|
||||
. ds L" ""
|
||||
. ds R" ""
|
||||
. ds C` ""
|
||||
. ds C' ""
|
||||
'br\}
|
||||
.el\{\
|
||||
. ds -- \|\(em\|
|
||||
. ds PI \(*p
|
||||
. ds L" ``
|
||||
. ds R" ''
|
||||
. ds C`
|
||||
. ds C'
|
||||
'br\}
|
||||
.\"
|
||||
.\" Escape single quotes in literal strings from groff's Unicode transform.
|
||||
.ie \n(.g .ds Aq \(aq
|
||||
.el .ds Aq '
|
||||
.\"
|
||||
.\" If the F register is >0, we'll generate index entries on stderr for
|
||||
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
|
||||
.\" entries marked with X<> in POD. Of course, you'll have to process the
|
||||
.\" output yourself in some meaningful fashion.
|
||||
.\"
|
||||
.\" Avoid warning from groff about undefined register 'F'.
|
||||
.de IX
|
||||
..
|
||||
.nr rF 0
|
||||
.if \n(.g .if rF .nr rF 1
|
||||
.if (\n(rF:(\n(.g==0)) \{\
|
||||
. if \nF \{\
|
||||
. de IX
|
||||
. tm Index:\\$1\t\\n%\t"\\$2"
|
||||
..
|
||||
. if !\nF==2 \{\
|
||||
. nr % 0
|
||||
. nr F 2
|
||||
. \}
|
||||
. \}
|
||||
.\}
|
||||
.rr rF
|
||||
.\"
|
||||
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
|
||||
.\" Fear. Run. Save yourself. No user-serviceable parts.
|
||||
. \" fudge factors for nroff and troff
|
||||
.if n \{\
|
||||
. ds #H 0
|
||||
. ds #V .8m
|
||||
. ds #F .3m
|
||||
. ds #[ \f1
|
||||
. ds #] \fP
|
||||
.\}
|
||||
.if t \{\
|
||||
. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
|
||||
. ds #V .6m
|
||||
. ds #F 0
|
||||
. ds #[ \&
|
||||
. ds #] \&
|
||||
.\}
|
||||
. \" simple accents for nroff and troff
|
||||
.if n \{\
|
||||
. ds ' \&
|
||||
. ds ` \&
|
||||
. ds ^ \&
|
||||
. ds , \&
|
||||
. ds ~ ~
|
||||
. ds /
|
||||
.\}
|
||||
.if t \{\
|
||||
. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
|
||||
. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
|
||||
. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
|
||||
. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
|
||||
. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
|
||||
. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
|
||||
.\}
|
||||
. \" troff and (daisy-wheel) nroff accents
|
||||
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
|
||||
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
|
||||
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
|
||||
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
|
||||
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
|
||||
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
|
||||
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
|
||||
.ds ae a\h'-(\w'a'u*4/10)'e
|
||||
.ds Ae A\h'-(\w'A'u*4/10)'E
|
||||
. \" corrections for vroff
|
||||
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
|
||||
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
|
||||
. \" for low resolution devices (crt and lpr)
|
||||
.if \n(.H>23 .if \n(.V>19 \
|
||||
\{\
|
||||
. ds : e
|
||||
. ds 8 ss
|
||||
. ds o a
|
||||
. ds d- d\h'-1'\(ga
|
||||
. ds D- D\h'-1'\(hy
|
||||
. ds th \o'bp'
|
||||
. ds Th \o'LP'
|
||||
. ds ae ae
|
||||
. ds Ae AE
|
||||
.\}
|
||||
.rm #[ #] #H #V #F C
|
||||
.\" ========================================================================
|
||||
.\"
|
||||
.IX Title "TABLIZER 1"
|
||||
.TH TABLIZER 1 "2022-10-13" "1" "User Commands"
|
||||
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
|
||||
.\" way too many mistakes in technical documents.
|
||||
.if n .ad l
|
||||
.nh
|
||||
.SH "NAME"
|
||||
tablizer \- Manipulate tabular output of other programs
|
||||
.SH "SYNOPSIS"
|
||||
.IX Header "SYNOPSIS"
|
||||
.Vb 2
|
||||
\& Usage:
|
||||
\& tablizer [regex] [file, ...] [flags]
|
||||
\&
|
||||
\& Flags:
|
||||
\& \-c, \-\-columns string Only show the speficied columns (separated by ,)
|
||||
\& \-d, \-\-debug Enable debugging
|
||||
\& \-h, \-\-help help for tablizer
|
||||
\& \-v, \-\-invert\-match select non\-matching rows
|
||||
\& \-m, \-\-man Display manual page
|
||||
\& \-n, \-\-no\-numbering Disable header numbering
|
||||
\& \-N, \-\-no\-color Disable pattern highlighting
|
||||
\& \-o, \-\-output string Output mode \- one of: orgtbl, markdown, extended, ascii(default)
|
||||
\& \-X, \-\-extended Enable extended output
|
||||
\& \-M, \-\-markdown Enable markdown table output
|
||||
\& \-O, \-\-orgtbl Enable org\-mode table output
|
||||
\& \-s, \-\-separator string Custom field separator
|
||||
\& \-k, \-\-sort\-by int Sort by column (default: 1)
|
||||
\& \-v, \-\-version Print program version
|
||||
.Ve
|
||||
.SH "DESCRIPTION"
|
||||
.IX Header "DESCRIPTION"
|
||||
Many programs generate tabular output. But sometimes you need to
|
||||
post-process these tables, you may need to remove one or more columns
|
||||
or you may want to filter for some pattern (See \s-1PATTERNS\s0) or you
|
||||
may need the output in another program and need to parse it somehow.
|
||||
Standard unix tools such as \fBawk\fR\|(1), \fBgrep\fR\|(1) or \fBcolumn\fR\|(1) may help, but
|
||||
sometimes it's a tedious business.
|
||||
.PP
|
||||
Let's take the output of the tool kubectl. It contains cells with
|
||||
withespace and they do not separate columns by \s-1TAB\s0 characters. This is
|
||||
not easy to process.
|
||||
.PP
|
||||
You can use \fBtablizer\fR to do these and more things.
|
||||
.PP
|
||||
\&\fBtablizer\fR analyses the header fiels of a table, registers the column
|
||||
positions of each header field and separates columns by those
|
||||
positions.
|
||||
.PP
|
||||
Without any options it reads its input from \f(CW\*(C`STDIN\*(C'\fR, but you can also
|
||||
specify a file as a parameter. If you want to reduce the output by
|
||||
some regular expression, just specify it as its first parameter. You
|
||||
may also use the \fB\-v\fR option to exclude all rows which match the
|
||||
pattern. Hence:
|
||||
.PP
|
||||
.Vb 2
|
||||
\& # read from STDIN
|
||||
\& kubectl get pods | tablizer
|
||||
\&
|
||||
\& # read a file
|
||||
\& tablizer filename
|
||||
\&
|
||||
\& # search for pattern in a file (works like grep)
|
||||
\& tablizer regex filename
|
||||
\&
|
||||
\& # search for pattern in STDIN
|
||||
\& kubectl get pods | tablizer regex
|
||||
.Ve
|
||||
.PP
|
||||
The output looks like the original one but every header field will
|
||||
have a numer associated with it, e.g.:
|
||||
.PP
|
||||
.Vb 1
|
||||
\& NAME(1) READY(2) STATUS(3) RESTARTS(4) AGE(5)
|
||||
.Ve
|
||||
.PP
|
||||
These numbers denote the column and you can use them to specify which
|
||||
columns you want to have in your output:
|
||||
.PP
|
||||
.Vb 1
|
||||
\& kubectl get pods | tablizer \-c1,3
|
||||
.Ve
|
||||
.PP
|
||||
You can specify the numbers in any order but output will always follow
|
||||
the original order.
|
||||
.PP
|
||||
The numbering can be suppressed by using the \fB\-n\fR option.
|
||||
.PP
|
||||
By default, if a \fBpattern\fR has been speficied, matches will be
|
||||
highlighted. You can disable this behavior with the \fB\-N\fR option.
|
||||
.PP
|
||||
Use the \fB\-k\fR option to specify by which column to sort the tabular
|
||||
data (as in \s-1GNU\s0 \fBsort\fR\|(1)). The default sort column is the first one. To
|
||||
disable sorting at all, supply 0 (Zero) to \-k.
|
||||
.PP
|
||||
Finally the \fB\-d\fR option enables debugging output which is mostly
|
||||
usefull for the developer.
|
||||
.SS "\s-1PATTERNS\s0"
|
||||
.IX Subsection "PATTERNS"
|
||||
You can reduce the rows being displayed by using a regular expression
|
||||
pattern. The regexp is \s-1PCRE\s0 compatible, refer to the syntax cheat
|
||||
sheet here: <https://github.com/google/re2/wiki/Syntax>. If you want
|
||||
to read a more comprehensive documentation about the topic and have
|
||||
perl installed you can read it with:
|
||||
.PP
|
||||
.Vb 1
|
||||
\& perldoc perlre
|
||||
.Ve
|
||||
.PP
|
||||
Or read it online: <https://perldoc.perl.org/perlre>.
|
||||
.PP
|
||||
A note on modifiers: the regexp engine used in tablizer uses another
|
||||
modifier syntax:
|
||||
.PP
|
||||
.Vb 1
|
||||
\& (?MODIFIER)
|
||||
.Ve
|
||||
.PP
|
||||
The most important modifiers are:
|
||||
.PP
|
||||
\&\f(CW\*(C`i\*(C'\fR ignore case
|
||||
\&\f(CW\*(C`m\*(C'\fR multiline mode
|
||||
\&\f(CW\*(C`s\*(C'\fR single line mode
|
||||
.PP
|
||||
Example for a case insensitve search:
|
||||
.PP
|
||||
.Vb 1
|
||||
\& kubectl get pods \-A | tablizer "(?i)account"
|
||||
.Ve
|
||||
.SS "\s-1OUTPUT MODES\s0"
|
||||
.IX Subsection "OUTPUT MODES"
|
||||
There might be cases when the tabular output of a program is way too
|
||||
large for your current terminal but you still need to see every
|
||||
column. In such cases the \fB\-o extended\fR or \fB\-X\fR option can be
|
||||
usefull which enables \fIextended mode\fR. In this mode, each row will be
|
||||
printed vertically, header left, value right, aligned by the field
|
||||
widths. Here's an example:
|
||||
.PP
|
||||
.Vb 6
|
||||
\& kubectl get pods | ./tablizer \-o extended
|
||||
\& NAME: repldepl\-7bcd8d5b64\-7zq4l
|
||||
\& READY: 1/1
|
||||
\& STATUS: Running
|
||||
\& RESTARTS: 1 (71m ago)
|
||||
\& AGE: 5h28m
|
||||
.Ve
|
||||
.PP
|
||||
You can of course still use a regex to reduce the number of rows
|
||||
displayed.
|
||||
.PP
|
||||
The option \fB\-o shell\fR can be used if the output has to be processed
|
||||
by the shell, it prints variable assignments for each cell, one line
|
||||
per row:
|
||||
.PP
|
||||
.Vb 4
|
||||
\& kubectl get pods | ./tablizer \-o extended ./tablizer \-o shell
|
||||
\& NAME="repldepl\-7bcd8d5b64\-7zq4l" READY="1/1" STATUS="Running" RESTARTS="9 (47m ago)" AGE="4d23h"
|
||||
\& NAME="repldepl\-7bcd8d5b64\-m48n8" READY="1/1" STATUS="Running" RESTARTS="9 (47m ago)" AGE="4d23h"
|
||||
\& NAME="repldepl\-7bcd8d5b64\-q2bf4" READY="1/1" STATUS="Running" RESTARTS="9 (47m ago)" AGE="4d23h"
|
||||
.Ve
|
||||
.PP
|
||||
You can use this in an eval loop.
|
||||
.PP
|
||||
Beside normal ascii mode (the default) and extended mode there are
|
||||
more output modes available: \fBorgtbl\fR which prints an Emacs org-mode
|
||||
table and \fBmarkdown\fR which prints a Markdown table.
|
||||
.SH "BUGS"
|
||||
.IX Header "BUGS"
|
||||
In order to report a bug, unexpected behavior, feature requests
|
||||
or to submit a patch, please open an issue on github:
|
||||
<https://github.com/TLINDEN/tablizer/issues>.
|
||||
.SH "LICENSE"
|
||||
.IX Header "LICENSE"
|
||||
This software is licensed under the \s-1GNU GENERAL PUBLIC LICENSE\s0 version 3.
|
||||
.PP
|
||||
Copyright (c) 2022 by Thomas von Dein
|
||||
.PP
|
||||
This software uses the following \s-1GO\s0 libraries:
|
||||
.IP "repr (https://github.com/alecthomas/repr)" 4
|
||||
.IX Item "repr (https://github.com/alecthomas/repr)"
|
||||
Released under the \s-1MIT\s0 License, Copyright (c) 2016 Alec Thomas
|
||||
.IP "cobra (https://github.com/spf13/cobra)" 4
|
||||
.IX Item "cobra (https://github.com/spf13/cobra)"
|
||||
Released under the Apache 2.0 license, Copyright 2013\-2022 The Cobra Authors
|
||||
.SH "AUTHORS"
|
||||
.IX Header "AUTHORS"
|
||||
Thomas von Dein \fBtom \s-1AT\s0 vondein \s-1DOT\s0 org\fR
|
||||
88
tablizer.pod
88
tablizer.pod
@@ -10,10 +10,17 @@ tablizer - Manipulate tabular output of other programs
|
||||
Flags:
|
||||
-c, --columns string Only show the speficied columns (separated by ,)
|
||||
-d, --debug Enable debugging
|
||||
-x, --extended Enable extended output
|
||||
-h, --help help for tablizer
|
||||
-v, --invert-match select non-matching rows
|
||||
-m, --man Display manual page
|
||||
-n, --no-numbering Disable header numbering
|
||||
-N, --no-color Disable pattern highlighting
|
||||
-o, --output string Output mode - one of: orgtbl, markdown, extended, ascii(default)
|
||||
-X, --extended Enable extended output
|
||||
-M, --markdown Enable markdown table output
|
||||
-O, --orgtbl Enable org-mode table output
|
||||
-s, --separator string Custom field separator
|
||||
-k, --sort-by int Sort by column (default: 1)
|
||||
-v, --version Print program version
|
||||
|
||||
|
||||
@@ -21,10 +28,10 @@ tablizer - Manipulate tabular output of other programs
|
||||
|
||||
Many programs generate tabular output. But sometimes you need to
|
||||
post-process these tables, you may need to remove one or more columns
|
||||
or you may want to filter for some pattern or you may need the output
|
||||
in another program and need to parse it somehow. Standard unix tools
|
||||
such as awk(1), grep(1) or column(1) may help, but sometimes it's a
|
||||
tedious business.
|
||||
or you may want to filter for some pattern (See L<PATTERNS>) or you
|
||||
may need the output in another program and need to parse it somehow.
|
||||
Standard unix tools such as awk(1), grep(1) or column(1) may help, but
|
||||
sometimes it's a tedious business.
|
||||
|
||||
Let's take the output of the tool kubectl. It contains cells with
|
||||
withespace and they do not separate columns by TAB characters. This is
|
||||
@@ -38,8 +45,9 @@ positions.
|
||||
|
||||
Without any options it reads its input from C<STDIN>, but you can also
|
||||
specify a file as a parameter. If you want to reduce the output by
|
||||
some regular expression, just specify it as its first
|
||||
parameters. Hence:
|
||||
some regular expression, just specify it as its first parameter. You
|
||||
may also use the B<-v> option to exclude all rows which match the
|
||||
pattern. Hence:
|
||||
|
||||
# read from STDIN
|
||||
kubectl get pods | tablizer
|
||||
@@ -68,14 +76,54 @@ the original order.
|
||||
|
||||
The numbering can be suppressed by using the B<-n> option.
|
||||
|
||||
By default, if a B<pattern> has been speficied, matches will be
|
||||
highlighted. You can disable this behavior with the B<-N> option.
|
||||
|
||||
Use the B<-k> option to specify by which column to sort the tabular
|
||||
data (as in GNU sort(1)). The default sort column is the first one. To
|
||||
disable sorting at all, supply 0 (Zero) to -k.
|
||||
|
||||
Finally the B<-d> option enables debugging output which is mostly
|
||||
usefull for the developer.
|
||||
|
||||
=head2 PATTERNS
|
||||
|
||||
You can reduce the rows being displayed by using a regular expression
|
||||
pattern. The regexp is PCRE compatible, refer to the syntax cheat
|
||||
sheet here: L<https://github.com/google/re2/wiki/Syntax>. If you want
|
||||
to read a more comprehensive documentation about the topic and have
|
||||
perl installed you can read it with:
|
||||
|
||||
perldoc perlre
|
||||
|
||||
Or read it online: L<https://perldoc.perl.org/perlre>.
|
||||
|
||||
A note on modifiers: the regexp engine used in tablizer uses another
|
||||
modifier syntax:
|
||||
|
||||
(?MODIFIER)
|
||||
|
||||
The most important modifiers are:
|
||||
|
||||
C<i> ignore case
|
||||
C<m> multiline mode
|
||||
C<s> single line mode
|
||||
|
||||
Example for a case insensitve search:
|
||||
|
||||
kubectl get pods -A | tablizer "(?i)account"
|
||||
|
||||
|
||||
=head2 OUTPUT MODES
|
||||
|
||||
There might be cases when the tabular output of a program is way too
|
||||
large for your current terminal but you still need to see every
|
||||
column. In such cases the B<-x> option can be usefull which enables
|
||||
I<extended mode>. In this mode, each row will be printed vertically,
|
||||
header left, value right, aligned by the field widths. Here's an
|
||||
example:
|
||||
column. In such cases the B<-o extended> or B<-X> option can be
|
||||
usefull which enables I<extended mode>. In this mode, each row will be
|
||||
printed vertically, header left, value right, aligned by the field
|
||||
widths. Here's an example:
|
||||
|
||||
kubectl get pods | ./tablizer -x
|
||||
kubectl get pods | ./tablizer -o extended
|
||||
NAME: repldepl-7bcd8d5b64-7zq4l
|
||||
READY: 1/1
|
||||
STATUS: Running
|
||||
@@ -85,8 +133,20 @@ example:
|
||||
You can of course still use a regex to reduce the number of rows
|
||||
displayed.
|
||||
|
||||
Finally the B<-d> option enables debugging output which is mostly
|
||||
usefull for the developer.
|
||||
The option B<-o shell> can be used if the output has to be processed
|
||||
by the shell, it prints variable assignments for each cell, one line
|
||||
per row:
|
||||
|
||||
kubectl get pods | ./tablizer -o extended ./tablizer -o shell
|
||||
NAME="repldepl-7bcd8d5b64-7zq4l" READY="1/1" STATUS="Running" RESTARTS="9 (47m ago)" AGE="4d23h"
|
||||
NAME="repldepl-7bcd8d5b64-m48n8" READY="1/1" STATUS="Running" RESTARTS="9 (47m ago)" AGE="4d23h"
|
||||
NAME="repldepl-7bcd8d5b64-q2bf4" READY="1/1" STATUS="Running" RESTARTS="9 (47m ago)" AGE="4d23h"
|
||||
|
||||
You can use this in an eval loop.
|
||||
|
||||
Beside normal ascii mode (the default) and extended mode there are
|
||||
more output modes available: B<orgtbl> which prints an Emacs org-mode
|
||||
table and B<markdown> which prints a Markdown table.
|
||||
|
||||
=head1 BUGS
|
||||
|
||||
|
||||
Reference in New Issue
Block a user