From aae1ca745615e20d0aaa498f1e49b58ea7621557 Mon Sep 17 00:00:00 2001 From: Arnas Udovic Date: Fri, 9 Aug 2024 12:20:34 +0300 Subject: [PATCH] support exclude filter --- README.md | 2 +- tree.go | 25 ++++++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ad43441..7bb0fad 100644 --- a/README.md +++ b/README.md @@ -67,5 +67,5 @@ There are two main commands: `PopulateTree` to prepare source (format, add metad Also both commands use `Config` object that can be prepared by `NewConfig` command. With it can be configurable readable formats, attachment directory, custom meta and excludes (not movable files by regexp). `Tree` object has methods: `FileById` to get `File` by Id, `Slice` to get sub-tree of given path and `Filter` to filter tree by filter. -Filter contains array of meta key and searching value. `tag` key is searched as equal and other meta values of keys can contain part. +Filter contains array of meta key and searching value. `tag` key is searched as equal and other meta values of keys can contain part. If `-` is before key - filter is filter out. diff --git a/tree.go b/tree.go index 7f815aa..b9caae3 100644 --- a/tree.go +++ b/tree.go @@ -79,9 +79,16 @@ func (t Tree) Filter(filter map[string][]string) (Tree, bool) { for _, f := range t.Files { addFile := false + exclude := false for option, values := range filter { + negative := false + if string(option[0]) == "-" { + negative = true + option = option[1:] + } + for _, value := range values { - if option == "tag" { + if !negative && option == "tag" { for _, tag := range f.Tags { if tag == value { addFile = true @@ -89,12 +96,24 @@ func (t Tree) Filter(filter map[string][]string) (Tree, bool) { } continue } - if strings.Contains(f.Meta[option], value) { + if negative && option == "tag" { + for _, tag := range f.Tags { + if tag == value { + exclude = true + } + } + continue + } + if !negative && strings.Contains(f.Meta[option], value) { addFile = true + continue + } + if negative && strings.Contains(f.Meta[option], value) { + exclude = true } } } - if addFile { + if addFile && !exclude { found = true filtered.Files = append(filtered.Files, f) }