-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsearch.go
45 lines (40 loc) · 851 Bytes
/
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
package main
import (
"bytes"
"fmt"
"regexp"
"strings"
)
// search takes a query and returns torrents with match
func search(tokens []string) {
// make sure that we got a query
if len(tokens) == 0 {
send("search: needs an argument", false)
return
}
query := strings.Join(tokens, " ")
// "(?i)" for case insensitivity
regx, err := regexp.Compile("(?i)" + query)
if err != nil {
logger.Print(err)
send("search: "+err.Error(), false)
return
}
torrents, err := rtorrent.Torrents()
if err != nil {
logger.Print(err)
send("search: "+err.Error(), false)
return
}
buf := new(bytes.Buffer)
for i := range torrents {
if regx.MatchString(torrents[i].Name) {
buf.WriteString(fmt.Sprintf("<%d> %s\n", i, torrents[i].Name))
}
}
if buf.Len() == 0 {
send("No matches!", false)
return
}
send(buf.String(), false)
}