~delthas/senpai-dev

Add CTRL+K binding that kills text to the end of line v1 PROPOSED

Guilherme Puida Moreira: 1
 Add CTRL+K binding that kills text to the end of line

 4 files changed, 39 insertions(+), 0 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~delthas/senpai-dev/patches/53514/mbox | git am -3
Learn more about email & git

[PATCH v1] Add CTRL+K binding that kills text to the end of line Export this patch

This mimics the readline/emacs behavior.
---
 app.go            |  5 +++++
 ui/editor.go      | 12 ++++++++++++
 ui/editor_test.go | 18 ++++++++++++++++++
 ui/ui.go          |  4 ++++
 4 files changed, 39 insertions(+)

diff --git a/app.go b/app.go
index d9c62d5..aa30306 100644
--- a/app.go
+++ b/app.go
@@ -731,6 +731,11 @@ func (app *App) handleKeyEvent(ev *tcell.EventKey) {
		if ok {
			app.typing()
		}
	case tcell.KeyCtrlK:
		ok := app.win.InputKillLine()
		if ok {
			app.typing()
		}
	case tcell.KeyCtrlR:
		app.win.InputBackSearch()
	case tcell.KeyTab:
diff --git a/ui/editor.go b/ui/editor.go
index e1898ea..01e7cea 100644
--- a/ui/editor.go
+++ b/ui/editor.go
@@ -200,6 +200,18 @@ func (e *Editor) RemWord() (ok bool) {
	return
}

func (e *Editor) KillLine() (ok bool) {
	ok = e.RemRuneForward()
	if !ok {
		return
	}

	for e.RemRuneForward() {
	}

	return
}

func (e *Editor) Flush() string {
	content := string(e.text[e.lineIdx])
	if len(content) > 0 {
diff --git a/ui/editor_test.go b/ui/editor_test.go
index 30ea8fc..a5583ba 100644
--- a/ui/editor_test.go
+++ b/ui/editor_test.go
@@ -110,3 +110,21 @@ func TestLeftAndRem(t *testing.T) {
	e.PutRune('l')
	assertEditorEq(t, e, hell)
}

func TestKillLine(t *testing.T) {
	e := NewEditor(ConfigColors{}, nil)
	e.Resize(5)
	e.PutRune('h')
	e.PutRune('e')
	e.PutRune('l')
	e.PutRune('l')

	e.PutRune('o')
	e.PutRune('o')
	e.PutRune('o')
	e.left()
	e.left()
	e.left()
	e.KillLine()
	assertEditorEq(t, e, hell)
}
diff --git a/ui/ui.go b/ui/ui.go
index 801ee99..4c58aa8 100644
--- a/ui/ui.go
+++ b/ui/ui.go
@@ -485,6 +485,10 @@ func (ui *UI) InputDeleteWord() (ok bool) {
	return ui.e.RemWord()
}

func (ui *UI) InputKillLine() (ok bool) {
	return ui.e.KillLine()
}

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

base-commit: e64baed82e73284dc49a756742654d6a034b9b7f
-- 
2.45.2
I just realized that there was a previously rejected patch [1] that
also implemented this. The rejection mentions:
Consider this my request to implement C-k. I usually do C-a + C-k to
kill an entire line when using bash/zsh, so it would be nice to have
this in senpai as well.

By the way, I like the implementation in [1] better than mine. I also
forgot to update the man page (whoops).

[1]: https://lists.sr.ht/~delthas/senpai-dev/patches/32141

--puida