瀏覽代碼

Support multiple words in file search

Gildas Chabot 4 年之前
父節點
當前提交
039aa791f2
共有 2 個文件被更改,包括 15 次插入2 次删除
  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
 func (fs *FileSource) Search(query string) []string {
37
 func (fs *FileSource) Search(query string) []string {
38
+	split := strings.Split(query, " ")
39
+
38
 	var res []string
40
 	var res []string
39
 	for _, path := range fs.Files {
41
 	for _, path := range fs.Files {
40
-		if strings.Contains(strings.ToLower(path), strings.ToLower(query)) {
42
+		if stringContainsAll(path, split) {
41
 			res = append(res, path)
43
 			res = append(res, path)
42
 		}
44
 		}
43
 	}
45
 	}
44
 
46
 
45
 	return res
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
 	fmt.Println(len(fs.Files))
16
 	fmt.Println(len(fs.Files))
17
-	for _, res := range fs.Search("angie") {
17
+	for _, res := range fs.Search("game of thrones s02e06") {
18
 		fmt.Println(res)
18
 		fmt.Println(res)
19
 	}
19
 	}
20
 }
20
 }