From 10022c964cdf003d1e52ebf8acacfef2fd573013 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Tue, 21 Jul 2020 18:22:18 -0400 Subject: [PATCH] Fix slice out of bounds errors that could happen when deleting text --- textinput/textinput.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/textinput/textinput.go b/textinput/textinput.go index 66cec33..769abf6 100644 --- a/textinput/textinput.go +++ b/textinput/textinput.go @@ -282,8 +282,10 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) { fallthrough case tea.KeyDelete: if len(m.value) > 0 { - m.value = append(m.value[:m.pos-1], m.value[m.pos:]...) - m.pos-- + m.value = append(m.value[:max(0, m.pos-1)], m.value[m.pos:]...) + if m.pos > 0 { + m.pos-- + } } case tea.KeyLeft: if msg.Alt { // alt+left arrow, back one word @@ -366,7 +368,7 @@ func View(model tea.Model) string { } value := m.value[m.offset:m.offsetRight] - pos := m.pos - m.offset + pos := max(0, m.pos-m.offset) v := m.colorText(string(value[:pos]))