Compare commits

..

9 Commits

17 changed files with 528 additions and 276 deletions

View File

@@ -4,6 +4,36 @@ 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).
## [v1.0.12](https://github.com/TLINDEN/tablizer/tree/v1.0.12) - 2022-10-25
[Full Changelog](https://github.com/TLINDEN/tablizer/compare/v1.0.11...v1.0.12)
### Added
- Added support to parse CSV input
- Added CSV output support
- Added support for environment variables
### Changed
- We do not use the generated help message anymore, instead we use the
usage from the manpage, which we have to maintain anyway. It looks
better and has flag groups, which cobra is still lacking as of this
writing.
- More refactoring and re-organization, runtime configuration now
lives in the cfg module.
### Fixed
- Fixed [Bug #5](https://github.com/TLINDEN/tablizer/issues/5), where
matches have not been highlighted correctly in some rare cases.
## [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)

View File

@@ -41,6 +41,10 @@ cmd/%.go: %.pod
pod2text $*.pod >> cmd/$*.go
echo "\`" >> cmd/$*.go
echo "var usage = \`" >> cmd/$*.go
awk '/SYNOPS/{f=1;next} /DESCR/{f=0} f' $*.pod | sed 's/^ //' >> cmd/$*.go
echo "\`" >> cmd/$*.go
buildlocal:
go build -ldflags "-X 'github.com/tlinden/tablizer/cfg.VERSION=$(VERSION)'"

View File

@@ -1,15 +1,9 @@
## 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
- add output mode csv
- add comment support (csf.NewReader().Comment = '#')
- add --no-headers option
- add input parsing support for CSV including unquoting of stuff
like: `"xxx","1919 b"` etc, maybe an extra option for unquoting

View File

@@ -20,12 +20,12 @@ import (
"errors"
"fmt"
"github.com/gookit/color"
"os"
"regexp"
)
const DefaultSeparator string = `(\s\s+|\t)`
const ValidOutputModes string = "(orgtbl|markdown|extended|ascii|yaml|shell)"
const Version string = "v1.0.11"
const Version string = "v1.0.12"
var VERSION string // maintained by -x
@@ -35,9 +35,10 @@ type Config struct {
Columns string
UseColumns []int
Separator string
OutputMode string
OutputMode int
InvertMatch bool
Pattern string
PatternR *regexp.Regexp
SortMode string
SortDescending bool
@@ -45,12 +46,10 @@ type Config struct {
/*
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.
see https://github.com/gookit/color.
*/
MatchFG string
MatchBG string
ColorStyle color.Style
NoColor bool
}
@@ -62,8 +61,20 @@ type Modeflag struct {
S bool
Y bool
A bool
C bool
}
// used for switching printers
const (
Extended = iota + 1
Orgtbl
Markdown
Shell
Yaml
CSV
Ascii
)
// various sort types
type Sortmode struct {
Numeric bool
@@ -71,22 +82,43 @@ type Sortmode struct {
Age bool
}
func Colors() map[color.Level]map[string]string {
// default color schemes
return map[color.Level]map[string]string{
// default color schemes
func Colors() map[color.Level]map[string]color.Color {
return map[color.Level]map[string]color.Color{
color.Level16: {
"bg": "green", "fg": "black",
"bg": color.BgGreen, "fg": color.FgBlack,
},
color.Level256: {
"bg": "lightGreen", "fg": "black",
"bg": color.BgLightGreen, "fg": color.FgBlack,
},
color.LevelRgb: {
// FIXME: maybe use something nicer
"bg": "lightGreen", "fg": "black",
"bg": color.BgLightGreen, "fg": color.FgBlack,
},
}
}
// find supported color mode, modifies config based on constants
func (c *Config) DetermineColormode() {
if !isTerminal(os.Stdout) {
color.Disable()
} else {
level := color.TermColorLevel()
colors := Colors()
c.ColorStyle = color.New(colors[level]["bg"], colors[level]["fg"])
}
}
// Return true if current terminal is interactive
func isTerminal(f *os.File) bool {
o, _ := f.Stat()
if (o.Mode() & os.ModeCharDevice) == os.ModeCharDevice {
return true
} else {
return false
}
}
func Getversion() string {
// main program version
@@ -97,39 +129,6 @@ func Getversion() string {
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:
@@ -142,3 +141,60 @@ func (conf *Config) PrepareSortFlags(flag Sortmode) {
conf.SortMode = "string"
}
}
func (conf *Config) PrepareModeFlags(flag Modeflag) {
switch {
case flag.X:
conf.OutputMode = Extended
case flag.O:
conf.OutputMode = Orgtbl
case flag.M:
conf.OutputMode = Markdown
case flag.S:
conf.OutputMode = Shell
case flag.Y:
conf.OutputMode = Yaml
case flag.C:
conf.OutputMode = CSV
default:
conf.OutputMode = Ascii
}
}
func (c *Config) CheckEnv() {
// check for environment vars, command line flags have precedence,
// NO_COLOR is being checked by the color module itself.
if !c.NoNumbering {
_, set := os.LookupEnv("T_NO_HEADER_NUMBERING")
if set {
c.NoNumbering = true
}
}
if len(c.Columns) == 0 {
cols := os.Getenv("T_COLUMNS")
if len(cols) > 1 {
c.Columns = cols
}
}
}
func (c *Config) ApplyDefaults() {
// mode specific defaults
if c.OutputMode == Yaml || c.OutputMode == CSV {
c.NoNumbering = true
}
}
func (c *Config) PreparePattern(pattern string) error {
PatternR, err := regexp.Compile(pattern)
if err != nil {
return errors.Unwrap(fmt.Errorf("Regexp pattern %s is invalid: %w", c.Pattern, err))
}
c.PatternR = PatternR
c.Pattern = pattern
return nil
}

View File

@@ -26,46 +26,26 @@ import (
func TestPrepareModeFlags(t *testing.T) {
var tests = []struct {
flag Modeflag
mode string // input, if any
expect string // output
want bool
expect int // output (constant enum)
}{
// 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},
{Modeflag{X: true}, Extended},
{Modeflag{S: true}, Shell},
{Modeflag{O: true}, Orgtbl},
{Modeflag{Y: true}, Yaml},
{Modeflag{M: true}, Markdown},
{Modeflag{}, Ascii},
}
// FIXME: use a map for easier printing
for _, tt := range tests {
testname := fmt.Sprintf("PrepareModeFlags-flags-mode-%s-expect-%s-want-%t",
tt.mode, tt.expect, tt.want)
testname := fmt.Sprintf("PrepareModeFlags-expect-%d", tt.expect)
t.Run(testname, func(t *testing.T) {
c := Config{OutputMode: tt.mode}
c := Config{}
// 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)
}
c.PrepareModeFlags(tt.flag)
if c.OutputMode != tt.expect {
t.Errorf("got: %d, expect: %d", c.OutputMode, tt.expect)
}
})
}
@@ -96,3 +76,28 @@ func TestPrepareSortFlags(t *testing.T) {
})
}
}
func TestPreparePattern(t *testing.T) {
var tests = []struct {
pattern string
wanterr bool
}{
{"[A-Z]+", false},
{"[a-z", true},
}
for _, tt := range tests {
testname := fmt.Sprintf("PreparePattern-pattern-%s-wanterr-%t", tt.pattern, tt.wanterr)
t.Run(testname, func(t *testing.T) {
c := Config{}
err := c.PreparePattern(tt.pattern)
if err != nil {
if !tt.wanterr {
t.Errorf("PreparePattern returned error: %s", err)
}
}
})
}
}

View File

@@ -25,6 +25,7 @@ import (
"log"
"os"
"os/exec"
"strings"
)
func man() {
@@ -48,7 +49,6 @@ func Execute() {
var (
conf cfg.Config
ShowManual bool
Outputmode string
ShowVersion bool
modeflag cfg.Modeflag
sortmode cfg.Sortmode
@@ -69,16 +69,15 @@ func Execute() {
return nil
}
// prepare flags
err := conf.PrepareModeFlags(modeflag, Outputmode)
if err != nil {
return err
}
// Setup
conf.CheckEnv()
conf.PrepareModeFlags(modeflag)
conf.PrepareSortFlags(sortmode)
conf.DetermineColormode()
conf.ApplyDefaults()
// actual execution starts here
return lib.ProcessFiles(conf, args)
return lib.ProcessFiles(&conf, args)
},
}
@@ -94,26 +93,24 @@ func Execute() {
// sort options
rootCmd.PersistentFlags().IntVarP(&conf.SortByColumn, "sort-by", "k", 0, "Sort by column (default: 1)")
// sort mode, only 1 allowed
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")
rootCmd.MarkFlagsMutuallyExclusive("sort-desc", "sort-numeric", "sort-time", "sort-age")
// output flags, only 1 allowed, hidden, since just short cuts
// output flags, only 1 allowed
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")
rootCmd.PersistentFlags().BoolVarP(&modeflag.C, "csv", "C", false, "Enable CSV output")
rootCmd.MarkFlagsMutuallyExclusive("extended", "markdown", "orgtbl", "shell", "yaml", "csv")
// 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)")
rootCmd.SetUsageTemplate(strings.TrimSpace(usage) + "\n")
err := rootCmd.Execute()
if err != nil {

View File

@@ -8,24 +8,33 @@ SYNOPSIS
Usage:
tablizer [regex] [file, ...] [flags]
Flags:
Operational 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, yaml, ascii(default)
-s, --separator string Custom field separator
-k, --sort-by int Sort by column (default: 1)
Output Flags (mutually exclusive):
-X, --extended Enable extended output
-M, --markdown Enable markdown table output
-O, --orgtbl Enable org-mode table output
-s, --separator string Custom field separator
-S, --shell Enable shell evaluable ouput
-Y, --yaml Enable yaml output
-C, --csv Enable CSV output
-A, --ascii Default output mode, ascii tabular
Sort Mode Flags (mutually exclusive):
-a, --sort-age sort according to age (duration) string
-k, --sort-by int Sort by column (default: 1)
-D, --sort-desc Sort in descending order (default: ascending)
-i, --sort-numeric sort according to string numerical value
-t, --sort-time sort according to time string
Other Flags:
-d, --debug Enable debugging
-h, --help help for tablizer
-m, --man Display manual page
-v, --version Print program version
DESCRIPTION
@@ -178,8 +187,17 @@ DESCRIPTION
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 and yaml, which prints yaml
encoding.
markdown which prints a Markdown table, yaml, which prints yaml encoding
and CSV mode, which prints a comma separated value file.
ENVIRONMENT VARIABLES
tablizer supports certain environment variables which use can use to
influence program behavior. Commandline flags have always precedence
over environment variables.
<T_NO_HEADER_NUMBERING> - disable numbering of header fields, like -n.
<T_COLUMNS> - comma separated list of columns to output, like -c
<NO_COLORS> - disable colorization of matches, like -N
BUGS
In order to report a bug, unexpected behavior, feature requests or to
@@ -205,3 +223,39 @@ AUTHORS
Thomas von Dein tom AT vondein DOT org
`
var usage = `
Usage:
tablizer [regex] [file, ...] [flags]
Operational Flags:
-c, --columns string Only show the speficied columns (separated by ,)
-v, --invert-match select non-matching rows
-n, --no-numbering Disable header numbering
-N, --no-color Disable pattern highlighting
-s, --separator string Custom field separator
-k, --sort-by int Sort by column (default: 1)
Output Flags (mutually exclusive):
-X, --extended Enable extended output
-M, --markdown Enable markdown table output
-O, --orgtbl Enable org-mode table output
-S, --shell Enable shell evaluable ouput
-Y, --yaml Enable yaml output
-C, --csv Enable CSV output
-A, --ascii Default output mode, ascii tabular
Sort Mode Flags (mutually exclusive):
-a, --sort-age sort according to age (duration) string
-D, --sort-desc Sort in descending order (default: ascending)
-i, --sort-numeric sort according to string numerical value
-t, --sort-time sort according to time string
Other Flags:
-d, --debug Enable debugging
-h, --help help for tablizer
-m, --man Display manual page
-v, --version Print program version
`

View File

@@ -20,7 +20,6 @@ package lib
// 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

View File

@@ -155,17 +155,10 @@ func trimRow(row []string) []string {
func colorizeData(c cfg.Config, output string) string {
if len(c.Pattern) > 0 && !c.NoColor && color.IsConsole(os.Stdout) {
r := regexp.MustCompile("(" + c.Pattern + ")")
return r.ReplaceAllString(output, "<bg="+c.MatchBG+";fg="+c.MatchFG+">$1</>")
return r.ReplaceAllStringFunc(output, func(in string) string {
return c.ColorStyle.Sprint(in)
})
} else {
return output
}
}
func isTerminal(f *os.File) bool {
o, _ := f.Stat()
if (o.Mode() & os.ModeCharDevice) == os.ModeCharDevice {
return true
} else {
return false
}
}

View File

@@ -48,12 +48,7 @@ func TestContains(t *testing.T) {
func TestPrepareColumns(t *testing.T) {
data := Tabdata{
maxwidthHeader: 5,
maxwidthPerCol: []int{
5,
5,
8,
},
columns: 3,
columns: 3,
headers: []string{
"ONE", "TWO", "THREE",
},

View File

@@ -19,50 +19,39 @@ package lib
import (
"errors"
"github.com/gookit/color"
"github.com/tlinden/tablizer/cfg"
"io"
"os"
)
func ProcessFiles(c cfg.Config, args []string) error {
fds, pattern, err := determineIO(&c, args)
func ProcessFiles(c *cfg.Config, args []string) error {
fds, pattern, err := determineIO(c, args)
if err != nil {
return err
}
determineColormode(&c)
if err := c.PreparePattern(pattern); err != nil {
return err
}
for _, fd := range fds {
data, err := parseFile(c, fd, pattern)
data, err := Parse(*c, fd)
if err != nil {
return err
}
err = PrepareColumns(&c, &data)
err = PrepareColumns(c, &data)
if err != nil {
return err
}
printData(os.Stdout, c, &data)
printData(os.Stdout, *c, &data)
}
return nil
}
// 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 fds []io.Reader

View File

@@ -19,6 +19,7 @@ package lib
import (
"bufio"
"encoding/csv"
"errors"
"fmt"
"github.com/alecthomas/repr"
@@ -28,20 +29,84 @@ import (
"strings"
)
/*
Parser switch
*/
func Parse(c cfg.Config, input io.Reader) (Tabdata, error) {
if len(c.Separator) == 1 {
return parseCSV(c, input)
}
return parseTabular(c, input)
}
/*
Parse CSV input.
*/
func parseCSV(c cfg.Config, input io.Reader) (Tabdata, error) {
var content io.Reader = input
data := Tabdata{}
if len(c.Pattern) > 0 {
scanner := bufio.NewScanner(input)
lines := []string{}
hadFirst := false
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if hadFirst {
// don't match 1st line, it's the header
if c.PatternR.MatchString(line) == c.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
}
}
lines = append(lines, line)
hadFirst = true
}
content = strings.NewReader(strings.Join(lines, "\n"))
}
csvreader := csv.NewReader(content)
csvreader.Comma = rune(c.Separator[0])
records, err := csvreader.ReadAll()
if err != nil {
return data, errors.Unwrap(fmt.Errorf("Could not parse CSV input: %w", err))
}
if len(records) >= 1 {
data.headers = records[0]
data.columns = len(records)
for _, head := range data.headers {
// register widest header field
headerlen := len(head)
if headerlen > data.maxwidthHeader {
data.maxwidthHeader = headerlen
}
}
if len(records) > 1 {
data.entries = records[1:]
}
}
return data, nil
}
/*
Parse tabular input.
*/
func parseFile(c cfg.Config, input io.Reader, pattern string) (Tabdata, error) {
func parseTabular(c cfg.Config, input io.Reader) (Tabdata, error) {
data := Tabdata{}
var scanner *bufio.Scanner
hadFirst := false
separate := regexp.MustCompile(c.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)
@@ -76,8 +141,8 @@ func parseFile(c cfg.Config, input io.Reader, pattern string) (Tabdata, error) {
}
} else {
// data processing
if len(pattern) > 0 {
if patternR.MatchString(line) == c.InvertMatch {
if len(c.Pattern) > 0 {
if c.PatternR.MatchString(line) == c.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,
@@ -89,16 +154,6 @@ func parseFile(c cfg.Config, input io.Reader, pattern string) (Tabdata, error) {
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)
// }

View File

@@ -25,40 +25,58 @@ import (
"testing"
)
var input = []struct {
name string
text string
separator string
}{
{
name: "tabular-data",
separator: cfg.DefaultSeparator,
text: `
ONE TWO THREE
asd igig cxxxncnc
19191 EDD 1 X`,
},
{
name: "csv-data",
separator: ",",
text: `
ONE,TWO,THREE
asd,igig,cxxxncnc
19191,"EDD 1",X`,
},
}
func TestParser(t *testing.T) {
data := Tabdata{
maxwidthHeader: 5,
maxwidthPerCol: []int{
5, 5, 8,
},
columns: 3,
columns: 3,
headers: []string{
"ONE", "TWO", "THREE",
},
entries: [][]string{
{
"asd", "igig", "cxxxncnc",
},
{
"19191", "EDD 1", "X",
},
{"asd", "igig", "cxxxncnc"},
{"19191", "EDD 1", "X"},
},
}
table := `ONE TWO THREE
asd igig cxxxncnc
19191 EDD 1 X`
for _, in := range input {
testname := fmt.Sprintf("parse-%s", in.name)
t.Run(testname, func(t *testing.T) {
readFd := strings.NewReader(strings.TrimSpace(in.text))
c := cfg.Config{Separator: in.separator}
gotdata, err := Parse(c, readFd)
readFd := strings.NewReader(table)
c := cfg.Config{Separator: cfg.DefaultSeparator}
gotdata, err := parseFile(c, readFd, "")
if err != nil {
t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, gotdata)
}
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", c.Separator, data, gotdata)
if !reflect.DeepEqual(data, gotdata) {
t.Errorf("Parser returned invalid data\nExp: %+v\nGot: %+v\n",
data, gotdata)
}
})
}
}
@@ -71,86 +89,70 @@ func TestParserPatternmatching(t *testing.T) {
}{
{
entries: [][]string{
{
"asd", "igig", "cxxxncnc",
},
{"asd", "igig", "cxxxncnc"},
},
pattern: "ig",
invert: false,
},
{
entries: [][]string{
{
"19191", "EDD 1", "X",
},
{"19191", "EDD 1", "X"},
},
pattern: "ig",
invert: true,
},
{
entries: [][]string{
{
"asd", "igig", "cxxxncnc",
},
},
pattern: "[a-z",
want: true,
},
}
table := `ONE TWO THREE
asd igig cxxxncnc
19191 EDD 1 X`
for _, in := range input {
for _, tt := range tests {
testname := fmt.Sprintf("parse-%s-with-pattern-%s-inverted-%t",
in.name, tt.pattern, tt.invert)
t.Run(testname, func(t *testing.T) {
c := cfg.Config{InvertMatch: tt.invert, Pattern: tt.pattern,
Separator: in.separator}
for _, tt := range tests {
testname := fmt.Sprintf("parse-with-pattern-%s-inverted-%t", tt.pattern, tt.invert)
t.Run(testname, func(t *testing.T) {
c := cfg.Config{InvertMatch: tt.invert, Pattern: tt.pattern, Separator: cfg.DefaultSeparator}
_ = c.PreparePattern(tt.pattern)
readFd := strings.NewReader(table)
gotdata, err := parseFile(c, readFd, tt.pattern)
readFd := strings.NewReader(strings.TrimSpace(in.text))
gotdata, err := Parse(c, readFd)
if err != nil {
if !tt.want {
t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, gotdata)
if err != nil {
if !tt.want {
t.Errorf("Parser returned error: %s\nData processed so far: %+v",
err, gotdata)
}
} else {
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)
}
}
} else {
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)
}
}
})
})
}
}
}
func TestParserIncompleteRows(t *testing.T) {
data := Tabdata{
maxwidthHeader: 5,
maxwidthPerCol: []int{
5, 5, 1,
},
columns: 3,
columns: 3,
headers: []string{
"ONE", "TWO", "THREE",
},
entries: [][]string{
{
"asd", "igig", "",
},
{
"19191", "EDD 1", "X",
},
{"asd", "igig", ""},
{"19191", "EDD 1", "X"},
},
}
table := `ONE TWO THREE
table := `
ONE TWO THREE
asd igig
19191 EDD 1 X`
readFd := strings.NewReader(table)
readFd := strings.NewReader(strings.TrimSpace(table))
c := cfg.Config{Separator: cfg.DefaultSeparator}
gotdata, err := parseFile(c, readFd, "")
gotdata, err := Parse(c, readFd)
if err != nil {
t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, gotdata)

View File

@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package lib
import (
"encoding/csv"
"fmt"
"github.com/gookit/color"
"github.com/olekukonko/tablewriter"
@@ -43,21 +44,24 @@ func printData(w io.Writer, c cfg.Config, data *Tabdata) {
sortTable(c, data)
switch c.OutputMode {
case "extended":
case cfg.Extended:
printExtendedData(w, c, data)
case "ascii":
case cfg.Ascii:
printAsciiData(w, c, data)
case "orgtbl":
case cfg.Orgtbl:
printOrgmodeData(w, c, data)
case "markdown":
case cfg.Markdown:
printMarkdownData(w, c, data)
case "shell":
case cfg.Shell:
printShellData(w, c, data)
case "yaml":
case cfg.Yaml:
printYamlData(w, c, data)
case cfg.CSV:
printCSVData(w, c, data)
default:
printAsciiData(w, c, data)
}
}
func output(w io.Writer, str string) {
@@ -185,7 +189,7 @@ func printShellData(w io.Writer, c cfg.Config, data *Tabdata) {
}
}
// no colrization here
// no colorization here
output(w, out)
}
@@ -224,3 +228,23 @@ func printYamlData(w io.Writer, c cfg.Config, data *Tabdata) {
output(w, string(yamlstr))
}
func printCSVData(w io.Writer, c cfg.Config, data *Tabdata) {
csvout := csv.NewWriter(w)
if err := csvout.Write(data.headers); err != nil {
log.Fatalln("error writing record to csv:", err)
}
for _, entry := range data.entries {
if err := csvout.Write(entry); err != nil {
log.Fatalln("error writing record to csv:", err)
}
}
csvout.Flush()
if err := csvout.Error(); err != nil {
log.Fatal(err)
}
}

View File

@@ -29,13 +29,7 @@ import (
func newData() Tabdata {
return Tabdata{
maxwidthHeader: 8,
maxwidthPerCol: []int{
5,
9,
3,
26,
},
columns: 4,
columns: 4,
headers: []string{
"NAME",
"DURATION",
@@ -72,24 +66,33 @@ var tests = []struct {
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
mode int // 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",
mode: cfg.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`,
},
{
mode: cfg.CSV,
name: "csv",
expect: `
NAME,DURATION,COUNT,WHEN
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",
mode: cfg.Orgtbl,
expect: `
+---------+-------------+----------+----------------------------+
| NAME(1) | DURATION(2) | COUNT(3) | WHEN(4) |
@@ -101,7 +104,7 @@ ceta 33d12h 9 06/Jan/2008 15:04:05 -0700`,
},
{
name: "default",
mode: "markdown",
mode: cfg.Markdown,
expect: `
| NAME(1) | DURATION(2) | COUNT(3) | WHEN(4) |
|---------|-------------|----------|----------------------------|
@@ -111,7 +114,7 @@ ceta 33d12h 9 06/Jan/2008 15:04:05 -0700`,
},
{
name: "default",
mode: "shell",
mode: cfg.Shell,
nonum: true,
expect: `
NAME="beta" DURATION="1d10h5m1s" COUNT="33" WHEN="3/1/2014"
@@ -120,7 +123,7 @@ NAME="ceta" DURATION="33d12h" COUNT="9" WHEN="06/Jan/2008 15:04:05 -0700"`,
},
{
name: "default",
mode: "yaml",
mode: cfg.Yaml,
nonum: true,
expect: `
entries:
@@ -139,7 +142,7 @@ entries:
},
{
name: "default",
mode: "extended",
mode: cfg.Extended,
expect: `
NAME(1): beta
DURATION(2): 1d10h5m1s
@@ -248,7 +251,7 @@ DURATION(2) WHEN(4)
func TestPrinter(t *testing.T) {
for _, tt := range tests {
testname := fmt.Sprintf("print-sortcol-%d-desc-%t-sortby-%s-mode-%s-usecolumns-%s",
testname := fmt.Sprintf("print-sortcol-%d-desc-%t-sortby-%s-mode-%d-usecolumns-%s",
tt.column, tt.desc, tt.sortby, tt.mode, tt.usecolstr)
t.Run(testname, func(t *testing.T) {
// replaces os.Stdout, but we ignore it
@@ -265,6 +268,8 @@ func TestPrinter(t *testing.T) {
NoColor: true,
}
c.ApplyDefaults()
// the test checks the len!
if len(tt.usecol) > 0 {
c.Columns = "yes"

View File

@@ -133,7 +133,7 @@
.\" ========================================================================
.\"
.IX Title "TABLIZER 1"
.TH TABLIZER 1 "2022-10-16" "1" "User Commands"
.TH TABLIZER 1 "2022-10-23" "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
@@ -146,24 +146,33 @@ tablizer \- Manipulate tabular output of other programs
\& Usage:
\& tablizer [regex] [file, ...] [flags]
\&
\& Flags:
\& Operational 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, yaml, ascii(default)
\& \-s, \-\-separator string Custom field separator
\& \-k, \-\-sort\-by int Sort by column (default: 1)
\&
\& Output Flags (mutually exclusive):
\& \-X, \-\-extended Enable extended output
\& \-M, \-\-markdown Enable markdown table output
\& \-O, \-\-orgtbl Enable org\-mode table output
\& \-s, \-\-separator string Custom field separator
\& \-S, \-\-shell Enable shell evaluable ouput
\& \-Y, \-\-yaml Enable yaml output
\& \-C, \-\-csv Enable CSV output
\& \-A, \-\-ascii Default output mode, ascii tabular
\&
\& Sort Mode Flags (mutually exclusive):
\& \-a, \-\-sort\-age sort according to age (duration) string
\& \-k, \-\-sort\-by int Sort by column (default: 1)
\& \-D, \-\-sort\-desc Sort in descending order (default: ascending)
\& \-i, \-\-sort\-numeric sort according to string numerical value
\& \-t, \-\-sort\-time sort according to time string
\&
\& Other Flags:
\& \-d, \-\-debug Enable debugging
\& \-h, \-\-help help for tablizer
\& \-m, \-\-man Display manual page
\& \-v, \-\-version Print program version
.Ve
.SH "DESCRIPTION"
@@ -345,8 +354,22 @@ 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 and \fByaml\fR, which
prints yaml encoding.
table and \fBmarkdown\fR which prints a Markdown table, \fByaml\fR, which
prints yaml encoding and \s-1CSV\s0 mode, which prints a comma separated
value file.
.SS "\s-1ENVIRONMENT VARIABLES\s0"
.IX Subsection "ENVIRONMENT VARIABLES"
\&\fBtablizer\fR supports certain environment variables which use can use
to influence program behavior. Commandline flags have always
precedence over environment variables.
.IP "<T_NO_HEADER_NUMBERING> \- disable numbering of header fields, like \fB\-n\fR." 4
.IX Item "<T_NO_HEADER_NUMBERING> - disable numbering of header fields, like -n."
.PD 0
.IP "<T_COLUMNS> \- comma separated list of columns to output, like \fB\-c\fR" 4
.IX Item "<T_COLUMNS> - comma separated list of columns to output, like -c"
.IP "<\s-1NO_COLORS\s0> \- disable colorization of matches, like \fB\-N\fR" 4
.IX Item "<NO_COLORS> - disable colorization of matches, like -N"
.PD
.SH "BUGS"
.IX Header "BUGS"
In order to report a bug, unexpected behavior, feature requests

View File

@@ -7,24 +7,33 @@ tablizer - Manipulate tabular output of other programs
Usage:
tablizer [regex] [file, ...] [flags]
Flags:
Operational 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, yaml, ascii(default)
-s, --separator string Custom field separator
-k, --sort-by int Sort by column (default: 1)
Output Flags (mutually exclusive):
-X, --extended Enable extended output
-M, --markdown Enable markdown table output
-O, --orgtbl Enable org-mode table output
-s, --separator string Custom field separator
-S, --shell Enable shell evaluable ouput
-Y, --yaml Enable yaml output
-C, --csv Enable CSV output
-A, --ascii Default output mode, ascii tabular
Sort Mode Flags (mutually exclusive):
-a, --sort-age sort according to age (duration) string
-k, --sort-by int Sort by column (default: 1)
-D, --sort-desc Sort in descending order (default: ascending)
-i, --sort-numeric sort according to string numerical value
-t, --sort-time sort according to time string
Other Flags:
-d, --debug Enable debugging
-h, --help help for tablizer
-m, --man Display manual page
-v, --version Print program version
@@ -198,8 +207,26 @@ 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 and B<yaml>, which
prints yaml encoding.
table and B<markdown> which prints a Markdown table, B<yaml>, which
prints yaml encoding and CSV mode, which prints a comma separated
value file.
=head2 ENVIRONMENT VARIABLES
B<tablizer> supports certain environment variables which use can use
to influence program behavior. Commandline flags have always
precedence over environment variables.
=over
=item <T_NO_HEADER_NUMBERING> - disable numbering of header fields, like B<-n>.
=item <T_COLUMNS> - comma separated list of columns to output, like B<-c>
=item <NO_COLORS> - disable colorization of matches, like B<-N>
=back
=head1 BUGS