summaryrefslogtreecommitdiff
path: root/search
diff options
context:
space:
mode:
authorewy <ewy0@protonmail.com>2026-04-22 19:47:29 +0200
committerewy <ewy0@protonmail.com>2026-04-22 19:47:29 +0200
commitc6a362a039b1662dfc0badeb8badc4458e446787 (patch)
tree40ed4abc183e65d8341d47454b81036200173406 /search
parent37900eae9d3c00a9c93a6ef2816b879f6c6f072b (diff)
work out the "default" behaviour a little better
Diffstat (limited to 'search')
-rw-r--r--search/search.go21
-rw-r--r--search/search_test.go21
2 files changed, 38 insertions, 4 deletions
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)
+}