add support for template output mode with new option --templage <tmpl> (#47)

This commit is contained in:
T. von Dein
2025-12-08 22:01:15 +01:00
parent 2bb0cdb0af
commit fc0352efa9
11 changed files with 216 additions and 48 deletions

View File

@@ -33,6 +33,7 @@ tablizer - Manipulate tabular output of other programs
-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,
@@ -80,16 +81,16 @@ may also use the B<-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 B<-n>,
then every header field will have a numer associated with it, e.g.:
@@ -99,7 +100,7 @@ then every header field will have a numer associated with it, e.g.:
These numbers denote the column and you can use them to specify which
columns you want to have in your output (see L<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.
@@ -107,11 +108,11 @@ 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 B<-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 B<-H> option. Be aware that
@@ -236,7 +237,7 @@ and append the flag. The following flags are supported:
Example for a case insensitive search:
kubectl get pods -A | tablizer "/account/i"
> kubectl get pods -A | tablizer "/account/i"
If you use the C<!> flag, then the regex match will be negated, that
is, if a line in the input matches the given regex, but C<!> is
@@ -307,7 +308,7 @@ Lets take this table:
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
@@ -340,7 +341,7 @@ use a regexp containing the C</> character, eg:
Example:
cat t/testtable2
> cat t/testtable2
NAME DURATION
x 10
a 100
@@ -348,7 +349,7 @@ Example:
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
@@ -365,7 +366,7 @@ useful 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 -o extended
> kubectl get pods | ./tablizer -o extended
NAME: repldepl-7bcd8d5b64-7zq4l
READY: 1/1
STATUS: Running
@@ -379,7 +380,7 @@ 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
> 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"
@@ -389,9 +390,22 @@ 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, B<yaml>, which
prints yaml encoding and CSV mode, which prints a comma separated
prints yaml encoding and B<CSV> mode, which prints a comma separated
value file.
A special output mode ist the B<Template> mode, activated with the
option C<--template>. Template language is the Golang template
language: L<https://pkg.go.dev/text/template>. You can also use lot's
of additional functions from:
L<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.
=head2 PUT FIELDS TO CLIPBOARD
You can let tablizer put fields to the clipboard using the option