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
/
fuzzychannels.go
137 lines (107 loc) · 2.47 KB
/
fuzzychannels.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"fmt"
"sort"
"strings"
"github.com/diamondburned/discordgo"
"github.com/diamondburned/tview/v2"
"github.com/sahilm/fuzzy"
)
type allChannels []fuzzyReadState
type fuzzyReadState struct {
*discordgo.Channel
Unread bool
Format string
}
var channelFuzzyCache = allChannels([]fuzzyReadState{})
// String returns the fuzzy search part of the struct
func (ac allChannels) String(i int) string {
if ac[i].Unread {
return "[::b]#" + ac[i].Format + "[::-]"
}
return "[::d]#" + ac[i].Format + "[::-]"
}
// Len returns the length
func (ac allChannels) Len() int {
return len(ac)
}
func fuzzyChannels(last string) {
var fuzzied []fuzzy.Match
if len(last) > 0 {
if len(channelFuzzyCache) == 0 {
for _, c := range d.State.PrivateChannels {
var name = c.Name
if c.Name == "" {
recips := make([]string, len(c.Recipients))
for i, r := range c.Recipients {
recips[i] = r.Username
}
name = HumanizeStrings(recips)
}
channelFuzzyCache = append(
channelFuzzyCache,
fuzzyReadState{c, isUnread(c), name},
)
}
for _, g := range d.State.Guilds {
for _, c := range g.Channels {
if !isSendCh(c.Type) {
continue
}
channelFuzzyCache = append(
channelFuzzyCache,
fuzzyReadState{
c,
isUnread(c),
c.Name + " (" + g.Name + ")",
},
)
}
}
}
fuzzied = fuzzy.FindFrom(
strings.TrimPrefix(last, "#"),
channelFuzzyCache,
)
if Channel != nil {
c, err := d.State.Channel(Channel.ID)
if err == nil {
guildID := c.GuildID
sort.SliceStable(fuzzied, func(i, j int) bool {
return channelFuzzyCache[fuzzied[i].Index].GuildID == guildID
})
}
}
}
clearList()
if len(fuzzied) > 0 {
for i, fz := range fuzzied {
autocomp.InsertItem(i, &tview.ListItem{fz.Str, "", 0, nil})
if i == 25 {
break
}
}
rightflex.ResizeItem(autocomp, min(len(fuzzied), 10), 1)
autofillfunc = func(i int) {
defer stateResetter()
words := strings.Fields(input.GetText())
withoutlast := words[:len(words)-1]
withoutlast = append(withoutlast, fmt.Sprintf(
"<#%d> ", channelFuzzyCache[fuzzied[i].Index].ID,
))
switch {
case strings.HasPrefix(input.GetText(), "/goto "):
input.SetText("")
gotoChannel(withoutlast)
return
default:
input.SetText(strings.Join(withoutlast, " "))
}
clearList()
app.SetFocus(input)
}
} else {
rightflex.ResizeItem(autocomp, 1, 1)
}
app.Draw()
}