-
Notifications
You must be signed in to change notification settings - Fork 155
/
engine.go
332 lines (295 loc) · 6.8 KB
/
engine.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package jid
import (
"io"
"strings"
termbox "github.com/nsf/termbox-go"
)
const (
DefaultY int = 1
FilterPrompt string = "[Filter]> "
)
type EngineInterface interface {
Run() EngineResultInterface
GetQuery() QueryInterface
}
type EngineResultInterface interface {
GetQueryString() string
GetContent() string
GetError() error
}
type Engine struct {
manager *JsonManager
query QueryInterface
queryCursorIdx int
term *Terminal
complete []string
keymode bool
candidates []string
candidatemode bool
candidateidx int
contentOffset int
queryConfirm bool
prettyResult bool
}
type EngineAttribute struct {
DefaultQuery string
Monochrome bool
PrettyResult bool
}
func NewEngine(s io.Reader, ea *EngineAttribute) (EngineInterface, error) {
j, err := NewJsonManager(s)
if err != nil {
return nil, err
}
e := &Engine{
manager: j,
term: NewTerminal(FilterPrompt, DefaultY, ea.Monochrome),
query: NewQuery([]rune(ea.DefaultQuery)),
complete: []string{"", ""},
keymode: false,
candidates: []string{},
candidatemode: false,
candidateidx: 0,
contentOffset: 0,
queryConfirm: false,
prettyResult: ea.PrettyResult,
}
e.queryCursorIdx = e.query.Length()
return e, nil
}
type EngineResult struct {
content string
qs string
err error
}
func (er *EngineResult) GetQueryString() string {
return er.qs
}
func (er *EngineResult) GetContent() string {
return er.content
}
func (er *EngineResult) GetError() error {
return er.err
}
func (e *Engine) GetQuery() QueryInterface {
return e.query
}
func (e *Engine) Run() EngineResultInterface {
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
var contents []string
for {
if e.query.StringGet() == "" {
e.query.StringSet(".")
e.queryCursorIdx = e.query.Length()
}
bl := len(contents)
contents = e.getContents()
e.setCandidateData()
e.queryConfirm = false
if bl != len(contents) {
e.contentOffset = 0
}
ta := &TerminalDrawAttributes{
Query: e.query.StringGet(),
Contents: contents,
CandidateIndex: e.candidateidx,
ContentsOffsetY: e.contentOffset,
Complete: e.complete[0],
Candidates: e.candidates,
CursorOffset: e.query.IndexOffset(e.queryCursorIdx),
}
err = e.term.Draw(ta)
if err != nil {
panic(err)
}
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
switch ev.Key {
case 0:
e.inputChar(ev.Ch)
case termbox.KeyBackspace, termbox.KeyBackspace2:
e.deleteChar()
case termbox.KeyTab:
e.tabAction()
case termbox.KeyArrowLeft, termbox.KeyCtrlB:
e.moveCursorBackward()
case termbox.KeyArrowRight, termbox.KeyCtrlF:
e.moveCursorForward()
case termbox.KeyHome, termbox.KeyCtrlA:
e.moveCursorToTop()
case termbox.KeyEnd, termbox.KeyCtrlE:
e.moveCursorToEnd()
case termbox.KeyCtrlK:
e.scrollToAbove()
case termbox.KeyCtrlJ:
e.scrollToBelow()
case termbox.KeyCtrlG:
e.scrollToBottom(len(contents))
case termbox.KeyCtrlT:
e.scrollToTop()
case termbox.KeyCtrlN:
_, h := termbox.Size()
e.scrollPageDown(len(contents), h)
case termbox.KeyCtrlP:
_, h := termbox.Size()
e.scrollPageUp(h)
case termbox.KeyCtrlL:
e.toggleKeymode()
case termbox.KeyCtrlU:
e.deleteLineQuery()
case termbox.KeyCtrlW:
e.deleteWordBackward()
case termbox.KeyEsc:
e.escapeCandidateMode()
case termbox.KeyEnter:
if !e.candidatemode {
var cc string
var err error
if e.prettyResult {
cc, _, _, err = e.manager.GetPretty(e.query, true)
} else {
cc, _, _, err = e.manager.Get(e.query, true)
}
return &EngineResult{
content: cc,
qs: e.query.StringGet(),
err: err,
}
}
e.confirmCandidate()
case termbox.KeyCtrlC:
return &EngineResult{}
default:
}
case termbox.EventError:
panic(ev.Err)
break
default:
}
}
}
func (e *Engine) getContents() []string {
var c string
var contents []string
c, e.complete, e.candidates, _ = e.manager.GetPretty(e.query, e.queryConfirm)
if e.keymode {
contents = e.candidates
} else {
contents = strings.Split(c, "\n")
}
return contents
}
func (e *Engine) setCandidateData() {
if l := len(e.candidates); e.complete[0] == "" && l > 1 {
if e.candidateidx >= l {
e.candidateidx = 0
}
} else {
e.candidatemode = false
}
if !e.candidatemode {
e.candidateidx = 0
e.candidates = []string{}
}
}
func (e *Engine) confirmCandidate() {
_, _ = e.query.PopKeyword()
_ = e.query.StringAdd(".")
_ = e.query.StringAdd(e.candidates[e.candidateidx])
e.queryCursorIdx = e.query.Length()
e.queryConfirm = true
}
func (e *Engine) deleteChar() {
if i := e.queryCursorIdx - 1; i > 0 {
_ = e.query.Delete(i)
e.queryCursorIdx--
}
}
func (e *Engine) deleteLineQuery() {
_ = e.query.StringSet("")
e.queryCursorIdx = 0
}
func (e *Engine) scrollToBelow() {
e.contentOffset++
}
func (e *Engine) scrollToAbove() {
if o := e.contentOffset - 1; o >= 0 {
e.contentOffset = o
}
}
func (e *Engine) scrollToBottom(rownum int) {
e.contentOffset = rownum - 1
}
func (e *Engine) scrollToTop() {
e.contentOffset = 0
}
func (e *Engine) scrollPageDown(rownum int, height int) {
co := rownum - 1
if o := rownum - e.contentOffset; o > height {
co = e.contentOffset + (height - DefaultY)
}
e.contentOffset = co
}
func (e *Engine) scrollPageUp(height int) {
co := 0
if o := e.contentOffset - (height - DefaultY); o > 0 {
co = o
}
e.contentOffset = co
}
func (e *Engine) toggleKeymode() {
e.keymode = !e.keymode
}
func (e *Engine) deleteWordBackward() {
if k, _ := e.query.StringPopKeyword(); k != "" && !strings.Contains(k, "[") {
_ = e.query.StringAdd(".")
}
e.queryCursorIdx = e.query.Length()
}
func (e *Engine) tabAction() {
if !e.candidatemode {
e.candidatemode = true
if e.complete[0] != e.complete[1] && e.complete[0] != "" {
if k, _ := e.query.StringPopKeyword(); !strings.Contains(k, "[") {
_ = e.query.StringAdd(".")
}
_ = e.query.StringAdd(e.complete[1])
} else {
_ = e.query.StringAdd(e.complete[0])
}
} else {
e.candidateidx = e.candidateidx + 1
}
e.queryCursorIdx = e.query.Length()
}
func (e *Engine) escapeCandidateMode() {
e.candidatemode = false
}
func (e *Engine) inputChar(ch rune) {
_ = e.query.Insert([]rune{ch}, e.queryCursorIdx)
e.queryCursorIdx++
}
func (e *Engine) moveCursorBackward() {
if i := e.queryCursorIdx - 1; i >= 0 {
e.queryCursorIdx--
}
}
func (e *Engine) moveCursorForward() {
if e.query.Length() > e.queryCursorIdx {
e.queryCursorIdx++
}
}
func (e *Engine) moveCursorWordBackwark() {
}
func (e *Engine) moveCursorWordForward() {
}
func (e *Engine) moveCursorToTop() {
e.queryCursorIdx = 0
}
func (e *Engine) moveCursorToEnd() {
e.queryCursorIdx = e.query.Length()
}