@@ -24,38 +24,42 @@ type EngineResultInterface interface {
24
24
}
25
25
26
26
type Engine struct {
27
- manager * JsonManager
28
- query QueryInterface
29
- term * Terminal
30
- complete [] string
31
- keymode bool
32
- candidates [] string
33
- candidatemode bool
34
- candidateidx int
35
- contentOffset int
36
- queryConfirm bool
37
- cursorOffsetX int
27
+ manager * JsonManager
28
+ query QueryInterface
29
+ queryCursorIdx int
30
+ term * Terminal
31
+ complete [] string
32
+ keymode bool
33
+ candidates [] string
34
+ candidatemode bool
35
+ candidateidx int
36
+ contentOffset int
37
+ queryConfirm bool
38
38
}
39
39
40
- func NewEngine (s io.Reader , qs string ) (EngineInterface , error ) {
40
+ type EngineAttribute struct {
41
+ DefaultQuery string
42
+ Monochrome bool
43
+ }
44
+
45
+ func NewEngine (s io.Reader , ea * EngineAttribute ) (EngineInterface , error ) {
41
46
j , err := NewJsonManager (s )
42
47
if err != nil {
43
48
return nil , err
44
49
}
45
50
e := & Engine {
46
51
manager : j ,
47
- term : NewTerminal (FilterPrompt , DefaultY ),
48
- query : NewQuery ([]rune (qs )),
52
+ term : NewTerminal (FilterPrompt , DefaultY , ea . Monochrome ),
53
+ query : NewQuery ([]rune (ea . DefaultQuery )),
49
54
complete : []string {"" , "" },
50
55
keymode : false ,
51
56
candidates : []string {},
52
57
candidatemode : false ,
53
58
candidateidx : 0 ,
54
59
contentOffset : 0 ,
55
60
queryConfirm : false ,
56
- cursorOffsetX : 0 ,
57
61
}
58
- e .cursorOffsetX = len ( e .query .Get () )
62
+ e .queryCursorIdx = e .query .Length ( )
59
63
return e , nil
60
64
}
61
65
@@ -94,7 +98,7 @@ func (e *Engine) Run() EngineResultInterface {
94
98
95
99
if e .query .StringGet () == "" {
96
100
e .query .StringSet ("." )
97
- e .cursorOffsetX = len ( e .query .StringGet () )
101
+ e .queryCursorIdx = e .query .Length ( )
98
102
}
99
103
100
104
contents = e .getContents ()
@@ -103,15 +107,17 @@ func (e *Engine) Run() EngineResultInterface {
103
107
104
108
ta := & TerminalDrawAttributes {
105
109
Query : e .query .StringGet (),
106
- CursorOffsetX : e .cursorOffsetX ,
107
110
Contents : contents ,
108
111
CandidateIndex : e .candidateidx ,
109
112
ContentsOffsetY : e .contentOffset ,
110
113
Complete : e .complete [0 ],
111
114
Candidates : e .candidates ,
115
+ CursorOffset : e .query .IndexOffset (e .queryCursorIdx ),
116
+ }
117
+ err = e .term .Draw (ta )
118
+ if err != nil {
119
+ panic (err )
112
120
}
113
-
114
- e .term .draw (ta )
115
121
116
122
switch ev := termbox .PollEvent (); ev .Type {
117
123
case termbox .EventKey :
@@ -193,21 +199,22 @@ func (e *Engine) setCandidateData() {
193
199
func (e * Engine ) confirmCandidate () {
194
200
_ , _ = e .query .PopKeyword ()
195
201
_ = e .query .StringAdd ("." )
196
- q : = e .query .StringAdd (e .candidates [e .candidateidx ])
197
- e .cursorOffsetX = len ( q )
202
+ _ = e .query .StringAdd (e .candidates [e .candidateidx ])
203
+ e .queryCursorIdx = e . query . Length ( )
198
204
e .queryConfirm = true
199
205
}
200
206
201
207
func (e * Engine ) deleteChar () {
202
- if e . cursorOffsetX > 0 {
203
- _ = e .query .Delete (e . cursorOffsetX - 1 )
204
- e .cursorOffsetX -= 1
208
+ if i := e . queryCursorIdx - 1 ; i > 0 {
209
+ _ = e .query .Delete (i )
210
+ e .queryCursorIdx --
205
211
}
212
+
206
213
}
207
214
208
215
func (e * Engine ) deleteLineQuery () {
209
216
_ = e .query .StringSet ("" )
210
- e .cursorOffsetX = 0
217
+ e .queryCursorIdx = 0
211
218
}
212
219
213
220
func (e * Engine ) scrollToBelow () {
@@ -225,14 +232,12 @@ func (e *Engine) deleteWordBackward() {
225
232
if k , _ := e .query .StringPopKeyword (); k != "" && ! strings .Contains (k , "[" ) {
226
233
_ = e .query .StringAdd ("." )
227
234
}
228
- e .cursorOffsetX = len ( e .query .Get () )
235
+ e .queryCursorIdx = e .query .Length ( )
229
236
}
230
237
func (e * Engine ) tabAction () {
231
238
if ! e .candidatemode {
232
239
e .candidatemode = true
233
- if e .query .StringGet () == "" {
234
- _ = e .query .StringAdd ("." )
235
- } else if e .complete [0 ] != e .complete [1 ] && e .complete [0 ] != "" {
240
+ if e .complete [0 ] != e .complete [1 ] && e .complete [0 ] != "" {
236
241
if k , _ := e .query .StringPopKeyword (); ! strings .Contains (k , "[" ) {
237
242
_ = e .query .StringAdd ("." )
238
243
}
@@ -243,36 +248,35 @@ func (e *Engine) tabAction() {
243
248
} else {
244
249
e .candidateidx = e .candidateidx + 1
245
250
}
246
- e .cursorOffsetX = len ( e .query .Get () )
251
+ e .queryCursorIdx = e .query .Length ( )
247
252
}
248
253
func (e * Engine ) escapeCandidateMode () {
249
254
e .candidatemode = false
250
255
}
251
256
func (e * Engine ) inputChar (ch rune ) {
252
- b := len (e .query .Get ())
253
- q := e .query .StringInsert (string (ch ), e .cursorOffsetX )
254
- if b < len (q ) {
255
- e .cursorOffsetX += 1
256
- }
257
+ _ = e .query .Insert ([]rune {ch }, e .queryCursorIdx )
258
+ e .queryCursorIdx ++
257
259
}
258
260
259
261
func (e * Engine ) moveCursorBackward () {
260
- if e . cursorOffsetX > 0 {
261
- e .cursorOffsetX -= 1
262
+ if i := e . queryCursorIdx - 1 ; i >= 0 {
263
+ e .queryCursorIdx --
262
264
}
263
265
}
266
+
264
267
func (e * Engine ) moveCursorForward () {
265
- if len ( e .query .Get ()) > e .cursorOffsetX {
266
- e .cursorOffsetX += 1
268
+ if e .query .Length () > e .queryCursorIdx {
269
+ e .queryCursorIdx ++
267
270
}
268
271
}
272
+
269
273
func (e * Engine ) moveCursorWordBackwark () {
270
274
}
271
275
func (e * Engine ) moveCursorWordForward () {
272
276
}
273
277
func (e * Engine ) moveCursorToTop () {
274
- e .cursorOffsetX = 0
278
+ e .queryCursorIdx = 0
275
279
}
276
280
func (e * Engine ) moveCursorToEnd () {
277
- e .cursorOffsetX = len ( e .query .Get () )
281
+ e .queryCursorIdx = e .query .Length ( )
278
282
}
0 commit comments