Skip to content

Commit 9572b5c

Browse files
authored
Merge pull request #13 from Sneagan/feature/fuzzy-match
Partial Show Name Match
2 parents cd128b6 + 0a5e233 commit 9572b5c

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

cmd/root.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"os"
2222
"path/filepath"
2323
"strconv"
24+
"strings"
2425

2526
"github.com/kvannotten/pcd"
2627
homedir "github.com/mitchellh/go-homedir"
@@ -88,7 +89,7 @@ func findPodcast(idOrName string) (*pcd.Podcast, error) {
8889
if err != nil {
8990
switch err.(type) {
9091
case *strconv.NumError: // try as a name instead
91-
return findByName(idOrName), nil
92+
return findByNameFragment(idOrName), nil
9293
default:
9394
log.Print("Could not parse podcast search argument, please use the ID or the name")
9495
return nil, err
@@ -107,9 +108,10 @@ func findAll() []pcd.Podcast {
107108
return podcasts
108109
}
109110

110-
func findByName(name string) *pcd.Podcast {
111+
func findByNameFragment(name string) *pcd.Podcast {
111112
return findByFunc(func(podcast *pcd.Podcast) bool {
112-
return podcast.Name == name
113+
return strings.Contains(
114+
strings.ToLower(podcast.Name), strings.ToLower(name))
113115
})
114116
}
115117

@@ -121,12 +123,19 @@ func findByID(id int) *pcd.Podcast {
121123

122124
func findByFunc(fn func(podcast *pcd.Podcast) bool) *pcd.Podcast {
123125
podcasts := findAll()
126+
var matchedPodcasts = make([]*pcd.Podcast, 0)
124127

125128
for _, podcast := range podcasts {
126129
if fn(&podcast) {
127-
return &podcast
130+
matchedPodcast := podcast
131+
matchedPodcasts = append(matchedPodcasts, &matchedPodcast)
128132
}
129133
}
134+
if len(matchedPodcasts) == 1 {
135+
return matchedPodcasts[0]
136+
} else {
137+
log.Fatalf("Provided search term matched too many podcasts: %v", matchedPodcasts)
138+
}
130139

131140
return nil
132141
}

0 commit comments

Comments
 (0)