-
Notifications
You must be signed in to change notification settings - Fork 1
/
fuzzyselect.go
274 lines (223 loc) · 6.21 KB
/
fuzzyselect.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package main
import (
"bufio"
"fmt"
"github.com/aqatl/mal/anilist"
"github.com/aqatl/mal/mal"
"github.com/fatih/color"
"github.com/jroimartin/gocui"
"github.com/sahilm/fuzzy"
"github.com/urfave/cli"
"strings"
)
func fuzzySelectEntry(ctx *cli.Context) error {
_, list, err := loadMAL(ctx)
if err != nil {
return err
}
cfg := LoadConfig()
displayData := make([]string, len(list))
for i := range list {
displayData[i] = list[i].Title
}
searchData := make([]string, len(list))
for i, entry := range list {
searchData[i] = strings.ToLower(fmt.Sprintf("%s %s",
entry.Title,
strings.Replace(entry.Synonyms, ";", "", -1)))
}
fsc := &fuzzySelCui{
DisplayData: displayData,
SearchData: searchData,
MatchIdx: -1,
}
initSearch := strings.Join(ctx.Args(), " ")
if ctx.NArg() != 0 {
fsc.Matches = fuzzy.Find(initSearch, fsc.SearchData)
if matchesLen := len(fsc.Matches); matchesLen == 0 {
return fmt.Errorf("no match found")
} else if matchesLen == 1 {
saveSelection(cfg, list[fsc.Matches[0].Index])
return nil
}
}
if err = startFuzzySelectCUI(fsc, initSearch); err != nil || fsc.MatchIdx == -1 {
return err
}
saveSelection(cfg, list[fsc.MatchIdx])
return nil
}
func alFuzzySelectEntry(ctx *cli.Context) error {
al, err := loadAniList(ctx)
if err != nil {
return err
}
cfg := LoadConfig()
list := alGetList(al, anilist.All)
displayData := make([]string, len(list))
for i := range list {
displayData[i] = list[i].Title.UserPreferred
}
searchData := make([]string, len(list))
for i, entry := range list {
searchData[i] = strings.ToLower(entry.Title.Romaji +
entry.Title.English +
entry.Title.Native +
strings.Join(entry.Synonyms, " "))
}
fsc := &fuzzySelCui{
DisplayData: displayData,
SearchData: searchData,
MatchIdx: -1,
}
initSearch := strings.Join(ctx.Args(), " ")
if ctx.NArg() != 0 {
fsc.Matches = fuzzy.Find(initSearch, fsc.SearchData)
if matchesLen := len(fsc.Matches); matchesLen == 0 {
return fmt.Errorf("no match found")
} else if matchesLen == 1 {
alSaveSelection(cfg, &list[fsc.Matches[0].Index], al.User.MediaListOptions.ScoreFormat)
return nil
}
}
if err = startFuzzySelectCUI(fsc, initSearch); err != nil || fsc.MatchIdx == -1 {
return err
}
alSaveSelection(cfg, &list[fsc.MatchIdx], al.User.MediaListOptions.ScoreFormat)
return nil
}
func startFuzzySelectCUI(fsc *fuzzySelCui, initSearch string) error {
gui, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
return fmt.Errorf("gocui error: %v", err)
}
gui.SetManager(fsc)
fsc.setGuiKeyBindings(gui)
gui.Cursor = true
gui.Mouse = false
gui.Highlight = true
gui.SelFgColor = gocui.ColorGreen
fsc.Layout(gui)
fsc.InputView.Write([]byte(initSearch))
fsc.InputView.Editor.Edit(fsc.InputView, gocui.KeyBackspace, 0, gocui.ModNone)
fsc.InputView.MoveCursor(len(initSearch), 0, true)
err = gui.MainLoop()
gui.Close()
if err == gocui.ErrQuit {
err = nil
}
return err
}
func saveSelection(cfg *Config, entry *mal.Anime) {
cfg.SelectedID = entry.ID
cfg.Save()
fmt.Println("Selected entry:")
malPrintEntryDetails(entry)
}
const (
FsgInputView = "fsgInputView"
FsgOutputView = "fsgOutputView"
FsgShortcutsView = "fsgShortcutsView"
)
type fuzzySelCui struct {
DisplayData []string
SearchData []string
Matches []fuzzy.Match
MatchIdx int
InputView, OutputView *gocui.View
}
func (fsc *fuzzySelCui) Layout(gui *gocui.Gui) error {
w, h := gui.Size()
if v, err := gui.SetView(FsgInputView, 0, 0, w-1, 2); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = "Input"
v.Editor = gocui.EditorFunc(fsc.InputViewEditor)
v.Editable = true
v.Wrap = true
gui.SetCurrentView(FsgInputView)
fsc.InputView = v
}
if v, err := gui.SetView(FsgOutputView, 0, 3, w-1, h-4); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = "Found entries"
v.SelBgColor = gocui.ColorGreen
v.SelFgColor = gocui.ColorBlack
v.Highlight = true
v.Editable = true
v.Editor = gocui.EditorFunc(fsc.OutputViewEditor)
fsc.OutputView = v
}
if v, err := gui.SetView(FsgShortcutsView, 0, h-3, w-1, h-1); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = "Shortcuts"
v.Editable = false
fmt.Fprintf(v, "Ctrl+C: quit | Tab: switch window | Enter: select highlighted entry")
}
return nil
}
var highlighter = color.New(color.FgBlack, color.BgWhite).FprintFunc()
func (fsc *fuzzySelCui) InputViewEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
if key == gocui.KeyArrowUp || key == gocui.KeyArrowDown {
fsc.OutputViewEditor(fsc.OutputView, key, ch, mod)
return
}
gocui.DefaultEditor.Edit(v, key, ch, mod)
fsc.OutputView.Clear()
pattern := strings.TrimSpace(v.Buffer())
fsc.Matches = fuzzy.Find(pattern, fsc.SearchData)
buf := bufio.NewWriter(fsc.OutputView)
for _, match := range fsc.Matches {
mIdx := 0
for i, r := range []rune(fsc.DisplayData[match.Index]) {
if mIdx < len(match.MatchedIndexes) && i == match.MatchedIndexes[mIdx] {
mIdx++
highlighter(buf, string(r))
} else {
buf.WriteRune(r)
}
}
buf.WriteRune('\n')
}
buf.Flush()
fsc.OutputView.SetCursor(0, 0)
}
func (fsc *fuzzySelCui) OutputViewEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
switch {
case key == gocui.KeyArrowDown || ch == 'j':
v.MoveCursor(0, 1, false)
case key == gocui.KeyArrowUp || ch == 'k':
v.MoveCursor(0, -1, false)
}
}
func (fsc *fuzzySelCui) setGuiKeyBindings(gui *gocui.Gui) {
quit := func(gui *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
gui.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit)
gui.SetKeybinding("", gocui.KeyCtrlQ, gocui.ModNone, quit)
gui.SetKeybinding("", gocui.KeyTab, gocui.ModNone, func(gui *gocui.Gui, v *gocui.View) error {
switch v.Name() {
case FsgInputView:
gui.SetCurrentView(FsgOutputView)
case FsgOutputView:
gui.SetCurrentView(FsgInputView)
}
return nil
})
gui.SetKeybinding("", gocui.KeyEnter, gocui.ModNone, func(gui *gocui.Gui, v *gocui.View) error {
_, y := fsc.OutputView.Cursor()
_, oy := fsc.OutputView.Origin()
y += oy
if ml := len(fsc.Matches); ml == 0 || y > ml-1 || y < 0 {
return nil
}
fsc.MatchIdx = fsc.Matches[y].Index
return gocui.ErrQuit
})
}