Skip to content

Commit

Permalink
get rid of lisp interpreter, -R and -F are enough, fixes #30
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas von Dein committed Jan 14, 2025
1 parent 4a41833 commit 06c7508
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 674 deletions.
46 changes: 43 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,49 @@ Tablizer can be used to re-format tabular output of other
programs. While you could do this using standard unix tools, in some
cases it's a hard job.

Usage:
```default
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
-H, --no-headers Disable headers display
-s, --separator string Custom field separator
-k, --sort-by int Sort by column (default: 1)
-z, --fuzzy Use fuzzy search [experimental]
-F, --filter field=reg Filter given field with regex, can be used multiple times
-T, --transpose-columns string Transpose the speficied columns (separated by ,)
-R, --regex-transposer /from/to/ Apply /search/replace/ regexp to fields given in -T
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 output
-Y, --yaml Enable yaml output
-C, --csv Enable CSV output
-A, --ascii Default output mode, ascii tabular
-L, --hightlight-lines Use alternating background colors for tables
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:
--completion <shell> Generate the autocompletion script for <shell>
-f, --config <file> Configuration file (default: ~/.config/tablizer/config)
-d, --debug Enable debugging
-h, --help help for tablizer
-m, --man Display manual page
-V, --version Print program version
```

Let's take this output:
```
% kubectl get pods -o wide
Expand Down Expand Up @@ -99,9 +142,6 @@ a dash. If you need to work with `/` characters, you can also use any
other separator, for instance: `-R '| |-|'`.


Last but not least tablizer has support for plugins written in
lisp. This feature is expermental yet. Take a look into the manpage
for details.

## Demo

Expand Down
10 changes: 0 additions & 10 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,3 @@

- add --no-headers option

### Lisp Plugin Infrastructure using zygo

Hooks:

| Filter | Purpose | Args | Return |
|-----------|-------------------------------------------------------------|---------------------|--------|
| filter | include or exclude lines | row as hash | bool |
| process | do calculations with data, store results in global lisp env | whole dataset | nil |
| transpose | modify a cell | headername and cell | cell |
| append | add one or more rows to the dataset (use this to add stats) | nil | rows |
14 changes: 0 additions & 14 deletions cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"regexp"
"strings"

"github.com/glycerine/zygomys/zygo"
"github.com/gookit/color"
"github.com/hashicorp/hcl/v2/hclsimple"
)
Expand All @@ -32,7 +31,6 @@ const DefaultSeparator string = `(\s\s+|\t)`
const Version string = "v1.3.0"
const MAXPARTS = 2

var DefaultLoadPath = os.Getenv("HOME") + "/.config/tablizer/lisp"
var DefaultConfigfile = os.Getenv("HOME") + "/.config/tablizer/config"

var VERSION string // maintained by -x
Expand Down Expand Up @@ -89,13 +87,6 @@ type Config struct {

NoColor bool

// special case: we use the config struct to transport the lisp
// env trough the program
Lisp *zygo.Zlisp

// a path containing lisp scripts to be loaded on startup
LispLoadPath string

// config file, optional
Configfile string

Expand Down Expand Up @@ -138,9 +129,6 @@ type Sortmode struct {
Age bool
}

// valid lisp hooks
var ValidHooks []string

// default color schemes
func (conf *Config) Colors() map[color.Level]map[string]color.Color {
colors := map[color.Level]map[string]color.Color{
Expand Down Expand Up @@ -342,8 +330,6 @@ func (conf *Config) ApplyDefaults() {
if conf.OutputMode == Yaml || conf.OutputMode == CSV {
conf.NoNumbering = true
}

ValidHooks = []string{"filter", "process", "transpose", "append"}
}

func (conf *Config) PreparePattern(pattern string) error {
Expand Down
7 changes: 0 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@ func Execute() {
conf.DetermineColormode()
conf.ApplyDefaults()

// setup lisp env, load plugins etc
wrapE(lib.SetupLisp(&conf))

// actual execution starts here
wrapE(lib.ProcessFiles(&conf, args))
},
Expand Down Expand Up @@ -187,10 +184,6 @@ func Execute() {
rootCmd.MarkFlagsMutuallyExclusive("extended", "markdown", "orgtbl",
"shell", "yaml", "csv")

// lisp options
rootCmd.PersistentFlags().StringVarP(&conf.LispLoadPath, "load-path", "l", cfg.DefaultLoadPath,
"Load path for lisp plugins (expects *.zy files)")

// config file
rootCmd.PersistentFlags().StringVarP(&conf.Configfile, "config", "f", cfg.DefaultConfigfile,
"config file (default: ~/.config/tablizer/config)")
Expand Down
56 changes: 0 additions & 56 deletions cmd/tablizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ SYNOPSIS
Other Flags:
--completion <shell> Generate the autocompletion script for <shell>
-f, --config <file> Configuration file (default: ~/.config/tablizer/config)
-l, --load-path <path> Load path for lisp plugins (expects *.zy files)
-d, --debug Enable debugging
-h, --help help for tablizer
-m, --man Display manual page
Expand Down Expand Up @@ -344,60 +343,6 @@ CONFIGURATION AND COLORS
Colorization can be turned off completely either by setting the
parameter "-N" or the environment variable NO_COLOR to a true value.
LISP PLUGINS [experimental]
Tablizer supports plugins written in zygomys lisp. You can supply a
directory to the "-l" parameter containing *.zy files or a single .zy
file containing lisp code.
You can put as much code as you want into the file, but you need to add
one lips function to a hook at the end.
The following hooks are available:
filter
The filter hook works one a whole line of the input. Your hook
function is expected to return true or false. If you return true,
the line will be included in the output, otherwise not.
Multiple filter hook functions are supported.
Example:
/*
Simple filter hook function. Splits the argument by whitespace,
fetches the 2nd element, converts it to an int and returns true
if it s larger than 5, false otherwise.
*/
(defn uselarge [line]
(cond (> (atoi (second (resplit line ` +`))) 5) true false))
/* Register the filter hook */
(addhook %filter %uselarge)
process
The process hook function gets a table containing the parsed input
data (see "lib/common.go:type Tabdata struct". It is expected to
return a pair containing a bool to denote if the table has been
modified, and the [modified] table. The resulting table may have
less rows than the original and cells may have changed content but
the number of columns must persist.
transpose
not yet implemented.
append
not yet implemented.
Beside the existing language features, the following additional lisp
functions are provided by tablizer:
(resplit [string, regex]) => list
(atoi [string]) => int
(matchre [string, regex]) => bool
The standard language is described here:
<https://github.com/glycerine/zygomys/wiki/Language>.
BUGS
In order to report a bug, unexpected behavior, feature requests or to
submit a patch, please open an issue on github:
Expand Down Expand Up @@ -472,7 +417,6 @@ Sort Mode Flags (mutually exclusive):
Other Flags:
--completion <shell> Generate the autocompletion script for <shell>
-f, --config <file> Configuration file (default: ~/.config/tablizer/config)
-l, --load-path <path> Load path for lisp plugins (expects *.zy files)
-d, --debug Enable debugging
-h, --help help for tablizer
-m, --man Display manual page
Expand Down
13 changes: 0 additions & 13 deletions lib/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package lib

import (
"bufio"
"fmt"
"io"
"strings"

Expand Down Expand Up @@ -144,18 +143,6 @@ func FilterByPattern(conf cfg.Config, input io.Reader) (io.Reader, error) {
// so we ignore all lines, which DO match.
continue
}

// apply user defined lisp filters, if any
accept, err := RunFilterHooks(conf, line)
if err != nil {
return input, fmt.Errorf("failed to apply filter hook: %w", err)
}

if !accept {
// IF there are filter hook[s] and IF one of them
// returns false on the current line, reject it
continue
}
}

lines = append(lines, line)
Expand Down
Loading

0 comments on commit 06c7508

Please sign in to comment.