Browse Source

Support multiple words in file search

Gildas Chabot 4 years ago
parent
commit
039aa791f2
2 changed files with 15 additions and 2 deletions
  1. 14 1
      files.go
  2. 1 1
      files_test.go

+ 14 - 1
files.go

@@ -35,12 +35,25 @@ func (fs *FileSource) Scan(root string) error {
35 35
 }
36 36
 
37 37
 func (fs *FileSource) Search(query string) []string {
38
+	split := strings.Split(query, " ")
39
+
38 40
 	var res []string
39 41
 	for _, path := range fs.Files {
40
-		if strings.Contains(strings.ToLower(path), strings.ToLower(query)) {
42
+		if stringContainsAll(path, split) {
41 43
 			res = append(res, path)
42 44
 		}
43 45
 	}
44 46
 
45 47
 	return res
46 48
 }
49
+
50
+func stringContainsAll(path string, split []string) bool {
51
+	path = strings.ToLower(path)
52
+
53
+	for _, s := range split {
54
+		if !strings.Contains(path, strings.ToLower(s)) {
55
+			return false
56
+		}
57
+	}
58
+	return true
59
+}

+ 1 - 1
files_test.go

@@ -14,7 +14,7 @@ func TestFileSource(t *testing.T) {
14 14
 	}
15 15
 
16 16
 	fmt.Println(len(fs.Files))
17
-	for _, res := range fs.Search("angie") {
17
+	for _, res := range fs.Search("game of thrones s02e06") {
18 18
 		fmt.Println(res)
19 19
 	}
20 20
 }