mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-17 20:41:03 +01:00
add -F filter by column flag (closes #13)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
Copyright © 2022 Thomas von Dein
|
||||
Copyright © 2022-2024 Thomas von Dein
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -24,3 +24,13 @@ type Tabdata struct {
|
||||
headers []string // [ "ID", "NAME", ...]
|
||||
entries [][]string
|
||||
}
|
||||
|
||||
func (data *Tabdata) CloneEmpty() Tabdata {
|
||||
new := Tabdata{
|
||||
maxwidthHeader: data.maxwidthHeader,
|
||||
columns: data.columns,
|
||||
headers: data.headers,
|
||||
}
|
||||
|
||||
return new
|
||||
}
|
||||
|
||||
82
lib/filter.go
Normal file
82
lib/filter.go
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
Copyright © 2022-2024 Thomas von Dein
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package lib
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/lithammer/fuzzysearch/fuzzy"
|
||||
"github.com/tlinden/tablizer/cfg"
|
||||
)
|
||||
|
||||
/*
|
||||
* [!]Match a line, use fuzzy search for normal pattern strings and
|
||||
* regexp otherwise.
|
||||
*/
|
||||
func matchPattern(c cfg.Config, line string) bool {
|
||||
if c.UseFuzzySearch {
|
||||
return fuzzy.MatchFold(c.Pattern, line)
|
||||
}
|
||||
|
||||
return c.PatternR.MatchString(line)
|
||||
}
|
||||
|
||||
/*
|
||||
* Filter parsed data by fields. The filter is positive, so if one or
|
||||
* more filters match on a row, it will be kept, otherwise it will be
|
||||
* excluded.
|
||||
*/
|
||||
func FilterByFields(conf cfg.Config, data Tabdata) (Tabdata, bool, error) {
|
||||
if len(conf.Filters) == 0 {
|
||||
// no filters, no checking
|
||||
return Tabdata{}, false, nil
|
||||
}
|
||||
|
||||
newdata := data.CloneEmpty()
|
||||
|
||||
for _, row := range data.entries {
|
||||
keep := true
|
||||
|
||||
for idx, header := range data.headers {
|
||||
if !Exists(conf.Filters, strings.ToLower(header)) {
|
||||
// do not filter by unspecified field
|
||||
continue
|
||||
}
|
||||
|
||||
if !conf.Filters[strings.ToLower(header)].MatchString(row[idx]) {
|
||||
// there IS a filter, but it doesn't match
|
||||
keep = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if keep == !conf.InvertMatch {
|
||||
// also apply -v
|
||||
newdata.entries = append(newdata.entries, row)
|
||||
}
|
||||
}
|
||||
|
||||
return newdata, true, nil
|
||||
}
|
||||
|
||||
func Exists[K comparable, V any](m map[K]V, v K) bool {
|
||||
if _, ok := m[v]; ok {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
164
lib/filter_test.go
Normal file
164
lib/filter_test.go
Normal file
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
Copyright © 2024 Thomas von Dein
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package lib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/tlinden/tablizer/cfg"
|
||||
)
|
||||
|
||||
func TestMatchPattern(t *testing.T) {
|
||||
var input = []struct {
|
||||
name string
|
||||
fuzzy bool
|
||||
pattern string
|
||||
line string
|
||||
}{
|
||||
{
|
||||
name: "normal",
|
||||
pattern: "haus",
|
||||
line: "hausparty",
|
||||
},
|
||||
{
|
||||
name: "fuzzy",
|
||||
pattern: "hpt",
|
||||
line: "haus-party-termin",
|
||||
fuzzy: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, in := range input {
|
||||
testname := fmt.Sprintf("match-pattern-%s", in.name)
|
||||
|
||||
t.Run(testname, func(t *testing.T) {
|
||||
c := cfg.Config{}
|
||||
|
||||
if in.fuzzy {
|
||||
c.UseFuzzySearch = true
|
||||
}
|
||||
|
||||
err := c.PreparePattern(in.pattern)
|
||||
if err != nil {
|
||||
t.Errorf("PreparePattern returned error: %s", err)
|
||||
}
|
||||
|
||||
if !matchPattern(c, in.line) {
|
||||
t.Errorf("matchPattern() did not match\nExp: true\nGot: false\n")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestFilterByFields(t *testing.T) {
|
||||
data := Tabdata{
|
||||
headers: []string{
|
||||
"ONE", "TWO", "THREE",
|
||||
},
|
||||
entries: [][]string{
|
||||
{"asd", "igig", "cxxxncnc"},
|
||||
{"19191", "EDD 1", "x"},
|
||||
{"8d8", "AN 1", "y"},
|
||||
},
|
||||
}
|
||||
|
||||
var input = []struct {
|
||||
name string
|
||||
filter []string
|
||||
expect Tabdata
|
||||
invert bool
|
||||
}{
|
||||
{
|
||||
name: "one-field",
|
||||
filter: []string{"one=19"},
|
||||
expect: Tabdata{
|
||||
headers: []string{
|
||||
"ONE", "TWO", "THREE",
|
||||
},
|
||||
entries: [][]string{
|
||||
{"19191", "EDD 1", "x"},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
name: "one-field-inverted",
|
||||
filter: []string{"one=19"},
|
||||
invert: true,
|
||||
expect: Tabdata{
|
||||
headers: []string{
|
||||
"ONE", "TWO", "THREE",
|
||||
},
|
||||
entries: [][]string{
|
||||
{"asd", "igig", "cxxxncnc"},
|
||||
{"8d8", "AN 1", "y"},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
name: "many-fields",
|
||||
filter: []string{"one=19", "two=DD"},
|
||||
expect: Tabdata{
|
||||
headers: []string{
|
||||
"ONE", "TWO", "THREE",
|
||||
},
|
||||
entries: [][]string{
|
||||
{"19191", "EDD 1", "x"},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
name: "many-fields-inverted",
|
||||
filter: []string{"one=19", "two=DD"},
|
||||
invert: true,
|
||||
expect: Tabdata{
|
||||
headers: []string{
|
||||
"ONE", "TWO", "THREE",
|
||||
},
|
||||
entries: [][]string{
|
||||
{"asd", "igig", "cxxxncnc"},
|
||||
{"8d8", "AN 1", "y"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, in := range input {
|
||||
testname := fmt.Sprintf("filter-by-fields-%s", in.name)
|
||||
|
||||
t.Run(testname, func(t *testing.T) {
|
||||
c := cfg.Config{Rawfilters: in.filter, InvertMatch: in.invert}
|
||||
|
||||
err := c.PrepareFilters()
|
||||
if err != nil {
|
||||
t.Errorf("PrepareFilters returned error: %s", err)
|
||||
}
|
||||
|
||||
data, _, _ := FilterByFields(c, data)
|
||||
if !reflect.DeepEqual(data, in.expect) {
|
||||
t.Errorf("Filtered data does not match expected data:\ngot: %+v\nexp: %+v", data, in.expect)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user