Improve ascii table, add --ofs flag, enhance documentation (#67)

* enhanced documentation
* added --ofs parameter
  use 2 spaces for ascii output but it is customizable with --ofs,
  which is also being used by CSV mode (whose defaults remains unchanged)
* improve ascii table output, use 2 spaces as OFS by default
This commit is contained in:
T.v.Dein
2025-09-30 11:21:49 +02:00
committed by GitHub
parent 687f4b7bb2
commit 5f3f7c417c
8 changed files with 249 additions and 192 deletions

View File

@@ -28,12 +28,12 @@ Operational Flags:
-n, --numbering Enable header numbering -n, --numbering Enable header numbering
-N, --no-color Disable pattern highlighting -N, --no-color Disable pattern highlighting
-H, --no-headers Disable headers display -H, --no-headers Disable headers display
-s, --separator string Custom field separator -s, --separator <string> Custom field separator
-k, --sort-by int|name Sort by column (default: 1) -k, --sort-by <int|name> Sort by column (default: 1)
-z, --fuzzy Use fuzzy search [experimental] -z, --fuzzy Use fuzzy search [experimental]
-F, --filter field[!]=reg Filter given field with regex, can be used multiple times -F, --filter <field[!]=reg> Filter given field with regex, can be used multiple times
-T, --transpose-columns string Transpose the speficied columns (separated by ,) -T, --transpose-columns string Transpose the speficied columns (separated by ,)
-R, --regex-transposer /from/to/ Apply /search/replace/ regexp to fields given in -T -R, --regex-transposer </from/to/> Apply /search/replace/ regexp to fields given in -T
-I, --interactive Interactively filter and select rows -I, --interactive Interactively filter and select rows
Output Flags (mutually exclusive): Output Flags (mutually exclusive):
@@ -47,6 +47,7 @@ Output Flags (mutually exclusive):
-L, --hightlight-lines Use alternating background colors for tables -L, --hightlight-lines Use alternating background colors for tables
-y, --yank-columns Yank specified columns (separated by ,) to clipboard, -y, --yank-columns Yank specified columns (separated by ,) to clipboard,
space separated space separated
--ofs <char> Output field separator, used by -A and -C.
Sort Mode Flags (mutually exclusive): Sort Mode Flags (mutually exclusive):
-a, --sort-age sort according to age (duration) string -a, --sort-age sort according to age (duration) string
@@ -55,6 +56,7 @@ Sort Mode Flags (mutually exclusive):
-t, --sort-time sort according to time string -t, --sort-time sort according to time string
Other Flags: Other Flags:
-r --read-file <file> Use <file> as input instead of STDIN
--completion <shell> Generate the autocompletion script for <shell> --completion <shell> Generate the autocompletion script for <shell>
-f, --config <file> Configuration file (default: ~/.config/tablizer/config) -f, --config <file> Configuration file (default: ~/.config/tablizer/config)
-d, --debug Enable debugging -d, --debug Enable debugging
@@ -77,13 +79,13 @@ to do this with tablizer:
``` ```
% kubectl get pods | tablizer % kubectl get pods | tablizer
NAME(1) READY(2) STATUS(3) RESTARTS(4) AGE(5) NAME READY STATUS RESTARTS AGE
repldepl-7bcd8d5b64-7zq4l 1/1 Running 1 (69m ago) 5h26m repldepl-7bcd8d5b64-7zq4l 1/1 Running 1 (69m ago) 5h26m
repldepl-7bcd8d5b64-m48n8 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 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) NAME STATUS
repldepl-7bcd8d5b64-7zq4l Running repldepl-7bcd8d5b64-7zq4l Running
repldepl-7bcd8d5b64-m48n8 Running repldepl-7bcd8d5b64-m48n8 Running
repldepl-7bcd8d5b64-q2bf4 Running repldepl-7bcd8d5b64-q2bf4 Running
@@ -121,14 +123,14 @@ 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) NAME READY STATUS RESTARTS AGE
repldepl-7bcd8d5b64-q2bf4 1/1 Running 1 (69m ago) 5h26m repldepl-7bcd8d5b64-q2bf4 1/1 Running 1 (69m ago) 5h26m
``` ```
Sometimes a filter regex is to broad and you wish to filter only on a Sometimes a filter regex is to broad and you wish to filter only on a
particular column. This is possible using `-F`: particular column. This is possible using `-F`:
``` ```
% kubectl get pods | tablizer -n -Fname=2 % kubectl get pods | tablizer -Fname=2
NAME READY STATUS RESTARTS AGE NAME READY STATUS RESTARTS AGE
repldepl-7bcd8d5b64-q2bf4 1/1 Running 1 (69m ago) 5h26m repldepl-7bcd8d5b64-q2bf4 1/1 Running 1 (69m ago) 5h26m
``` ```
@@ -142,7 +144,7 @@ You can also use it to modify certain cells using regular expression
matching. For example: matching. For example:
```shell ```shell
kubectl get pods | tablizer -n -T4 -R '/ /-/' kubectl get pods | tablizer -T4 -R '/ /-/'
NAME READY STATUS RESTARTS AGE NAME READY STATUS RESTARTS AGE
repldepl-7bcd8d5b64-7zq4l 1/1 Running 1-(69m-ago) 5h26m repldepl-7bcd8d5b64-7zq4l 1/1 Running 1-(69m-ago) 5h26m
repldepl-7bcd8d5b64-m48n8 1/1 Running 1-(69m-ago) 5h26m repldepl-7bcd8d5b64-m48n8 1/1 Running 1-(69m-ago) 5h26m
@@ -153,6 +155,11 @@ Here, we modified the 4th column (`-T4`) by replacing every space with
a dash. If you need to work with `/` characters, you can also use any a dash. If you need to work with `/` characters, you can also use any
other separator, for instance: `-R '| |-|'`. other separator, for instance: `-R '| |-|'`.
There's also an interactive mode, invoked with the option B<-I>, where
you can interactively filter and select rows:
<img width="937" height="293" alt="interactive" src="https://github.com/user-attachments/assets/0d4d65e2-d156-43ed-8021-39047c7939ed" />
## Installation ## Installation

