-
Notifications
You must be signed in to change notification settings - Fork 6
/
resultset.go
70 lines (57 loc) · 1.3 KB
/
resultset.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
67
68
69
70
package main
import (
"sort"
"strings"
)
type ResultArray []*Result
type ResultSet struct {
results ResultArray
count int
}
// Just for sorting
func (rc ResultArray) Len() int {
return len(rc)
}
func (rc ResultArray) Swap(i, j int) {
rc[i], rc[j] = rc[j], rc[i]
}
func (rc ResultArray) Less(i, j int) bool {
return rc[i].score > rc[j].score
}
func (rs *ResultSet) Insert(entry string) {
result := new(Result)
result.contents = strings.ToLower(entry)
result.displayContents = entry
result.id = rs.count
rs.results = append(rs.results, result)
rs.count++
}
func (rs *ResultSet) Filter(when int64, userinput string) (filtered ResultSet) {
if len(userinput) == 0 {
filtered.results = rs.results
filtered.count = len(rs.results)
for _, res := range filtered.results {
res.highlighted = nil
}
return
}
filtered.results = make(ResultArray, 0, 100)
filtered.count = 0
// Filter
for _, entry := range rs.results {
if global_lastkeypress > when {
break // partial
}
best := score(when, entry.contents, userinput)
entry.score, entry.highlighted = best.score, best.highlight
if entry.score > 0 {
filtered.results = append(filtered.results, entry)
filtered.count++
}
}
// Sort
sort.Sort(filtered.results)
// TODO: better cursor behaviouree
// r.SelectFirst()
return
}