Merge pull request #47 from TLINDEN/headernumbers

reverse the meaning of -n
This commit is contained in:
T.v.Dein
2025-03-06 17:11:00 +01:00
committed by GitHub
8 changed files with 75 additions and 61 deletions

View File

@@ -28,7 +28,7 @@ import (
) )
const DefaultSeparator string = `(\s\s+|\t)` const DefaultSeparator string = `(\s\s+|\t)`
const Version string = "v1.3.3" const Version string = "v1.4.0"
const MAXPARTS = 2 const MAXPARTS = 2
var DefaultConfigfile = os.Getenv("HOME") + "/.config/tablizer/config" var DefaultConfigfile = os.Getenv("HOME") + "/.config/tablizer/config"
@@ -66,7 +66,7 @@ type Filter struct {
// internal config // internal config
type Config struct { type Config struct {
Debug bool Debug bool
NoNumbering bool Numbering bool
NoHeaders bool NoHeaders bool
Columns string Columns string
UseColumns []int UseColumns []int
@@ -332,10 +332,10 @@ func (conf *Config) PrepareTransposers() error {
func (conf *Config) CheckEnv() { func (conf *Config) CheckEnv() {
// check for environment vars, command line flags have precedence, // check for environment vars, command line flags have precedence,
// NO_COLOR is being checked by the color module itself. // NO_COLOR is being checked by the color module itself.
if !conf.NoNumbering { if !conf.Numbering {
_, set := os.LookupEnv("T_NO_HEADER_NUMBERING") _, set := os.LookupEnv("T_HEADER_NUMBERING")
if set { if set {
conf.NoNumbering = true conf.Numbering = true
} }
} }
@@ -350,7 +350,7 @@ func (conf *Config) CheckEnv() {
func (conf *Config) ApplyDefaults() { func (conf *Config) ApplyDefaults() {
// mode specific defaults // mode specific defaults
if conf.OutputMode == Yaml || conf.OutputMode == CSV { if conf.OutputMode == Yaml || conf.OutputMode == CSV {
conf.NoNumbering = true conf.Numbering = false
} }
} }

View File

@@ -125,7 +125,7 @@ func Execute() {
// options // options
rootCmd.PersistentFlags().BoolVarP(&conf.Debug, "debug", "d", false, rootCmd.PersistentFlags().BoolVarP(&conf.Debug, "debug", "d", false,
"Enable debugging") "Enable debugging")
rootCmd.PersistentFlags().BoolVarP(&conf.NoNumbering, "no-numbering", "n", false, rootCmd.PersistentFlags().BoolVarP(&conf.Numbering, "numbering", "n", false,
"Disable header numbering") "Disable header numbering")
rootCmd.PersistentFlags().BoolVarP(&conf.NoHeaders, "no-headers", "H", false, rootCmd.PersistentFlags().BoolVarP(&conf.NoHeaders, "no-headers", "H", false,
"Disable header display") "Disable header display")

View File

@@ -11,7 +11,7 @@ SYNOPSIS
Operational Flags: Operational Flags:
-c, --columns string Only show the speficied columns (separated by ,) -c, --columns string Only show the speficied columns (separated by ,)
-v, --invert-match select non-matching rows -v, --invert-match select non-matching rows
-n, --no-numbering Disable 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
@@ -303,7 +303,7 @@ DESCRIPTION
influence program behavior. Commandline flags have always precedence influence program behavior. Commandline flags have always precedence
over environment variables. over environment variables.
<T_NO_HEADER_NUMBERING> - disable numbering of header fields, like -n. <T_HEADER_NUMBERING> - enable numbering of header fields, like -n.
<T_COLUMNS> - comma separated list of columns to output, like -c <T_COLUMNS> - comma separated list of columns to output, like -c
<NO_COLORS> - disable colorization of matches, like -N <NO_COLORS> - disable colorization of matches, like -N
@@ -428,7 +428,7 @@ Usage:
Operational Flags: Operational Flags:
-c, --columns string Only show the speficied columns (separated by ,) -c, --columns string Only show the speficied columns (separated by ,)
-v, --invert-match select non-matching rows -v, --invert-match select non-matching rows
-n, --no-numbering Disable 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

View File

@@ -208,13 +208,13 @@ func numberizeAndReduceHeaders(conf cfg.Config, data *Tabdata) {
} }
} }
if conf.NoNumbering { if conf.Numbering {
numberedHeaders = append(numberedHeaders, head)
headlen = len(head)
} else {
numhead := fmt.Sprintf("%s(%d)", head, idx+1) numhead := fmt.Sprintf("%s(%d)", head, idx+1)
headlen = len(numhead) headlen = len(numhead)
numberedHeaders = append(numberedHeaders, numhead) numberedHeaders = append(numberedHeaders, numhead)
} else {
numberedHeaders = append(numberedHeaders, head)
headlen = len(head)
} }
if headlen > maxwidth { if headlen > maxwidth {

View File

@@ -216,21 +216,21 @@ func TestNumberizeHeaders(t *testing.T) {
} }
var tests = []struct { var tests = []struct {
expect []string expect []string
columns []int columns []int
nonum bool numberize bool
}{ }{
{[]string{"ONE(1)", "TWO(2)", "THREE(3)"}, []int{1, 2, 3}, false}, {[]string{"ONE(1)", "TWO(2)", "THREE(3)"}, []int{1, 2, 3}, true},
{[]string{"ONE(1)", "TWO(2)"}, []int{1, 2}, false}, {[]string{"ONE(1)", "TWO(2)"}, []int{1, 2}, true},
{[]string{"ONE", "TWO"}, []int{1, 2}, true}, {[]string{"ONE", "TWO"}, []int{1, 2}, false},
} }
for _, testdata := range tests { for _, testdata := range tests {
testname := fmt.Sprintf("numberize-headers-columns-%+v-nonum-%t", testname := fmt.Sprintf("numberize-headers-columns-%+v-nonum-%t",
testdata.columns, testdata.nonum) testdata.columns, testdata.numberize)
t.Run(testname, func(t *testing.T) { t.Run(testname, func(t *testing.T) {
conf := cfg.Config{Columns: "x", UseColumns: testdata.columns, NoNumbering: testdata.nonum} conf := cfg.Config{Columns: "x", UseColumns: testdata.columns, Numbering: testdata.numberize}
usedata := data usedata := data
numberizeAndReduceHeaders(conf, &usedata) numberizeAndReduceHeaders(conf, &usedata)
if !reflect.DeepEqual(usedata.headers, testdata.expect) { if !reflect.DeepEqual(usedata.headers, testdata.expect) {

View File

@@ -65,7 +65,7 @@ var tests = []struct {
sortby string // empty == default sortby string // empty == default
column int // sort by this column (numbers start by 1) column int // sort by this column (numbers start by 1)
desc bool // sort in descending order, default == ascending desc bool // sort in descending order, default == ascending
nonum bool // hide numbering numberize bool // add header numbering
mode int // shell, orgtbl, etc. empty == default: ascii mode int // shell, orgtbl, etc. empty == default: ascii
usecol []int // columns to display, empty == display all usecol []int // columns to display, empty == display all
usecolstr string // for testname, must match usecol usecolstr string // for testname, must match usecol
@@ -73,8 +73,9 @@ var tests = []struct {
}{ }{
// --------------------- Default settings mode tests `` // --------------------- Default settings mode tests ``
{ {
mode: cfg.ASCII, mode: cfg.ASCII,
name: "default", numberize: true,
name: "default",
expect: ` expect: `
NAME(1) DURATION(2) COUNT(3) WHEN(4) NAME(1) DURATION(2) COUNT(3) WHEN(4)
beta 1d10h5m1s 33 3/1/2014 beta 1d10h5m1s 33 3/1/2014
@@ -82,8 +83,9 @@ alpha 4h35m 170 2013-Feb-03
ceta 33d12h 9 06/Jan/2008 15:04:05 -0700`, ceta 33d12h 9 06/Jan/2008 15:04:05 -0700`,
}, },
{ {
mode: cfg.CSV, mode: cfg.CSV,
name: "csv", numberize: false,
name: "csv",
expect: ` expect: `
NAME,DURATION,COUNT,WHEN NAME,DURATION,COUNT,WHEN
beta,1d10h5m1s,33,3/1/2014 beta,1d10h5m1s,33,3/1/2014
@@ -91,8 +93,9 @@ alpha,4h35m,170,2013-Feb-03
ceta,33d12h,9,06/Jan/2008 15:04:05 -0700`, ceta,33d12h,9,06/Jan/2008 15:04:05 -0700`,
}, },
{ {
name: "default", name: "orgtbl",
mode: cfg.Orgtbl, numberize: true,
mode: cfg.Orgtbl,
expect: ` expect: `
+---------+-------------+----------+----------------------------+ +---------+-------------+----------+----------------------------+
| NAME(1) | DURATION(2) | COUNT(3) | WHEN(4) | | NAME(1) | DURATION(2) | COUNT(3) | WHEN(4) |
@@ -103,8 +106,9 @@ ceta,33d12h,9,06/Jan/2008 15:04:05 -0700`,
+---------+-------------+----------+----------------------------+`, +---------+-------------+----------+----------------------------+`,
}, },
{ {
name: "default", name: "markdown",
mode: cfg.Markdown, mode: cfg.Markdown,
numberize: true,
expect: ` expect: `
| NAME(1) | DURATION(2) | COUNT(3) | WHEN(4) | | NAME(1) | DURATION(2) | COUNT(3) | WHEN(4) |
|---------|-------------|----------|----------------------------| |---------|-------------|----------|----------------------------|
@@ -113,18 +117,18 @@ ceta,33d12h,9,06/Jan/2008 15:04:05 -0700`,
| ceta | 33d12h | 9 | 06/Jan/2008 15:04:05 -0700 |`, | ceta | 33d12h | 9 | 06/Jan/2008 15:04:05 -0700 |`,
}, },
{ {
name: "default", name: "shell",
mode: cfg.Shell, mode: cfg.Shell,
nonum: true, numberize: false,
expect: ` expect: `
NAME="beta" DURATION="1d10h5m1s" COUNT="33" WHEN="3/1/2014" NAME="beta" DURATION="1d10h5m1s" COUNT="33" WHEN="3/1/2014"
NAME="alpha" DURATION="4h35m" COUNT="170" WHEN="2013-Feb-03" NAME="alpha" DURATION="4h35m" COUNT="170" WHEN="2013-Feb-03"
NAME="ceta" DURATION="33d12h" COUNT="9" WHEN="06/Jan/2008 15:04:05 -0700"`, NAME="ceta" DURATION="33d12h" COUNT="9" WHEN="06/Jan/2008 15:04:05 -0700"`,
}, },
{ {
name: "default", name: "yaml",
mode: cfg.Yaml, mode: cfg.Yaml,
nonum: true, numberize: false,
expect: ` expect: `
entries: entries:
- count: 33 - count: 33
@@ -141,8 +145,9 @@ entries:
when: "06/Jan/2008 15:04:05 -0700"`, when: "06/Jan/2008 15:04:05 -0700"`,
}, },
{ {
name: "default", name: "extended",
mode: cfg.Extended, mode: cfg.Extended,
numberize: true,
expect: ` expect: `
NAME(1): beta NAME(1): beta
DURATION(2): 1d10h5m1s DURATION(2): 1d10h5m1s
@@ -162,10 +167,11 @@ DURATION(2): 33d12h
//------------------------ SORT TESTS //------------------------ SORT TESTS
{ {
name: "sortbycolumn3", name: "sortbycolumn3",
column: 3, column: 3,
sortby: "numeric", sortby: "numeric",
desc: false, numberize: true,
desc: false,
expect: ` expect: `
NAME(1) DURATION(2) COUNT(3) WHEN(4) NAME(1) DURATION(2) COUNT(3) WHEN(4)
ceta 33d12h 9 06/Jan/2008 15:04:05 -0700 ceta 33d12h 9 06/Jan/2008 15:04:05 -0700
@@ -173,10 +179,11 @@ beta 1d10h5m1s 33 3/1/2014
alpha 4h35m 170 2013-Feb-03`, alpha 4h35m 170 2013-Feb-03`,
}, },
{ {
name: "sortbycolumn4", name: "sortbycolumn4",
column: 4, column: 4,
sortby: "time", sortby: "time",
desc: false, desc: false,
numberize: true,
expect: ` expect: `
NAME(1) DURATION(2) COUNT(3) WHEN(4) NAME(1) DURATION(2) COUNT(3) WHEN(4)
ceta 33d12h 9 06/Jan/2008 15:04:05 -0700 ceta 33d12h 9 06/Jan/2008 15:04:05 -0700
@@ -184,10 +191,11 @@ alpha 4h35m 170 2013-Feb-03
beta 1d10h5m1s 33 3/1/2014`, beta 1d10h5m1s 33 3/1/2014`,
}, },
{ {
name: "sortbycolumn2", name: "sortbycolumn2",
column: 2, column: 2,
sortby: "duration", sortby: "duration",
desc: false, numberize: true,
desc: false,
expect: ` expect: `
NAME(1) DURATION(2) COUNT(3) WHEN(4) NAME(1) DURATION(2) COUNT(3) WHEN(4)
alpha 4h35m 170 2013-Feb-03 alpha 4h35m 170 2013-Feb-03
@@ -199,6 +207,7 @@ ceta 33d12h 9 06/Jan/2008 15:04:05 -0700`,
{ {
name: "usecolumns", name: "usecolumns",
usecol: []int{1, 4}, usecol: []int{1, 4},
numberize: true,
usecolstr: "1,4", usecolstr: "1,4",
expect: ` expect: `
NAME(1) WHEN(4) NAME(1) WHEN(4)
@@ -209,6 +218,7 @@ ceta 06/Jan/2008 15:04:05 -0700`,
{ {
name: "usecolumns", name: "usecolumns",
usecol: []int{2}, usecol: []int{2},
numberize: true,
usecolstr: "2", usecolstr: "2",
expect: ` expect: `
DURATION(2) DURATION(2)
@@ -219,6 +229,7 @@ DURATION(2)
{ {
name: "usecolumns", name: "usecolumns",
usecol: []int{3}, usecol: []int{3},
numberize: true,
usecolstr: "3", usecolstr: "3",
expect: ` expect: `
COUNT(3) COUNT(3)
@@ -230,6 +241,7 @@ COUNT(3)
name: "usecolumns", name: "usecolumns",
column: 0, column: 0,
usecol: []int{1, 3}, usecol: []int{1, 3},
numberize: true,
usecolstr: "1,3", usecolstr: "1,3",
expect: ` expect: `
NAME(1) COUNT(3) NAME(1) COUNT(3)
@@ -240,6 +252,7 @@ ceta 9`,
{ {
name: "usecolumns", name: "usecolumns",
usecol: []int{2, 4}, usecol: []int{2, 4},
numberize: true,
usecolstr: "2,4", usecolstr: "2,4",
expect: ` expect: `
DURATION(2) WHEN(4) DURATION(2) WHEN(4)
@@ -251,8 +264,10 @@ DURATION(2) WHEN(4)
func TestPrinter(t *testing.T) { func TestPrinter(t *testing.T) {
for _, testdata := range tests { for _, testdata := range tests {
testname := fmt.Sprintf("print-%s-%d-desc-%t-sortby-%s-mode-%d-usecolumns-%s", testname := fmt.Sprintf("print-%s-%d-desc-%t-sortby-%s-mode-%d-usecolumns-%s-numberize-%t",
testdata.name, testdata.column, testdata.desc, testdata.sortby, testdata.mode, testdata.usecolstr) testdata.name, testdata.column, testdata.desc, testdata.sortby,
testdata.mode, testdata.usecolstr, testdata.numberize)
t.Run(testname, func(t *testing.T) { t.Run(testname, func(t *testing.T) {
// replaces os.Stdout, but we ignore it // replaces os.Stdout, but we ignore it
var writer bytes.Buffer var writer bytes.Buffer
@@ -262,7 +277,7 @@ func TestPrinter(t *testing.T) {
SortDescending: testdata.desc, SortDescending: testdata.desc,
SortMode: testdata.sortby, SortMode: testdata.sortby,
OutputMode: testdata.mode, OutputMode: testdata.mode,
NoNumbering: testdata.nonum, Numbering: testdata.numberize,
UseColumns: testdata.usecol, UseColumns: testdata.usecol,
NoColor: true, NoColor: true,
} }

View File

@@ -133,8 +133,7 @@
.\" ======================================================================== .\" ========================================================================
.\" .\"
.IX Title "TABLIZER 1" .IX Title "TABLIZER 1"
.TH TABLIZER 1 "2025-02-23" "1" "User Commands" .TH TABLIZER 1 "2025-03-06" "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
@@ -150,7 +149,7 @@ tablizer \- Manipulate tabular output of other programs
\& Operational Flags: \& Operational Flags:
\& \-c, \-\-columns string Only show the speficied columns (separated by ,) \& \-c, \-\-columns string Only show the speficied columns (separated by ,)
\& \-v, \-\-invert\-match select non\-matching rows \& \-v, \-\-invert\-match select non\-matching rows
\& \-n, \-\-no\-numbering Disable 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
@@ -484,8 +483,8 @@ separated by one space.
\&\fBtablizer\fR supports certain environment variables which use can use \&\fBtablizer\fR supports certain environment variables which use can use
to influence program behavior. Commandline flags have always to influence program behavior. Commandline flags have always
precedence over environment variables. precedence over environment variables.
.IP "<T_NO_HEADER_NUMBERING> \- disable numbering of header fields, like \fB\-n\fR." 4 .IP "<T_HEADER_NUMBERING> \- enable numbering of header fields, like \fB\-n\fR." 4
.IX Item "<T_NO_HEADER_NUMBERING> - disable numbering of header fields, like -n." .IX Item "<T_HEADER_NUMBERING> - enable numbering of header fields, like -n."
.PD 0 .PD 0
.IP "<T_COLUMNS> \- comma separated list of columns to output, like \fB\-c\fR" 4 .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" .IX Item "<T_COLUMNS> - comma separated list of columns to output, like -c"

View File

@@ -10,7 +10,7 @@ tablizer - Manipulate tabular output of other programs
Operational Flags: Operational Flags:
-c, --columns string Only show the speficied columns (separated by ,) -c, --columns string Only show the speficied columns (separated by ,)
-v, --invert-match select non-matching rows -v, --invert-match select non-matching rows
-n, --no-numbering Disable 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
@@ -329,7 +329,7 @@ precedence over environment variables.
=over =over
=item <T_NO_HEADER_NUMBERING> - disable numbering of header fields, like B<-n>. =item <T_HEADER_NUMBERING> - enable numbering of header fields, like B<-n>.
=item <T_COLUMNS> - comma separated list of columns to output, like B<-c> =item <T_COLUMNS> - comma separated list of columns to output, like B<-c>