View File

@@ -28,7 +28,7 @@ import (
) )
const DefaultSeparator string = `(\s\s+|\t)` const DefaultSeparator string = `(\s\s+|\t)`
const Version string = "v1.5.4" const Version string = "v1.5.5"
const MAXPARTS = 2 const MAXPARTS = 2
var DefaultConfigfile = os.Getenv("HOME") + "/.config/tablizer/config" var DefaultConfigfile = os.Getenv("HOME") + "/.config/tablizer/config"
@@ -112,6 +112,8 @@ type Config struct {
// -r <file> // -r <file>
InputFile string InputFile string
OFS string
} }
// maps outputmode short flags to output mode, ie. -O => -o orgtbl // maps outputmode short flags to output mode, ie. -O => -o orgtbl

View File

@@ -132,6 +132,8 @@ func Execute() {
"Transpose the speficied columns (separated by ,)") "Transpose the speficied columns (separated by ,)")
rootCmd.PersistentFlags().BoolVarP(&conf.Interactive, "interactive", "I", false, rootCmd.PersistentFlags().BoolVarP(&conf.Interactive, "interactive", "I", false,
"interactive mode (experimental)") "interactive mode (experimental)")
rootCmd.PersistentFlags().StringVarP(&conf.OFS, "ofs", "", "",
"Output field separator (' ' for ascii table, ',' for CSV)")
// sort options // sort options
rootCmd.PersistentFlags().StringVarP(&conf.SortByColumn, "sort-by", "k", "", rootCmd.PersistentFlags().StringVarP(&conf.SortByColumn, "sort-by", "k", "",

View File

@@ -14,12 +14,12 @@ SYNOPSIS
-n, --numbering Enable header numbering -n, --numbering Enable header numbering
-N, --no-color Disable pattern highlighting -N, --no-color Disable pattern highlighting
-H, --no-headers Disable headers display -H, --no-headers Disable headers display
-s, --separator string Custom field separator -s, --separator <string> Custom field separator
-k, --sort-by int|name Sort by column (default: 1) -k, --sort-by <int|name> Sort by column (default: 1)
-z, --fuzzy Use fuzzy search [experimental] -z, --fuzzy Use fuzzy search [experimental]
-F, --filter field[!]=reg Filter given field with regex, can be used multiple times -F, --filter <field[!]=reg> Filter given field with regex, can be used multiple times
-T, --transpose-columns string Transpose the speficied columns (separated by ,) -T, --transpose-columns string Transpose the speficied columns (separated by ,)
-R, --regex-transposer /from/to/ Apply /search/replace/ regexp to fields given in -T -R, --regex-transposer </from/to/> Apply /search/replace/ regexp to fields given in -T
-I, --interactive Interactively filter and select rows -I, --interactive Interactively filter and select rows
Output Flags (mutually exclusive): Output Flags (mutually exclusive):
@@ -33,6 +33,7 @@ SYNOPSIS
-L, --hightlight-lines Use alternating background colors for tables -L, --hightlight-lines Use alternating background colors for tables
-y, --yank-columns Yank specified columns (separated by ,) to clipboard, -y, --yank-columns Yank specified columns (separated by ,) to clipboard,
space separated space separated
--ofs <char> Output field separator, used by -A and -C.
Sort Mode Flags (mutually exclusive): Sort Mode Flags (mutually exclusive):
-a, --sort-age sort according to age (duration) string -a, --sort-age sort according to age (duration) string
@@ -75,16 +76,16 @@ DESCRIPTION
kubectl get pods | tablizer kubectl get pods | tablizer
# read a file # read a file
tablizer filename tablizer -r filename
# search for pattern in a file (works like grep) # search for pattern in a file (works like grep)
tablizer regex filename tablizer regex -r filename
# search for pattern in STDIN # search for pattern in STDIN
kubectl get pods | tablizer regex kubectl get pods | tablizer regex
The output looks like the original one but every header field will have The output looks like the original one. You can add the option -n, then
a numer associated with it, e.g.: every header field will have a numer associated with it, e.g.:
NAME(1) READY(2) STATUS(3) RESTARTS(4) AGE(5) NAME(1) READY(2) STATUS(3) RESTARTS(4) AGE(5)
@@ -96,7 +97,13 @@ DESCRIPTION
You can specify the numbers in any order but output will always follow You can specify the numbers in any order but output will always follow
the original order. the original order.
The numbering can be suppressed by using the -n option. However, you may also just use the header names instead of numbers, eg:
kubectl get pods | tablizer -cname,status
You can also use regular expressions with -c, eg:
kubectl get pods | tablizer -c '[ae]'
By default tablizer shows a header containing the names of each column. 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 This can be disabled using the -H option. Be aware that this only
@@ -450,12 +457,12 @@ Operational Flags:
-n, --numbering Enable header numbering -n, --numbering Enable header numbering
-N, --no-color Disable pattern highlighting -N, --no-color Disable pattern highlighting
-H, --no-headers Disable headers display -H, --no-headers Disable headers display
-s, --separator string Custom field separator -s, --separator <string> Custom field separator
-k, --sort-by int|name Sort by column (default: 1) -k, --sort-by <int|name> Sort by column (default: 1)
-z, --fuzzy Use fuzzy search [experimental] -z, --fuzzy Use fuzzy search [experimental]
-F, --filter field[!]=reg Filter given field with regex, can be used multiple times -F, --filter <field[!]=reg> Filter given field with regex, can be used multiple times
-T, --transpose-columns string Transpose the speficied columns (separated by ,) -T, --transpose-columns string Transpose the speficied columns (separated by ,)
-R, --regex-transposer /from/to/ Apply /search/replace/ regexp to fields given in -T -R, --regex-transposer </from/to/> Apply /search/replace/ regexp to fields given in -T
-I, --interactive Interactively filter and select rows -I, --interactive Interactively filter and select rows
Output Flags (mutually exclusive): Output Flags (mutually exclusive):
@@ -469,6 +476,7 @@ Output Flags (mutually exclusive):
-L, --hightlight-lines Use alternating background colors for tables -L, --hightlight-lines Use alternating background colors for tables
-y, --yank-columns Yank specified columns (separated by ,) to clipboard, -y, --yank-columns Yank specified columns (separated by ,) to clipboard,
space separated space separated
--ofs <char> Output field separator, used by -A and -C.
Sort Mode Flags (mutually exclusive): Sort Mode Flags (mutually exclusive):
-a, --sort-age sort according to age (duration) string -a, --sort-age sort according to age (duration) string

View File

@@ -62,7 +62,7 @@ func printData(writer io.Writer, conf cfg.Config, data *Tabdata) {
case cfg.Yaml: case cfg.Yaml:
printYamlData(writer, data) printYamlData(writer, data)
case cfg.CSV: case cfg.CSV:
printCSVData(writer, data) printCSVData(writer, conf, data)
default: default:
printASCIIData(writer, conf, data) printASCIIData(writer, conf, data)
} }
@@ -194,6 +194,11 @@ func printMarkdownData(writer io.Writer, conf cfg.Config, data *Tabdata) {
Simple ASCII table without any borders etc, just like the input we expect Simple ASCII table without any borders etc, just like the input we expect
*/ */
func printASCIIData(writer io.Writer, conf cfg.Config, data *Tabdata) { func printASCIIData(writer io.Writer, conf cfg.Config, data *Tabdata) {
OFS := " "
if conf.OFS != "" {
OFS = conf.OFS
}
tableString := &strings.Builder{} tableString := &strings.Builder{}
styleTSV := tw.NewSymbolCustom("space").WithColumn("\t") styleTSV := tw.NewSymbolCustom("space").WithColumn("\t")
@@ -213,14 +218,14 @@ func printASCIIData(writer io.Writer, conf cfg.Config, data *Tabdata) {
Formatting: tw.CellFormatting{ Formatting: tw.CellFormatting{
AutoFormat: tw.Off, AutoFormat: tw.Off,
}, },
Padding: tw.CellPadding{Global: tw.Padding{Left: "", Right: " "}}, Padding: tw.CellPadding{Global: tw.Padding{Left: "", Right: OFS}},
}, },
Row: tw.CellConfig{ Row: tw.CellConfig{
Formatting: tw.CellFormatting{ Formatting: tw.CellFormatting{
AutoWrap: tw.WrapNone, AutoWrap: tw.WrapNone,
Alignment: tw.AlignLeft, Alignment: tw.AlignLeft,
}, },
Padding: tw.CellPadding{Global: tw.Padding{Right: " "}}, Padding: tw.CellPadding{Global: tw.Padding{Right: OFS}},
}, },
Debug: true, Debug: true,
@@ -323,8 +328,14 @@ func printYamlData(writer io.Writer, data *Tabdata) {
output(writer, string(yamlstr)) output(writer, string(yamlstr))
} }
func printCSVData(writer io.Writer, data *Tabdata) { func printCSVData(writer io.Writer, conf cfg.Config, data *Tabdata) {
OFS := ","
if conf.OFS != "" {
OFS = conf.OFS
}
csvout := csv.NewWriter(writer) csvout := csv.NewWriter(writer)
csvout.Comma = []rune(OFS)[0]
if err := csvout.Write(data.headers); err != nil { if err := csvout.Write(data.headers); err != nil {
log.Fatalln("error writing record to csv:", err) log.Fatalln("error writing record to csv:", err)

View File

@@ -280,6 +280,11 @@ func TestPrinter(t *testing.T) {
Numbering: testdata.numberize, Numbering: testdata.numberize,
UseColumns: testdata.usecol, UseColumns: testdata.usecol,
NoColor: true, NoColor: true,
OFS: " ",
}
if conf.OutputMode == cfg.CSV {
conf.OFS = ","
} }
if testdata.column > 0 { if testdata.column > 0 {
@@ -304,8 +309,10 @@ func TestPrinter(t *testing.T) {
if got != exp { if got != exp {
t.Errorf("not rendered correctly:\n+++ got:\n%s\n+++ want:\n%s", t.Errorf("not rendered correctly:\n+++ got:\n%s\n+++ want:\n%s",
strings.ReplaceAll(got, " ", "_"), got, exp,
strings.ReplaceAll(exp, " ", "_")) // strings.ReplaceAll(got, " ", "_"),
// strings.ReplaceAll(exp, " ", "_")
)
} }
}) })
} }

View File

@@ -133,7 +133,7 @@
.\" ======================================================================== .\" ========================================================================
.\" .\"
.IX Title "TABLIZER 1" .IX Title "TABLIZER 1"
.TH TABLIZER 1 "2025-09-11" "1" "User Commands" .TH TABLIZER 1 "2025-09-30" "1" "User Commands"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents. .\" way too many mistakes in technical documents.
.if n .ad l .if n .ad l
@@ -152,12 +152,12 @@ tablizer \- Manipulate tabular output of other programs
\& \-n, \-\-numbering Enable header numbering \& \-n, \-\-numbering Enable header numbering
\& \-N, \-\-no\-color Disable pattern highlighting \& \-N, \-\-no\-color Disable pattern highlighting
\& \-H, \-\-no\-headers Disable headers display \& \-H, \-\-no\-headers Disable headers display
\& \-s, \-\-separator string Custom field separator \& \-s, \-\-separator <string> Custom field separator
\& \-k, \-\-sort\-by int|name Sort by column (default: 1) \& \-k, \-\-sort\-by <int|name> Sort by column (default: 1)
\& \-z, \-\-fuzzy Use fuzzy search [experimental] \& \-z, \-\-fuzzy Use fuzzy search [experimental]
\& \-F, \-\-filter field[!]=reg Filter given field with regex, can be used multiple times \& \-F, \-\-filter <field[!]=reg> Filter given field with regex, can be used multiple times
\& \-T, \-\-transpose\-columns string Transpose the speficied columns (separated by ,) \& \-T, \-\-transpose\-columns string Transpose the speficied columns (separated by ,)
\& \-R, \-\-regex\-transposer /from/to/ Apply /search/replace/ regexp to fields given in \-T \& \-R, \-\-regex\-transposer </from/to/> Apply /search/replace/ regexp to fields given in \-T
\& \-I, \-\-interactive Interactively filter and select rows \& \-I, \-\-interactive Interactively filter and select rows
\& \&
\& Output Flags (mutually exclusive): \& Output Flags (mutually exclusive):
@@ -171,6 +171,7 @@ tablizer \- Manipulate tabular output of other programs
\& \-L, \-\-hightlight\-lines Use alternating background colors for tables \& \-L, \-\-hightlight\-lines Use alternating background colors for tables
\& \-y, \-\-yank\-columns Yank specified columns (separated by ,) to clipboard, \& \-y, \-\-yank\-columns Yank specified columns (separated by ,) to clipboard,
\& space separated \& space separated
\& \-\-ofs <char> Output field separator, used by \-A and \-C.
\& \&
\& Sort Mode Flags (mutually exclusive): \& Sort Mode Flags (mutually exclusive):
\& \-a, \-\-sort\-age sort according to age (duration) string \& \-a, \-\-sort\-age sort according to age (duration) string
@@ -217,17 +218,17 @@ pattern. Hence:
\& kubectl get pods | tablizer \& kubectl get pods | tablizer
\& \&
\& # read a file \& # read a file
\& tablizer filename \& tablizer \-r filename
\& \&
\& # search for pattern in a file (works like grep) \& # search for pattern in a file (works like grep)
\& tablizer regex filename \& tablizer regex \-r filename
\& \&
\& # search for pattern in STDIN \& # search for pattern in STDIN
\& kubectl get pods | tablizer regex \& kubectl get pods | tablizer regex
.Ve .Ve
.PP .PP
The output looks like the original one but every header field will The output looks like the original one. You can add the option \fB\-n\fR,
have a numer associated with it, e.g.: then every header field will have a numer associated with it, e.g.:
.PP .PP
.Vb 1 .Vb 1
\& NAME(1) READY(2) STATUS(3) RESTARTS(4) AGE(5) \& NAME(1) READY(2) STATUS(3) RESTARTS(4) AGE(5)
@@ -243,7 +244,18 @@ columns you want to have in your output (see \s-1COLUMNS\s0:
You can specify the numbers in any order but output will always follow You can specify the numbers in any order but output will always follow
the original order. the original order.
.PP .PP
The numbering can be suppressed by using the \fB\-n\fR option. However, you may also just use the header names instead of numbers,
eg:
.PP
.Vb 1
\& kubectl get pods | tablizer \-cname,status
.Ve
.PP
You can also use regular expressions with \fB\-c\fR, eg:
.PP
.Vb 1
\& kubectl get pods | tablizer \-c \*(Aq[ae]\*(Aq
.Ve
.PP .PP
By default tablizer shows a header containing the names of each By default tablizer shows a header containing the names of each
column. This can be disabled using the \fB\-H\fR option. Be aware that column. This can be disabled using the \fB\-H\fR option. Be aware that

View File

@@ -13,12 +13,12 @@ tablizer - Manipulate tabular output of other programs
-n, --numbering Enable header numbering -n, --numbering Enable header numbering
-N, --no-color Disable pattern highlighting -N, --no-color Disable pattern highlighting
-H, --no-headers Disable headers display -H, --no-headers Disable headers display
-s, --separator string Custom field separator -s, --separator <string> Custom field separator
-k, --sort-by int|name Sort by column (default: 1) -k, --sort-by <int|name> Sort by column (default: 1)
-z, --fuzzy Use fuzzy search [experimental] -z, --fuzzy Use fuzzy search [experimental]
-F, --filter field[!]=reg Filter given field with regex, can be used multiple times -F, --filter <field[!]=reg> Filter given field with regex, can be used multiple times
-T, --transpose-columns string Transpose the speficied columns (separated by ,) -T, --transpose-columns string Transpose the speficied columns (separated by ,)
-R, --regex-transposer /from/to/ Apply /search/replace/ regexp to fields given in -T -R, --regex-transposer </from/to/> Apply /search/replace/ regexp to fields given in -T
-I, --interactive Interactively filter and select rows -I, --interactive Interactively filter and select rows
Output Flags (mutually exclusive): Output Flags (mutually exclusive):
@@ -32,6 +32,7 @@ tablizer - Manipulate tabular output of other programs
-L, --hightlight-lines Use alternating background colors for tables -L, --hightlight-lines Use alternating background colors for tables
-y, --yank-columns Yank specified columns (separated by ,) to clipboard, -y, --yank-columns Yank specified columns (separated by ,) to clipboard,
space separated space separated
--ofs <char> Output field separator, used by -A and -C.
Sort Mode Flags (mutually exclusive): Sort Mode Flags (mutually exclusive):
-a, --sort-age sort according to age (duration) string -a, --sort-age sort according to age (duration) string
@@ -78,16 +79,16 @@ pattern. Hence:
kubectl get pods | tablizer kubectl get pods | tablizer
# read a file # read a file
tablizer filename tablizer -r filename
# search for pattern in a file (works like grep) # search for pattern in a file (works like grep)
tablizer regex filename tablizer regex -r filename
# search for pattern in STDIN # search for pattern in STDIN
kubectl get pods | tablizer regex kubectl get pods | tablizer regex
The output looks like the original one but every header field will The output looks like the original one. You can add the option B<-n>,
have a numer associated with it, e.g.: then every header field will have a numer associated with it, e.g.:
NAME(1) READY(2) STATUS(3) RESTARTS(4) AGE(5) NAME(1) READY(2) STATUS(3) RESTARTS(4) AGE(5)
@@ -99,7 +100,14 @@ columns you want to have in your output (see L<COLUMNS>:
You can specify the numbers in any order but output will always follow You can specify the numbers in any order but output will always follow
the original order. the original order.
The numbering can be suppressed by using the B<-n> option. However, you may also just use the header names instead of numbers,
eg:
kubectl get pods | tablizer -cname,status
You can also use regular expressions with B<-c>, eg:
kubectl get pods | tablizer -c '[ae]'
By default tablizer shows a header containing the names of each By default tablizer shows a header containing the names of each
column. This can be disabled using the B<-H> option. Be aware that column. This can be disabled using the B<-H> option. Be aware that