From c6a362a039b1662dfc0badeb8badc4458e446787 Mon Sep 17 00:00:00 2001 From: ewy Date: Wed, 22 Apr 2026 19:47:29 +0200 Subject: work out the "default" behaviour a little better --- indexers/pikdex/index.go | 2 ++ search/search.go | 21 +++++++++++++++++---- search/search_test.go | 21 +++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/indexers/pikdex/index.go b/indexers/pikdex/index.go index 9a54502..aec6b00 100644 --- a/indexers/pikdex/index.go +++ b/indexers/pikdex/index.go @@ -73,7 +73,9 @@ func (u *pikdex) Index(absPath string, f fs.FS, runners []model.Runner) ([]model return nil, err } var targets []model.Target + u.Lock() mod := u.mods[absPath] + u.Unlock() if mod == nil { u.mods[absPath] = &SourceData{ Path: absPath, diff --git a/search/search.go b/search/search.go index 009dd49..767473d 100644 --- a/search/search.go +++ b/search/search.go @@ -26,13 +26,18 @@ func Search(s *model.State, args ...string) *Result { var suspectSource *model.Source args_loop: - for _, arg := range args { + for i, arg := range args { for _, src := range s.Sources { if targetSource == nil { if src.Is(arg) { targetSource = src + // only try to find the default target if this is the last argument + if len(args)-1 != i { + continue args_loop + } + // try to look for arg target with the same name as the source // "default target" of sorts for _, t := range targetSource.Targets { @@ -51,8 +56,13 @@ args_loop: // uncertain about source, check ours to see if any match for _, t := range src.Targets { if t.Matches(arg) { - target = t - targetSource = src + if slices.Equal(t.Sub(), subdir) { + target = t + targetSource = src + } else { + suspect = t + suspectSource = src + } continue args_loop } } @@ -91,7 +101,10 @@ args_loop: if suspect != nil && target == nil { target = suspect targetSource = suspectSource - confirm = true + + if !(suspect.Sub() != nil && subdir == nil) { + confirm = true + } } if target != nil && target.Sub() != nil && subdir != nil && !slices.Equal(target.Sub(), subdir) { diff --git a/search/search_test.go b/search/search_test.go index 5eaa1b9..3671f75 100644 --- a/search/search_test.go +++ b/search/search_test.go @@ -133,3 +133,24 @@ func TestSearch_SourceDefault(t *testing.T) { assert.Equal(t, res.Target, src.Targets[0]) assert.False(t, res.NeedsConfirmation) } + +func TestSearch_SourceDefault_Other(t *testing.T) { + src := testx.TSource("src", "src", "other") + st := testx.TState(src) + res := Search(st, "src", "other") + assert.Nil(t, res.Args) + assert.Equal(t, res.Target, src.Targets[1]) + assert.False(t, res.NeedsConfirmation) +} + +func TestSearch_SubdirDefault_Other(t *testing.T) { + tgt := testx.TTarget("subname", "subname") + other := testx.TTarget("othername", "subname") + src := testx.TSource("src") + src.Targets = append(src.Targets, tgt, other) + st := testx.TState(src) + res := Search(st, "subname", "othername") + assert.Nil(t, res.Args) + assert.Equal(t, other, res.Target) + assert.False(t, res.NeedsConfirmation) +} -- cgit v1.3.1