mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-17 20:41:03 +01:00
add template output mode
This commit is contained in:
@@ -23,9 +23,9 @@ import (
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"codeberg.org/scip/tablizer/cfg"
|
||||
"codeberg.org/scip/tablizer/lib"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func completion(cmd *cobra.Command, mode string) error {
|
||||
@@ -179,6 +179,8 @@ func Execute() {
|
||||
"Enable ASCII output (default)")
|
||||
rootCmd.MarkFlagsMutuallyExclusive("extended", "markdown", "orgtbl",
|
||||
"shell", "yaml", "csv")
|
||||
rootCmd.PersistentFlags().StringVarP(&conf.Template, "template", "P", "",
|
||||
"template for template output mode")
|
||||
|
||||
// config file
|
||||
rootCmd.PersistentFlags().StringVarP(&conf.Configfile, "config", "f", cfg.DefaultConfigfile,
|
||||
|
||||
@@ -13,6 +13,6 @@ const shortusage = `tablizer [regex,...] [-r file] [flags]
|
||||
-x col,... use custom headers -d debug
|
||||
-o char use char as output separator -g auto generate headers
|
||||
|
||||
-O org -C CSV -M md -X ext -S shell -Y yaml -J json -D sort descending order
|
||||
-m show manual --help show detailed help -v show version
|
||||
-a sort by age -i sort numerically -t sort by time`
|
||||
-O org -C CSV -M md -X ext -S shell -Y yaml -J json -P template
|
||||
-a sort by age -i sort numerically -t sort by time -D sort descending order
|
||||
-m show manual -v show version --help show detailed help`
|
||||
|
||||
@@ -34,6 +34,7 @@ SYNOPSIS
|
||||
-J, --jsonout Enable JSON output
|
||||
-C, --csv Enable CSV output
|
||||
-A, --ascii Default output mode, ascii tabular
|
||||
-P, --template <tpl> Enable template mode with template <tpl>
|
||||
-L, --hightlight-lines Use alternating background colors for tables
|
||||
-o, --ofs <char> Output field separator, used by -A and -C.
|
||||
-y, --yank-columns Yank specified columns (separated by ,) to clipboard,
|
||||
@@ -77,16 +78,16 @@ DESCRIPTION
|
||||
use the -v option to exclude all rows which match the pattern. Hence:
|
||||
|
||||
# read from STDIN
|
||||
kubectl get pods | tablizer
|
||||
> kubectl get pods | tablizer
|
||||
|
||||
# read a file
|
||||
tablizer -r filename
|
||||
> tablizer -r filename
|
||||
|
||||
# search for pattern in a file (works like grep)
|
||||
tablizer regex -r filename
|
||||
> tablizer regex -r filename
|
||||
|
||||
# search for pattern in STDIN
|
||||
kubectl get pods | tablizer regex
|
||||
> kubectl get pods | tablizer regex
|
||||
|
||||
The output looks like the original one. You can add the option -n, then
|
||||
every header field will have a numer associated with it, e.g.:
|
||||
@@ -96,18 +97,18 @@ DESCRIPTION
|
||||
These numbers denote the column and you can use them to specify which
|
||||
columns you want to have in your output (see COLUMNS:
|
||||
|
||||
kubectl get pods | tablizer -c1,3
|
||||
> kubectl get pods | tablizer -c1,3
|
||||
|
||||
You can specify the numbers in any order but output will always follow
|
||||
the original order.
|
||||
|
||||
However, you may also just use the header names instead of numbers, eg:
|
||||
|
||||
kubectl get pods | tablizer -cname,status
|
||||
> kubectl get pods | tablizer -cname,status
|
||||
|
||||
You can also use regular expressions with -c, eg:
|
||||
|
||||
kubectl get pods | tablizer -c '[ae]'
|
||||
> kubectl get pods | tablizer -c '[ae]'
|
||||
|
||||
By default tablizer shows a header containing the names of each column.
|
||||
This can be disabled using the -H option. Be aware that this only
|
||||
@@ -218,7 +219,7 @@ DESCRIPTION
|
||||
|
||||
Example for a case insensitive search:
|
||||
|
||||
kubectl get pods -A | tablizer "/account/i"
|
||||
> kubectl get pods -A | tablizer "/account/i"
|
||||
|
||||
If you use the "!" flag, then the regex match will be negated, that is,
|
||||
if a line in the input matches the given regex, but "!" is supplied,
|
||||
@@ -284,7 +285,7 @@ DESCRIPTION
|
||||
|
||||
We want to see only the CMD column and use a regex for this:
|
||||
|
||||
ps | tablizer -s '\s+' -c C
|
||||
> ps | tablizer -s '\s+' -c C
|
||||
CMD(4)
|
||||
bash
|
||||
ps
|
||||
@@ -315,7 +316,7 @@ DESCRIPTION
|
||||
|
||||
Example:
|
||||
|
||||
cat t/testtable2
|
||||
> cat t/testtable2
|
||||
NAME DURATION
|
||||
x 10
|
||||
a 100
|
||||
@@ -323,7 +324,7 @@ DESCRIPTION
|
||||
u 4
|
||||
k 6
|
||||
|
||||
cat t/testtable2 | tablizer -T2 -R '/^\d/4/' -n
|
||||
> cat t/testtable2 | tablizer -T2 -R '/^\d/4/' -n
|
||||
NAME DURATION
|
||||
x 40
|
||||
a 400
|
||||
@@ -339,7 +340,7 @@ DESCRIPTION
|
||||
header left, value right, aligned by the field widths. Here's an
|
||||
example:
|
||||
|
||||
kubectl get pods | ./tablizer -o extended
|
||||
> kubectl get pods | ./tablizer -o extended
|
||||
NAME: repldepl-7bcd8d5b64-7zq4l
|
||||
READY: 1/1
|
||||
STATUS: Running
|
||||
@@ -352,7 +353,7 @@ DESCRIPTION
|
||||
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
|
||||
> 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"
|
||||
@@ -364,6 +365,18 @@ DESCRIPTION
|
||||
markdown which prints a Markdown table, yaml, which prints yaml encoding
|
||||
and CSV mode, which prints a comma separated value file.
|
||||
|
||||
A special output mode ist the Template mode, activated with the option
|
||||
"--template". Template language is the Golang template language:
|
||||
<https://pkg.go.dev/text/template>. You can also use lot's of additional
|
||||
functions from: <https://masterminds.github.io/sprig/>. Here's an
|
||||
example:
|
||||
|
||||
> kubectl get pods | tablizer --template "{{.name}} is {{.status}}"
|
||||
alertmanager-kube-prometheus-alertmanager-0 is Running
|
||||
grafana-fcc54cbc9-bk7s8 is Running
|
||||
|
||||
You can use header names as variables.
|
||||
|
||||
PUT FIELDS TO CLIPBOARD
|
||||
You can let tablizer put fields to the clipboard using the option "-y".
|
||||
This best fits the use-case when the result of your filtering yields
|
||||
@@ -532,6 +545,7 @@ Output Flags (mutually exclusive):
|
||||
-J, --jsonout Enable JSON output
|
||||
-C, --csv Enable CSV output
|
||||
-A, --ascii Default output mode, ascii tabular
|
||||
-P, --template <tpl> Enable template mode with template <tpl>
|
||||
-L, --hightlight-lines Use alternating background colors for tables
|
||||
-o, --ofs <char> Output field separator, used by -A and -C.
|
||||
-y, --yank-columns Yank specified columns (separated by ,) to clipboard,
|
||||
|
||||
Reference in New Issue
Block a user