This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
fuzzymentions.go
102 lines (77 loc) · 1.87 KB
/
fuzzymentions.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"fmt"
"strings"
"github.com/diamondburned/tview/v2"
"github.com/sahilm/fuzzy"
)
// String returns the fuzzy search part of the struct
func (gm UserStoreArray) String(i int) string {
var s = gm[i].Name
if gm[i].Nick != "" {
s += " " + gm[i].Nick
}
return s
}
// Len returns the length
func (gm UserStoreArray) Len() int {
return len(gm)
}
// FuzzyMembers fuzzy searches and returns the slice of results
func FuzzyMembers(pattern string, s *UserStore, guildID int64) (fzr UserStoreArray) {
s.RLock()
defer s.RUnlock()
this, ok := s.Guilds[guildID]
if !ok {
return
}
results := fuzzy.FindFrom(pattern, this)
for i := 0; i < len(results) && i < 10; i++ {
fzr = append(fzr, this[results[i].Index])
}
return
}
func fuzzyMentions(last string) {
var fuzzied UserStoreArray
if len(last) > 0 && Channel != nil {
fuzzied = FuzzyMembers(
strings.TrimPrefix(last, "@"), us, Channel.GuildID,
)
}
clearList()
if len(fuzzied) > 0 {
g, _ := d.State.Guild(Channel.GuildID)
for i, u := range fuzzied {
var username = u.Name + "[::d]#" + u.Discrim + "[::-]"
if u.Nick != "" {
username += " [::d](" + u.Nick + ")[::-]"
}
if g != nil {
for _, p := range g.Presences {
if p.User.ID == fuzzied[i].ID {
username = fmt.Sprintf(
"[%s]%s[-]",
ReflectStatusColor(p.Status),
username,
)
}
}
}
autocomp.InsertItem(i, &tview.ListItem{"@" + username, "", 0, nil})
}
rightflex.ResizeItem(autocomp, min(len(fuzzied), 10), 1)
autofillfunc = func(i int) {
words := strings.Fields(input.GetText())
withoutlast := words[:len(words)-1]
withoutlast = append(withoutlast, fmt.Sprintf(
"<@%d> ", fuzzied[i].ID,
))
input.SetText(strings.Join(withoutlast, " "))
clearList()
app.SetFocus(input)
}
} else {
rightflex.ResizeItem(autocomp, 1, 1)
}
app.Draw()
}