commit 0b3c0c4
Hubert Hirtz
·
2020-08-05 13:17:52 +0000 UTC
parent b54af7e
editor: handle DEL key
3 files changed,
+31,
-6
M
app.go
+5,
-0
1@@ -228,6 +228,11 @@ func (app *App) handleUIEvent(ev tcell.Event) {
2 if ok && app.win.InputLen() == 0 {
3 app.s.TypingStop(app.win.CurrentBuffer())
4 }
5+ case tcell.KeyDelete:
6+ ok := app.win.InputDelete()
7+ if ok && app.win.InputLen() == 0 {
8+ app.s.TypingStop(app.win.CurrentBuffer())
9+ }
10 case tcell.KeyCR, tcell.KeyLF:
11 buffer := app.win.CurrentBuffer()
12 input := app.win.InputEnter()
+18,
-6
1@@ -71,19 +71,31 @@ func (e *editor) RemRune() (ok bool) {
2 if !ok {
3 return
4 }
5+ e.remRuneAt(e.cursorIdx - 1)
6+ e.Left()
7+ return
8+}
9
10+func (e *editor) RemRuneForward() (ok bool) {
11+ ok = e.cursorIdx < len(e.text)
12+ if !ok {
13+ return
14+ }
15+ e.remRuneAt(e.cursorIdx)
16+ return
17+}
18+
19+func (e *editor) remRuneAt(idx int) {
20 // TODO avoid looping twice
21- rw := e.textWidth[e.cursorIdx] - e.textWidth[e.cursorIdx-1]
22- for i := e.cursorIdx; i < len(e.textWidth); i++ {
23+ rw := e.textWidth[idx+1] - e.textWidth[idx]
24+ for i := idx + 1; i < len(e.textWidth); i++ {
25 e.textWidth[i] -= rw
26 }
27- copy(e.textWidth[e.cursorIdx:], e.textWidth[e.cursorIdx+1:])
28+ copy(e.textWidth[idx+1:], e.textWidth[idx+2:])
29 e.textWidth = e.textWidth[:len(e.textWidth)-1]
30
31- copy(e.text[e.cursorIdx-1:], e.text[e.cursorIdx:])
32+ copy(e.text[idx:], e.text[idx+1:])
33 e.text = e.text[:len(e.text)-1]
34- e.Left()
35- return
36 }
37
38 func (e *editor) Flush() (content string) {
M
ui/ui.go
+8,
-0
1@@ -174,6 +174,14 @@ func (ui *UI) InputBackspace() (ok bool) {
2 return
3 }
4
5+func (ui *UI) InputDelete() (ok bool) {
6+ ok = ui.e.RemRuneForward()
7+ if ok {
8+ ui.draw()
9+ }
10+ return
11+}
12+
13 func (ui *UI) InputEnter() (content string) {
14 content = ui.e.Flush()
15 ui.draw()