~taiite/senpai

a7f7c5ad40a3f6960ba61e67f737ddef56408d6e — Alexey Yerin 1 year, 2 months ago 5e2bbb5
editor: add ^W delete word binding

It allows to delete the current word (any string of characters until a
space). Also, all spaces at the start are cleared to allow doing
something like:

    Hello world|
    Hello | <- the cursor is at a space
    | <- nothing left
3 files changed, 40 insertions(+), 0 deletions(-)

M app.go
M ui/editor.go
M ui/ui.go
M app.go => app.go +6 -0
@@ 448,6 448,12 @@ func (app *App) handleKeyEvent(ev *tcell.EventKey) {
			app.typing()
			app.updatePrompt()
		}
	case tcell.KeyCtrlW:
		ok := app.win.InputDeleteWord()
		if ok {
			app.typing()
			app.updatePrompt()
		}
	case tcell.KeyTab:
		ok := app.win.InputAutoComplete(1)
		if ok {

M ui/editor.go => ui/editor.go +30 -0
@@ 116,6 116,36 @@ func (e *Editor) remRuneAt(idx int) {
	e.text[e.lineIdx] = e.text[e.lineIdx][:len(e.text[e.lineIdx])-1]
}

func (e *Editor) RemWord() (ok bool) {
	ok = 0 < e.cursorIdx
	if !ok {
		return
	}

	line := e.text[e.lineIdx]

	// To allow doing something like this (| is the cursor):
	// Hello world|
	// Hello |
	// |
	for line[e.cursorIdx - 1] == ' ' {
		e.remRuneAt(e.cursorIdx - 1)
		e.Left()
	}

	for i := e.cursorIdx - 1; i >= 0; i -= 1 {
		if line[i] == ' ' {
			break
		}
		e.remRuneAt(i)
		e.Left()
	}

	e.autoCache = nil
	return
}


func (e *Editor) Flush() (content string) {
	content = string(e.text[e.lineIdx])
	if len(e.text[len(e.text)-1]) == 0 {

M ui/ui.go => ui/ui.go +4 -0
@@ 192,6 192,10 @@ func (ui *UI) InputDelete() (ok bool) {
	return ui.e.RemRuneForward()
}

func (ui *UI) InputDeleteWord() (ok bool) {
	return ui.e.RemWord()
}

func (ui *UI) InputAutoComplete(offset int) (ok bool) {
	return ui.e.AutoComplete(offset)
}