summaryrefslogtreecommitdiff
path: root/search/search.go
diff options
context:
space:
mode:
authorewy <ewy0@protonmail.com>2026-04-14 16:37:17 +0200
committerewy <ewy0@protonmail.com>2026-04-14 16:37:17 +0200
commit45a297a8e526094e8fce6e2c5c0fd89b381d1765 (patch)
tree852ebc3a0112c94dc9726d0b27ab057bf6383660 /search/search.go
i have to commit at some point!
Diffstat (limited to 'search/search.go')
-rw-r--r--search/search.go86
1 files changed, 86 insertions, 0 deletions
diff --git a/search/search.go b/search/search.go
new file mode 100644
index 0000000..5b227b2
--- /dev/null
+++ b/search/search.go
@@ -0,0 +1,86 @@
+package search
+
+import (
+ "pik/model"
+ "slices"
+)
+
+func Search(s *model.State, args ...string) (model.Target, *model.Source, bool, []string, []string) {
+ var target model.Target
+ var suspect model.Target
+ var suspectSource *model.Source
+ var targetSource *model.Source
+ var forward []string
+ var subdir []string
+ confirm := false
+
+args_loop:
+ for _, a := range args {
+ for _, src := range s.Sources {
+
+ if targetSource == nil {
+ if src.Is(a) {
+ targetSource = src
+ for _, t := range targetSource.Targets {
+ if t.Matches(a) {
+ target = t
+ continue args_loop
+ }
+ }
+ continue args_loop
+ }
+ }
+
+ if target == nil && targetSource == nil {
+ for _, t := range src.Targets {
+ if t.Matches(a) {
+ target = t
+ targetSource = src
+ continue args_loop
+ }
+ }
+ } else if target == nil { // && targetSource == nil (but it is always true)
+ for _, t := range targetSource.Targets {
+ if t.Matches(a) {
+ target = t
+ continue args_loop
+ }
+ }
+ // if we find the right target
+ for _, t := range src.Targets {
+ if t.Matches(a) {
+ confirm = true
+ suspect = t
+ suspectSource = src
+ continue args_loop
+ }
+ }
+ }
+
+ }
+
+ if target == nil {
+ subdir = append(subdir, a)
+ continue args_loop
+ } else if targetSource != nil {
+ forward = append(forward, a)
+ continue args_loop
+ }
+ }
+
+ if suspect != nil && target == nil {
+ target = suspect
+ targetSource = suspectSource
+ confirm = true
+ }
+
+ if target != nil && target.Sub() != nil && subdir != nil && !slices.Equal(target.Sub(), subdir) {
+ confirm = true
+ }
+
+ if target == nil {
+ forward = args
+ }
+
+ return target, targetSource, confirm, subdir, forward
+}