@@ -36,7 +36,7 @@ import (
36
36
"github.com/spf13/pflag"
37
37
)
38
38
39
- const version = "0.3.1 (2018-10-31 )"
39
+ const version = "0.3.2 (2018-12-04 )"
40
40
41
41
// TODO: in case of error, show it in red (bg?), then below show again initial normal output (see also #4)
42
42
// TODO: F1 should display help, and it should be multi-line, and scrolling licensing credits
@@ -283,9 +283,10 @@ func NewEditor(prompt string) *Editor {
283
283
284
284
type Editor struct {
285
285
// TODO: make editor multiline. Reuse gocui or something for this?
286
- prompt []rune
287
- value []rune
288
- cursor int
286
+ prompt []rune
287
+ value []rune
288
+ killspace []rune
289
+ cursor int
289
290
// lastw is length of value on last Draw; we need it to know how much to erase after backspace
290
291
lastw int
291
292
}
@@ -326,27 +327,43 @@ func (e *Editor) HandleKey(ev *tcell.EventKey) bool {
326
327
e .delete (- 1 )
327
328
case key (tcell .KeyDelete ):
328
329
e .delete (0 )
329
- case key (tcell .KeyLeft ):
330
+ case key (tcell .KeyLeft ),
331
+ key (tcell .KeyCtrlB ),
332
+ ctrlKey (tcell .KeyCtrlB ):
330
333
if e .cursor > 0 {
331
334
e .cursor --
332
335
}
333
- case key (tcell .KeyRight ):
336
+ case key (tcell .KeyRight ),
337
+ key (tcell .KeyCtrlF ),
338
+ ctrlKey (tcell .KeyCtrlF ):
334
339
if e .cursor < len (e .value ) {
335
340
e .cursor ++
336
341
}
342
+ case key (tcell .KeyCtrlA ),
343
+ ctrlKey (tcell .KeyCtrlA ):
344
+ e .cursor = 0
345
+ case key (tcell .KeyCtrlE ),
346
+ ctrlKey (tcell .KeyCtrlE ):
347
+ e .cursor = len (e .value )
348
+ case key (tcell .KeyCtrlK ),
349
+ ctrlKey (tcell .KeyCtrlK ):
350
+ e .kill ()
351
+ case key (tcell .KeyCtrlY ),
352
+ ctrlKey (tcell .KeyCtrlY ):
353
+ e .insert (e .killspace ... )
337
354
default :
338
355
// Unknown key/combination, not handled
339
356
return false
340
357
}
341
358
return true
342
359
}
343
360
344
- func (e * Editor ) insert (ch rune ) {
345
- // Insert character into value ( https://github.com/golang/go/wiki/SliceTricks#insert)
346
- e .value = append (e .value , 0 )
347
- copy (e .value [e .cursor + 1 :], e .value [e .cursor :])
348
- e .value [e .cursor ] = ch
349
- e .cursor ++
361
+ func (e * Editor ) insert (ch ... rune ) {
362
+ // Based on https://github.com/golang/go/wiki/SliceTricks#insert
363
+ e .value = append (e .value , ch ... ) // = PREFIX + SUFFIX + (filler )
364
+ copy (e .value [e .cursor + len ( ch ) :], e .value [e .cursor :]) // = PREFIX + (filler) + SUFFIX
365
+ copy ( e .value [e .cursor :], ch ) // = PREFIX + ch + SUFFIX
366
+ e .cursor += len ( ch )
350
367
}
351
368
352
369
func (e * Editor ) delete (dx int ) {
@@ -358,6 +375,13 @@ func (e *Editor) delete(dx int) {
358
375
e .cursor = pos
359
376
}
360
377
378
+ func (e * Editor ) kill () {
379
+ if e .cursor != len (e .value ) {
380
+ e .killspace = append (e .killspace [:0 ], e .value [e .cursor :]... )
381
+ }
382
+ e .value = e .value [:e .cursor ]
383
+ }
384
+
361
385
type BufView struct {
362
386
// TODO: Wrap bool
363
387
Y int // Y of the view in the Buf, for down/up scrolling
0 commit comments