add readline shortcuts to textbox (ctrl+w, ctrl+u, ctrl+k)

This commit is contained in:
Username
2026-02-24 10:02:09 +01:00
parent c95736b632
commit 41f69052b5

View File

@@ -63,9 +63,12 @@ func (t *Textbox) uiDraw() {
func (t *Textbox) uiKeyEvent(mod Modifier, key Key) { func (t *Textbox) uiKeyEvent(mod Modifier, key Key) {
redraw := false redraw := false
switch key { switch key {
case KeyCtrlC: case KeyCtrlC, KeyCtrlU, KeyCtrlK:
t.Text = "" t.Text = ""
redraw = true redraw = true
case KeyCtrlW:
t.Text = deleteWord(t.Text)
redraw = true
case KeyEnter: case KeyEnter:
if t.Input != nil { if t.Input != nil {
t.Input(t.ui, t, t.Text) t.Input(t.ui, t, t.Text)
@@ -91,6 +94,16 @@ func (t *Textbox) uiKeyEvent(mod Modifier, key Key) {
} }
} }
func deleteWord(s string) string {
// Trim trailing spaces, then trim non-spaces (the word)
s = strings.TrimRight(s, " ")
i := strings.LastIndex(s, " ")
if i < 0 {
return ""
}
return s[:i+1]
}
func (t *Textbox) uiCharacterEvent(chr rune) { func (t *Textbox) uiCharacterEvent(chr rune) {
if len(t.Text) >= maxTextboxLen { if len(t.Text) >= maxTextboxLen {
return return