diff options
author | Hubert Hirtz <hubert@hirtzfr.eu> | 2020-08-05 15:17:52 +0200 |
---|---|---|
committer | Hubert Hirtz <hubert@hirtzfr.eu> | 2020-08-05 15:18:11 +0200 |
commit | 0b3c0c44100b17b31195f2e5891bd340192fb17d (patch) | |
tree | d592a906f80983bee1730d57e7f3c3f20e3f933e | |
parent | editor: handle HOME and END keys (diff) |
editor: handle DEL key
-rw-r--r-- | app.go | 5 | ||||
-rw-r--r-- | ui/editor.go | 24 | ||||
-rw-r--r-- | ui/ui.go | 8 |
3 files changed, 31 insertions, 6 deletions
@@ -228,6 +228,11 @@ func (app *App) handleUIEvent(ev tcell.Event) { if ok && app.win.InputLen() == 0 { app.s.TypingStop(app.win.CurrentBuffer()) } + case tcell.KeyDelete: + ok := app.win.InputDelete() + if ok && app.win.InputLen() == 0 { + app.s.TypingStop(app.win.CurrentBuffer()) + } case tcell.KeyCR, tcell.KeyLF: buffer := app.win.CurrentBuffer() input := app.win.InputEnter() diff --git a/ui/editor.go b/ui/editor.go index e78d8cc..2710e06 100644 --- a/ui/editor.go +++ b/ui/editor.go @@ -71,19 +71,31 @@ func (e *editor) RemRune() (ok bool) { if !ok { return } + e.remRuneAt(e.cursorIdx - 1) + e.Left() + return +} +func (e *editor) RemRuneForward() (ok bool) { + ok = e.cursorIdx < len(e.text) + if !ok { + return + } + e.remRuneAt(e.cursorIdx) + return +} + +func (e *editor) remRuneAt(idx int) { // TODO avoid looping twice - rw := e.textWidth[e.cursorIdx] - e.textWidth[e.cursorIdx-1] - for i := e.cursorIdx; i < len(e.textWidth); i++ { + rw := e.textWidth[idx+1] - e.textWidth[idx] + for i := idx + 1; i < len(e.textWidth); i++ { e.textWidth[i] -= rw } - copy(e.textWidth[e.cursorIdx:], e.textWidth[e.cursorIdx+1:]) + copy(e.textWidth[idx+1:], e.textWidth[idx+2:]) e.textWidth = e.textWidth[:len(e.textWidth)-1] - copy(e.text[e.cursorIdx-1:], e.text[e.cursorIdx:]) + copy(e.text[idx:], e.text[idx+1:]) e.text = e.text[:len(e.text)-1] - e.Left() - return } func (e *editor) Flush() (content string) { @@ -174,6 +174,14 @@ func (ui *UI) InputBackspace() (ok bool) { return } +func (ui *UI) InputDelete() (ok bool) { + ok = ui.e.RemRuneForward() + if ok { + ui.draw() + } + return +} + func (ui *UI) InputEnter() (content string) { content = ui.e.Flush() ui.draw() |