diff options
Diffstat (limited to 'search/search.go')
| -rw-r--r-- | search/search.go | 66 |
1 files changed, 52 insertions, 14 deletions
diff --git a/search/search.go b/search/search.go index 5b227b2..009dd49 100644 --- a/search/search.go +++ b/search/search.go @@ -5,50 +5,70 @@ import ( "slices" ) -func Search(s *model.State, args ...string) (model.Target, *model.Source, bool, []string, []string) { +type Result struct { + Target model.Target + Source *model.Source + NeedsConfirmation bool + Overridden bool + Sub []string + Args []string +} + +// Search is the meat of pik +func Search(s *model.State, args ...string) *Result { var target model.Target - var suspect model.Target - var suspectSource *model.Source var targetSource *model.Source - var forward []string + var confirm bool + var overridden bool var subdir []string - confirm := false + var forward []string + var suspect model.Target + var suspectSource *model.Source args_loop: - for _, a := range args { + for _, arg := range args { for _, src := range s.Sources { if targetSource == nil { - if src.Is(a) { + if src.Is(arg) { targetSource = src + + // try to look for arg target with the same name as the source + // "default target" of sorts for _, t := range targetSource.Targets { - if t.Matches(a) { + if t.Matches(arg) { target = t continue args_loop } } + continue args_loop } } if target == nil && targetSource == nil { + + // uncertain about source, check ours to see if any match for _, t := range src.Targets { - if t.Matches(a) { + if t.Matches(arg) { target = t targetSource = src continue args_loop } } + } else if target == nil { // && targetSource == nil (but it is always true) + + // source located, for _, t := range targetSource.Targets { - if t.Matches(a) { + if t.Matches(arg) { target = t continue args_loop } } // if we find the right target for _, t := range src.Targets { - if t.Matches(a) { + if t.Matches(arg) { confirm = true suspect = t suspectSource = src @@ -60,10 +80,10 @@ args_loop: } if target == nil { - subdir = append(subdir, a) + subdir = append(subdir, arg) continue args_loop } else if targetSource != nil { - forward = append(forward, a) + forward = append(forward, arg) continue args_loop } } @@ -82,5 +102,23 @@ args_loop: forward = args } - return target, targetSource, confirm, subdir, forward + if target != nil && targetSource != nil { + for _, t := range targetSource.Targets { + if slices.Equal(t.Invocation(targetSource), target.Invocation(targetSource)) { + if t.Tags().Has(model.Override) { + overridden = true + target = t + } + } + } + } + + return &Result{ + Target: target, + Source: targetSource, + NeedsConfirmation: confirm, + Overridden: overridden, + Sub: subdir, + Args: forward, + } } |
