-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
66 lines (61 loc) · 2.26 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2019 by Chris Palmer (https://noncombatant.org)
// SPDX-License-Identifier: GPL-3.0
package main
import (
"strings"
)
func normalizeStringForSearch(s string) string {
normalized := removeAccents(s)
return strings.ToLower(normalized)
}
func matchItem(info *ItemInfo, queries []Query) bool {
for _, query := range queries {
matched := false
if query.Keyword == "path" || query.Keyword == "pathname" {
matched = strings.Contains(info.NormalizedPathname, query.Term)
} else if query.Keyword == "album" {
matched = strings.Contains(info.NormalizedAlbum, query.Term)
} else if query.Keyword == "artist" {
matched = strings.Contains(info.NormalizedArtist, query.Term)
} else if query.Keyword == "name" {
matched = strings.Contains(info.NormalizedName, query.Term)
} else if query.Keyword == "disc" {
matched = strings.Contains(info.NormalizedDisc, query.Term)
} else if query.Keyword == "track" {
matched = strings.Contains(info.NormalizedTrack, query.Term)
} else if query.Keyword == "year" {
matched = strings.Contains(info.NormalizedYear, query.Term)
} else if query.Keyword == "genre" {
matched = strings.Contains(info.NormalizedGenre, query.Term)
} else if query.Keyword == "mtime" || query.Keyword == "added" {
matched = strings.Contains(info.ModTime, query.Term)
} else {
if strings.Contains(info.NormalizedPathname, query.Term) ||
strings.Contains(info.NormalizedAlbum, query.Term) ||
strings.Contains(info.NormalizedArtist, query.Term) ||
strings.Contains(info.NormalizedName, query.Term) ||
strings.Contains(info.NormalizedDisc, query.Term) ||
strings.Contains(info.NormalizedTrack, query.Term) ||
strings.Contains(info.NormalizedYear, query.Term) ||
strings.Contains(info.NormalizedGenre, query.Term) ||
strings.Contains(info.ModTime, query.Term) {
matched = true
}
}
if (matched && query.Negated) || (!matched && !query.Negated) {
return false
}
}
return true
}
func matchItems(infos ItemInfos, rawQuery string) ItemInfos {
query := strings.TrimSpace(normalizeStringForSearch(rawQuery))
queries := reconstructQueries(parseTerms(query))
results := ItemInfos{}
for _, info := range infos {
if matchItem(&info, queries) {
results = append(results, info)
}
}
return results
}