/
blocks_emacs.go
267 lines (258 loc) · 6.68 KB
/
blocks_emacs.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
package promptx
import (
buffer "github.com/aggronmagi/promptx/buffer"
"github.com/aggronmagi/promptx/internal/debug"
runewidth "github.com/mattn/go-runewidth"
)
// BlocksEmacsBuffer emacs operate like input ctx.GetBuffer()fer
//
// Moving the cursor
// -----------------
// | ok | key | description |
// |-----+-----------+--------------------------------------------------------------|
// | [x] | Ctrl + a | Go to the beginning of the line (Home) |
// | [x] | Ctrl + e | Go to the End of the line (End) |
// | [x] | Ctrl + p | Previous command (Up arrow) |
// | [x] | Ctrl + n | Next command (Down arrow) |
// | [x] | Ctrl + f | Forward one character |
// | [x] | Ctrl + b | Backward one character |
// | [x] | Meta + B | |
// | [x] | Meta + F | |
// Editing
// -------
// | ok | key | description |
// |-----+----------+---------------------------------------------------------|
// | [x] | Ctrl + L | Clear the Screen, similar to the clear command |
// | [x] | Ctrl + d | Delete character under the cursor |
// | [x] | Ctrl + h | Delete character before the cursor (Backspace) |
// | [x] | Ctrl + w | Cut the Word before the cursor to the clipboard. |
// | [x] | Ctrl + k | Cut the Line after the cursor to the clipboard. |
// | [x] | Ctrl + u | Cut/delete the Line before the cursor to the clipboard. |
// | [ ] | Ctrl + t | Swap the last two characters before the cursor (typo). |
// | [ ] | Esc + t | Swap the last two words before the cursor. |
// | [ ] | ctrl + y | Paste the last thing to be cut (yank) |
// | [ ] | ctrl + _ | Undo |
type BlocksEmacsBuffer struct {
EmptyBlocks
buf *Buffer
// colors
TextColor Color
BGColor Color
// // Select
// SelectTextColor Color
// SelectBGColor Color
init bool
}
func (c *BlocksEmacsBuffer) ResetBuffer() {
c.buf = buffer.NewBuffer()
return
}
func (c *BlocksEmacsBuffer) GetBuffer() *Buffer {
return c.buf
}
func (c *BlocksEmacsBuffer) InitBlocks() {
if c.init {
return
}
c.SetActive(true)
c.ResetBuffer()
for _, v := range emacsKeyBindings {
c.BindKey(v.Fn, v.Key)
}
for _, v := range commonKeyBindings {
c.BindKey(v.Fn, v.Key)
}
c.init = true
}
// Render render to console
func (c *BlocksEmacsBuffer) Render(ctx PrintContext, preCursor int) int {
if c.buf == nil {
return preCursor
}
if ctx.Prepare() {
ctx.SetBuffer(c.buf)
return runewidth.StringWidth(c.buf.Text()) + preCursor
}
// SetCtx.GetBuffer()fer to notify show cursor
ctx.SetBuffer(c.buf)
text := c.buf.Text()
out := ctx.Writer()
out.SetColor(c.TextColor, c.BGColor, false)
out.WriteStr(text)
out.SetColor(DefaultColor, DefaultColor, false)
return runewidth.StringWidth(text) + preCursor
}
var emacsKeyBindings = []KeyBind{
// Go to the End of the line
{
Key: ControlE,
Fn: func(ctx PressContext) bool {
x := []rune(ctx.GetBuffer().Document().TextAfterCursor())
ctx.GetBuffer().CursorRight(len(x))
return false
},
},
// Go to the beginning of the line
{
Key: ControlA,
Fn: func(ctx PressContext) bool {
x := []rune(ctx.GetBuffer().Document().TextBeforeCursor())
ctx.GetBuffer().CursorLeft(len(x))
return false
},
},
// Cut the Line after the cursor
{
Key: ControlK,
Fn: func(ctx PressContext) bool {
x := []rune(ctx.GetBuffer().Document().TextAfterCursor())
ctx.GetBuffer().Delete(len(x))
return false
},
},
// Cut/delete the Line before the cursor
{
Key: ControlU,
Fn: func(ctx PressContext) bool {
x := []rune(ctx.GetBuffer().Document().TextBeforeCursor())
ctx.GetBuffer().DeleteBeforeCursor(len(x))
return false
},
},
// Delete character under the cursor
{
Key: ControlD,
Fn: func(ctx PressContext) bool {
if ctx.GetBuffer().Text() != "" {
ctx.GetBuffer().Delete(1)
}else {
// use control-d exit
return true
}
return false
},
},
// Backspace
{
Key: ControlH,
Fn: func(ctx PressContext) bool {
ctx.GetBuffer().DeleteBeforeCursor(1)
return false
},
},
// Left word arrow:
{
Key: MetaB,
Fn: func(ctx PressContext) bool {
buf := ctx.GetBuffer()
doc := buf.Document()
buf.CursorLeft(len([]rune(doc.GetWordBeforeCursorWithSpace())))
return false
},
},
// right word arrow:
{
Key: MetaF,
Fn: func(ctx PressContext) bool {
buf := ctx.GetBuffer()
doc := buf.Document()
buf.CursorRight(len([]rune(doc.GetWordAfterCursorWithSpace())))
return false
},
},
// Right allow: Forward one character
{
Key: ControlF,
Fn: func(ctx PressContext) bool {
ctx.GetBuffer().CursorRight(1)
return false
},
},
// Left allow: Backward one character
{
Key: ControlB,
Fn: func(ctx PressContext) bool {
ctx.GetBuffer().CursorLeft(1)
return false
},
},
// Cut the Word before the cursor.
{
Key: ControlW,
Fn: func(ctx PressContext) bool {
ctx.GetBuffer().DeleteBeforeCursor(len([]rune(
ctx.GetBuffer().Document().GetWordBeforeCursorWithSpace(),
)))
return false
},
},
// Clear the Screen, similar to the clear command
{
Key: ControlL,
Fn: func(ctx PressContext) bool {
out := ctx.Writer()
out.EraseScreen()
out.CursorGoTo(0, 0)
debug.AssertNoError(out.Flush())
return false
},
},
}
var commonKeyBindings = []KeyBind{
// Go to the End of the line
{
Key: End,
Fn: func(ctx PressContext) (exit bool) {
buf := ctx.GetBuffer()
x := []rune(buf.Document().TextAfterCursor())
buf.CursorRight(len(x))
return
},
},
// Go to the beginning of the line
{
Key: Home,
Fn: func(ctx PressContext) (exit bool) {
buf := ctx.GetBuffer()
x := []rune(buf.Document().TextBeforeCursor())
buf.CursorLeft(len(x))
return
},
},
// Delete character under the cursor
{
Key: Delete,
Fn: func(ctx PressContext) (exit bool) {
buf := ctx.GetBuffer()
buf.Delete(1)
return
},
},
// Backspace
{
Key: Backspace,
Fn: func(ctx PressContext) (exit bool) {
buf := ctx.GetBuffer()
buf.DeleteBeforeCursor(1)
return
},
},
// Right allow: Forward one character
{
Key: Right,
Fn: func(ctx PressContext) (exit bool) {
buf := ctx.GetBuffer()
buf.CursorRight(1)
return
},
},
// Left allow: Backward one character
{
Key: Left,
Fn: func(ctx PressContext) (exit bool) {
buf := ctx.GetBuffer()
buf.CursorLeft(1)
return
},
},
}