-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.go
463 lines (376 loc) · 11 KB
/
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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package main
import (
"bufio"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"time"
"github.com/gdamore/tcell/v2"
"github.com/mattn/go-runewidth"
)
func newEditor(scr tcell.Screen) *editor {
ed := &editor{
scr: scr,
}
ed.ops = []keyMapping{
{tcell.KeyCtrlSpace, ed.selectText, "start/stop selecting text"},
{tcell.KeyCtrlA, ed.gotoBOL, "go to beginning of line"},
{tcell.KeyCtrlB, ed.newBuffer, "create new buffer"},
{tcell.KeyCtrlC, ed.copyText, "copy selected text to clipboard"},
{tcell.KeyCtrlD, ed.closeBuffer, "close current buffer"},
{tcell.KeyCtrlE, ed.gotoEOL, "go to end of line"},
{tcell.KeyCtrlF, ed.find, "find text"},
{tcell.KeyCtrlH, ed.showHelp, "show help"},
{tcell.KeyCtrlK, ed.deleteToEOL, "delete text to end of line"},
{tcell.KeyCtrlL, ed.redraw, "redraw screen"},
{tcell.KeyCtrlN, ed.nextBuffer, "go to next file"},
{tcell.KeyCtrlO, ed.openFile, "open file in new buffer"},
{tcell.KeyCtrlP, ed.prevBuffer, "go to previous file"},
{tcell.KeyCtrlQ, ed.quit, "quit"},
{tcell.KeyCtrlR, ed.redo, "redo previously undone change"},
{tcell.KeyCtrlS, ed.save, "save file"},
{tcell.KeyCtrlU, ed.deleteFromBOL, "delete text from beginning of line"},
{tcell.KeyCtrlV, ed.pasteText, "paste text from clipboard"},
{tcell.KeyCtrlW, ed.saveAs, "save file as"},
{tcell.KeyCtrlX, ed.cutText, "cut selected text to clipboard"},
{tcell.KeyCtrlZ, ed.undo, "undo last change"},
{tcell.KeyCR, ed.newLine, "insert new line"},
{tcell.KeyUp, ed.keyUp, "go to previous line"},
{tcell.KeyDown, ed.keyDown, "go to next line"},
{tcell.KeyLeft, ed.keyLeft, "go to previous character"},
{tcell.KeyRight, ed.keyRight, "go to next character"},
{tcell.KeyPgDn, ed.pageDown, "go to next page"},
{tcell.KeyPgUp, ed.pageUp, "go to previous page"},
{tcell.KeyDEL, ed.keyBackspace, "delete character left from cursor"},
{tcell.KeyDelete, ed.keyDel, "delete character right from cursor"},
}
return ed
}
type keyMapping struct {
Key tcell.Key
Func func()
Desc string
}
type editor struct {
bufs []*buffer
scr tcell.Screen
bufIdx int
quitInputLoop bool
clipboard [][]rune
ops []keyMapping
}
func (e *editor) inputLoop() {
for {
e.redrawScreen()
if e.quitInputLoop {
break
}
e.handleEvent()
}
}
func (e *editor) handleEvent() {
evt := e.scr.PollEvent()
log.Printf("handleEvent: received event of type %T", evt)
switch ev := evt.(type) {
case *tcell.EventResize:
width, height := ev.Size()
log.Printf("handleEvent: resize event: %dx%d", width, height)
return
case *tcell.EventKey:
log.Printf("handleEvent: key: %v rune = %d mod = %b", ev.Key(), ev.Rune(), ev.Modifiers())
for _, op := range e.ops {
if ev.Key() == op.Key {
op.Func()
return
}
}
if ev.Key() == tcell.KeyRune || ev.Key() == tcell.KeyTAB {
e.handleInput(ev.Rune())
}
}
}
func (e *editor) loadBufferFromFile(fn string) error {
if _, err := os.Stat(fn); err != nil {
if errors.Is(err, os.ErrNotExist) {
e.addNewBuffer()
e.bufs[len(e.bufs)-1].fname = fn
return nil
}
}
f, err := os.Open(fn)
if err != nil {
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
buf := &buffer{
fname: fn,
historyIdx: -1,
}
for scanner.Scan() {
buf.lines = append(buf.lines, []rune(scanner.Text()))
}
if len(buf.lines) == 0 {
buf.lines = [][]rune{{}}
}
e.bufs = append(e.bufs, buf)
return nil
}
func (e *editor) addNewBuffer() {
e.bufs = append(e.bufs, &buffer{
lines: [][]rune{{}},
historyIdx: -1,
})
}
func (e *editor) handleInput(r rune) {
log.Printf("handleInput: rune = %c", r)
curBuf := e.bufs[e.bufIdx]
lineIdx := curBuf.y + curBuf.offset
curLine := curBuf.lines[lineIdx]
if curBuf.x >= len(curLine) {
curLine = append(curLine, r)
} else {
curLine = append(curLine[:curBuf.x], append([]rune{r}, curLine[curBuf.x:]...)...)
}
curBuf.lines[lineIdx] = curLine
curBuf.historyAddRune(r)
curBuf.x++
curBuf.modified = true
}
func (e *editor) saveFile(curBuf *buffer) {
tmpName := filepath.Join(filepath.Dir(curBuf.fname), fmt.Sprintf(".tmp%x", time.Now().UnixNano()))
log.Printf("saveFile: saving buffer of %d lines to %s (temporary file: %s)", len(curBuf.lines), curBuf.fname, tmpName)
f, err := os.OpenFile(tmpName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
log.Printf("saveFile: opening temporary file %s failed: %v", tmpName, err)
e.showError("Failed to open temporary file: %v", err)
return
}
defer f.Close()
w := bufio.NewWriter(f)
for _, line := range curBuf.lines {
if _, err := w.WriteString(string(line) + "\n"); err != nil {
log.Printf("saveFile: writing to temporary file failed: %v", err)
e.showError("Failed to write to temporary file: %v", err)
return
}
}
if err := w.Flush(); err != nil {
log.Printf("saveFile: Flush failed: %v", err)
e.showError("Failed to write file content: %v", err)
return
}
if err := os.Rename(tmpName, curBuf.fname); err != nil {
log.Printf("saveFile: Rename failed: %v", err)
e.showError("Failed to replace %s with temporary file %s: %v", curBuf.fname, tmpName, err)
return
}
curBuf.modified = false
}
func (e *editor) updateSelectedTextPos(buf *buffer) {
if buf.selecting {
buf.endX, buf.endY = buf.x, buf.offset+buf.y
log.Printf("updateSelectedTextPos: new selection end point at %d/%d", buf.endY, buf.endX)
}
}
func (e *editor) showError(s string, args ...interface{}) {
str := fmt.Sprintf(s, args...)
width, height := e.scr.Size()
e.clearLine(height-1, width, tcell.StyleDefault)
x := 0
for _, r := range str {
e.scr.SetContent(x, height-1, r, nil, tcell.StyleDefault)
x += runewidth.RuneWidth(r)
}
}
func (e *editor) readString(prompt string, inputRunes []rune) (input string, ok bool) {
log.Printf("readString: prompt %q, initial text %q", prompt, string(inputRunes))
cursorPos := len(inputRunes)
defer func() {
width, height := e.scr.Size()
e.clearLine(height-1, width, tcell.StyleDefault)
}()
prompt += ": "
for {
width, height := e.scr.Size()
e.clearLine(height-1, width, tcell.StyleDefault)
promptStyle := tcell.StyleDefault.Bold(true)
x := 0
for _, r := range prompt {
e.scr.SetContent(x, height-1, r, nil, promptStyle)
x += runewidth.RuneWidth(r)
}
i := 0
inputx := x
cursorPosFound := false
for _, r := range inputRunes {
log.Printf("readString: i = %d cursorPos = %d r = %c", i, cursorPos, r)
if i == cursorPos {
log.Printf("readString: showing cursor at x = %d y = %d", inputx, height-1)
e.scr.ShowCursor(inputx, height-1)
cursorPosFound = true
}
e.scr.SetContent(inputx, height-1, r, nil, tcell.StyleDefault)
i++
inputx += runewidth.RuneWidth(r)
}
if !cursorPosFound {
e.scr.ShowCursor(inputx, height-1)
}
e.scr.Show()
evt := e.scr.PollEvent()
switch ev := evt.(type) {
case *tcell.EventResize:
e.redrawScreen()
continue
case *tcell.EventKey:
log.Printf("readString: %v rune = %d mod = %b", ev.Key(), ev.Rune(), ev.Modifiers())
switch ev.Key() {
case tcell.KeyCR:
s := string(inputRunes)
log.Printf("readString: returning %q", s)
return s, true
case tcell.KeyESC, tcell.KeyCtrlG:
log.Printf("readString: cancelled input")
return "", false
case tcell.KeyLeft:
if cursorPos > 0 {
cursorPos--
}
case tcell.KeyRight:
if cursorPos < len(inputRunes) {
cursorPos++
}
case tcell.KeyDEL: // backspace
if cursorPos > 0 {
inputRunes = append(inputRunes[:cursorPos-1], inputRunes[cursorPos:]...)
}
cursorPos--
case tcell.KeyDelete: // DEL
if cursorPos < len(inputRunes) {
inputRunes = append(inputRunes[:cursorPos], inputRunes[cursorPos+1:]...)
}
case tcell.KeyCtrlU:
inputRunes = inputRunes[cursorPos:]
cursorPos = 0
case tcell.KeyCtrlK:
inputRunes = inputRunes[:cursorPos]
case tcell.KeyCtrlA:
cursorPos = 0
case tcell.KeyCtrlE:
cursorPos = len(inputRunes)
case tcell.KeyRune:
log.Printf("readString: rune input: %[1]c (%[1]d)", ev.Rune())
inputRunes = append(inputRunes[:cursorPos], append([]rune{ev.Rune()}, inputRunes[cursorPos:]...)...)
cursorPos++
}
}
}
}
func (e *editor) query(prompt string, validAnswers string) rune {
log.Printf("query: prompt %q valid answers: %q", prompt, validAnswers)
defer func() {
width, height := e.scr.Size()
e.clearLine(height-1, width, tcell.StyleDefault)
}()
prompt += " [" + validAnswers + "]"
for {
width, height := e.scr.Size()
e.clearLine(height-1, width, tcell.StyleDefault)
promptStyle := tcell.StyleDefault.Bold(true)
x := 0
for _, r := range prompt {
e.scr.SetContent(x, height-1, r, nil, promptStyle)
x += runewidth.RuneWidth(r)
}
e.scr.ShowCursor(x, height-1)
e.scr.Show()
evt := e.scr.PollEvent()
switch ev := evt.(type) {
case *tcell.EventResize:
e.redrawScreen()
continue
case *tcell.EventKey:
if ev.Key() == tcell.KeyRune {
for _, r := range validAnswers {
if r == ev.Rune() {
log.Printf("query: returning %c", r)
return r
}
}
}
}
}
}
func (e *editor) redrawScreen() {
width, height := e.scr.Size()
log.Printf("redrawScreen: %dx%d", width, height)
curBuf := e.bufs[e.bufIdx]
for i := curBuf.offset; i < curBuf.offset+height-2; i++ {
e.drawLine(curBuf, i-curBuf.offset, i, width, i == curBuf.curLineIdx())
}
x := runeWidth(curBuf.curLine()[:curBuf.x])
e.scr.ShowCursor(x, curBuf.y)
e.drawStatus(height-2, width)
e.scr.Show()
e.clearLine(height-1, width, tcell.StyleDefault)
for i := 0; i < len(curBuf.lines); i++ {
log.Printf("redrawScreen: line %d = %q\n", i, string(curBuf.lines[i]))
}
}
func (e *editor) drawLine(buf *buffer, y int, lineIdx int, width int, curLine bool) {
if len(buf.lines) <= lineIdx {
e.scr.SetContent(0, y, '~', nil, tcell.StyleDefault.Bold(true))
for i := 1; i < width; i++ {
e.scr.SetContent(i, y, ' ', nil, tcell.StyleDefault)
}
return
}
line := buf.lines[lineIdx]
style := tcell.StyleDefault.Underline(curLine)
e.clearLine(y, width, style)
x := 0
for idx, r := range line {
if x >= width {
r = '$'
} else if r == '\t' {
r = ' '
x += tabWidth - 1
}
charStyle := style
if buf.isWithinSelectedText(lineIdx, idx) {
charStyle = charStyle.Background(tcell.ColorYellow).Foreground(tcell.ColorBlack)
}
e.scr.SetContent(x, y, r, nil, charStyle)
x += runewidth.RuneWidth(r)
}
}
func (e *editor) clearLine(y int, width int, style tcell.Style) {
for x := 0; x < width; x++ {
e.scr.SetContent(x, y, ' ', nil, style)
}
}
func (e *editor) drawStatus(y int, width int) {
curBuf := e.bufs[e.bufIdx]
status := ""
if curBuf.modified {
status += "* "
} else {
status += "- "
}
if curBuf.fname == "" {
status += "<no file> "
} else {
status += curBuf.fname + " "
}
status += fmt.Sprintf("(%d of %d) [%d|%d-%d] - Press Ctrl-H for Help", e.bufIdx+1, len(e.bufs), curBuf.curLineIdx(), curBuf.x, runeWidth(curBuf.curLine()[:curBuf.x]))
statusStyle := tcell.StyleDefault.Reverse(true)
e.clearLine(y, width, statusStyle)
x := 0
for _, r := range status {
e.scr.SetContent(x, y, r, nil, statusStyle)
x += runewidth.RuneWidth(r)
}
}