Skip to content

Commit

Permalink
fix #32: treat header filters case insensitively
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas von Dein committed Jan 14, 2025
1 parent cf5ee09 commit 1906c3e
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (conf *Config) PrepareFilters() error {
parts[0], err)
}

conf.Filters[strings.ToLower(parts[0])] = reg
conf.Filters[strings.ToLower(strings.ToLower(parts[0]))] = reg
}

return nil
Expand Down
23 changes: 23 additions & 0 deletions cmd/tablizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ DESCRIPTION
where "C" is our regexp which matches CMD.
If a column specifier doesn't look like a regular expression, matching
against header fields will be case insensitive. So, if you have a field
with the name "ID" then these will all match: "-c id", "-c Id". The same
rule applies to the options "-T" and "-F".
TRANSPOSE FIELDS USING REGEXPS
You can manipulate field contents using regular expressions. You have to
tell tablizer which field[s] to operate on using the option "-T" and the
Expand All @@ -203,6 +208,24 @@ DESCRIPTION
|search-regexp|replace-string|
Example:
cat t/testtable2
NAME DURATION
x 10
a 100
z 0
u 4
k 6
cat t/testtable2 | tablizer -T2 -R '/^\d/4/' -n
NAME DURATION
x 40
a 400
z 4
u 4
k 4
OUTPUT MODES
There might be cases when the tabular output of a program is way too
large for your current terminal but you still need to see every column.
Expand Down
23 changes: 16 additions & 7 deletions lib/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,33 @@ func PrepareColumnVars(columns string, data *Tabdata) ([]int, error) {

usecolumns := []int{}

for _, use := range strings.Split(columns, ",") {
if len(use) == 0 {
isregex := regexp.MustCompile(`\W`)

for _, columnpattern := range strings.Split(columns, ",") {
if len(columnpattern) == 0 {
return nil, fmt.Errorf("could not parse columns list %s: empty column", columns)
}

usenum, err := strconv.Atoi(use)
usenum, err := strconv.Atoi(columnpattern)
if err != nil {
// might be a regexp
colPattern, err := regexp.Compile(use)
// not a number

if !isregex.MatchString(columnpattern) {
// is not a regexp (contains no non-word chars)
// lc() it so that word searches are case insensitive
columnpattern = strings.ToLower(columnpattern)
}

colPattern, err := regexp.Compile(columnpattern)
if err != nil {
msg := fmt.Sprintf("Could not parse columns list %s: %v", columns, err)

return nil, errors.New(msg)
}

// find matching header fields
// find matching header fields, ignoring case
for i, head := range data.headers {
if colPattern.MatchString(head) {
if colPattern.MatchString(strings.ToLower(head)) {
usecolumns = append(usecolumns, i+1)
}
}
Expand Down
3 changes: 0 additions & 3 deletions lib/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ func parseTabular(conf cfg.Config, input io.Reader) (Tabdata, error) {
if !hadFirst {
// header processing
data.columns = len(parts)
// if Debug {
// fmt.Println(parts)
// }

// process all header fields
for _, part := range parts {
Expand Down
27 changes: 26 additions & 1 deletion tablizer.1
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
.\" ========================================================================
.\"
.IX Title "TABLIZER 1"
.TH TABLIZER 1 "2025-01-13" "1" "User Commands"
.TH TABLIZER 1 "2025-01-14" "1" "User Commands"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
Expand Down Expand Up @@ -351,6 +351,11 @@ We want to see only the \s-1CMD\s0 column and use a regex for this:
.Ve
.PP
where \*(L"C\*(R" is our regexp which matches \s-1CMD.\s0
.PP
If a column specifier doesn't look like a regular expression, matching
against header fields will be case insensitive. So, if you have a
field with the name \f(CW\*(C`ID\*(C'\fR then these will all match: \f(CW\*(C`\-c id\*(C'\fR, \f(CW\*(C`\-c
Id\*(C'\fR. The same rule applies to the options \f(CW\*(C`\-T\*(C'\fR and \f(CW\*(C`\-F\*(C'\fR.
.SS "\s-1TRANSPOSE FIELDS USING REGEXPS\s0"
.IX Subsection "TRANSPOSE FIELDS USING REGEXPS"
You can manipulate field contents using regular expressions. You have
Expand All @@ -370,6 +375,26 @@ use a regexp containing the \f(CW\*(C`/\*(C'\fR character, eg:
.Vb 1
\& |search\-regexp|replace\-string|
.Ve
.PP
Example:
.PP
.Vb 7
\& cat t/testtable2
\& NAME DURATION
\& x 10
\& a 100
\& z 0
\& u 4
\& k 6
\&
\& cat t/testtable2 | tablizer \-T2 \-R \*(Aq/^\ed/4/\*(Aq \-n
\& NAME DURATION
\& x 40
\& a 400
\& z 4
\& u 4
\& k 4
.Ve
.SS "\s-1OUTPUT MODES\s0"
.IX Subsection "OUTPUT MODES"
There might be cases when the tabular output of a program is way too
Expand Down
6 changes: 6 additions & 0 deletions tablizer.pod
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ We want to see only the CMD column and use a regex for this:

where "C" is our regexp which matches CMD.

If a column specifier doesn't look like a regular expression, matching
against header fields will be case insensitive. So, if you have a
field with the name C<ID> then these will all match: C<-c id>, C<-c
Id>. The same rule applies to the options C<-T> and C<-F>.


=head2 TRANSPOSE FIELDS USING REGEXPS

You can manipulate field contents using regular expressions. You have
Expand Down

0 comments on commit 1906c3e

Please sign in to comment.