From 0b3c0c44100b17b31195f2e5891bd340192fb17d Mon Sep 17 00:00:00 2001 From: Hubert Hirtz Date: Wed, 5 Aug 2020 15:17:52 +0200 Subject: editor: handle DEL key --- app.go | 5 +++++ ui/editor.go | 24 ++++++++++++++++++------ ui/ui.go | 8 ++++++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/app.go b/app.go index e36c704..5bb77f9 100644 --- a/app.go +++ b/app.go @@ -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) { diff --git a/ui/ui.go b/ui/ui.go index 8cbfea1..9ea1381 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -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() -- cgit v1.2.3