-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui_editor.go
96 lines (92 loc) · 2.47 KB
/
gui_editor.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
package main
import (
"github.com/Alkorin/gocui"
)
type HistoryEditor struct {
history []string
currentHistoryLine int
callback func(string)
}
func NewHistoryEditor(cb func(string)) *HistoryEditor {
return &HistoryEditor{
history: []string{""},
callback: cb,
}
}
func (h *HistoryEditor) Edit(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
switch {
case ch != 0 && mod == 0:
v.EditWrite(ch)
case key == gocui.KeySpace:
v.EditWrite(' ')
case key == gocui.KeyBackspace || key == gocui.KeyBackspace2:
v.EditDelete(true)
case key == gocui.KeyDelete:
v.EditDelete(false)
case key == gocui.KeyInsert:
v.Overwrite = !v.Overwrite
case key == gocui.KeyArrowLeft:
v.MoveCursor(-1, 0, false)
case key == gocui.KeyArrowRight:
v.MoveCursor(1, 0, false)
case key == gocui.KeyHome || key == gocui.KeyCtrlA:
v.SetCursor(0, 0)
case key == gocui.KeyEnd || key == gocui.KeyCtrlE:
v.SetCursor(0, 0)
if lines := v.BufferLines(); len(lines) == 1 {
v.MoveCursor(len(lines[0]), 0, true)
}
case key == gocui.KeyCtrlK:
if lines := v.BufferLines(); len(lines) == 1 {
x, _ := v.Cursor()
data := lines[0][:x]
v.Clear()
v.SetCursor(0, 0)
v.Write([]byte(data))
v.MoveCursor(len(data), 0, true)
}
case key == gocui.KeyCtrlU:
if lines := v.BufferLines(); len(lines) == 1 {
x, _ := v.Cursor()
data := lines[0][x:]
v.Clear()
v.SetCursor(0, 0)
v.Write([]byte(data))
v.MoveCursor(len(data), 0, true)
}
case key == gocui.KeyArrowDown:
if h.currentHistoryLine < len(h.history)-1 {
// Save current line
if lines := v.BufferLines(); len(lines) == 1 {
h.history[h.currentHistoryLine] = lines[0]
}
h.currentHistoryLine++
v.Clear()
v.SetCursor(0, 0)
v.Write([]byte(h.history[h.currentHistoryLine]))
v.MoveCursor(len(h.history[h.currentHistoryLine]), 0, true)
}
case key == gocui.KeyArrowUp:
if h.currentHistoryLine > 0 {
// Save current line
if lines := v.BufferLines(); len(lines) == 1 {
h.history[h.currentHistoryLine] = lines[0]
}
h.currentHistoryLine--
v.Clear()
v.SetCursor(0, 0)
v.Write([]byte(h.history[h.currentHistoryLine]))
v.MoveCursor(len(h.history[h.currentHistoryLine]), 0, true)
}
case key == gocui.KeyEnter:
if lines := v.BufferLines(); len(lines) == 1 {
line := lines[0]
h.callback(line)
h.history[len(h.history)-1] = line
h.history = append(h.history, "")
h.currentHistoryLine = len(h.history) - 1
v.Clear()
v.SetCursor(0, 0)
}
}
}