From 9449cc7e415de25b2c405765290eacbe2385078b Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Mon, 26 Apr 2021 11:28:37 -0400 Subject: [PATCH] Don't allow word-based deletion when input is masked in textinput alt+d and ctrl+w will now delete all the way to the beginning and end, respectively, if EchoMode is not EchoNormal. --- textinput/textinput.go | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/textinput/textinput.go b/textinput/textinput.go index 12d7957..fa757aa 100644 --- a/textinput/textinput.go +++ b/textinput/textinput.go @@ -311,6 +311,22 @@ func (m *Model) handleOverflow() { } } +// deleteBeforeCursor deletes all text before the cursor. Returns whether or +// not the cursor blink should be reset. +func (m *Model) deleteBeforeCursor() bool { + m.value = m.value[m.pos:] + m.offset = 0 + return m.setCursor(0) +} + +// deleteAfterCursor deletes all text after the cursor. Returns whether or not +// the cursor blink should be reset. If input is masked delete everything after +// the cursor so as not to reveal word breaks in the masked input. +func (m *Model) deleteAfterCursor() bool { + m.value = m.value[:m.pos] + return m.setCursor(len(m.value)) +} + // deleteWordLeft deletes the word left to the cursor. Returns whether or not // the cursor blink should be reset. func (m *Model) deleteWordLeft() bool { @@ -318,6 +334,10 @@ func (m *Model) deleteWordLeft() bool { return false } + if m.EchoMode != EchoNormal { + return m.deleteBeforeCursor() + } + i := m.pos blink := m.setCursor(m.pos - 1) for unicode.IsSpace(m.value[m.pos]) { @@ -347,12 +367,17 @@ func (m *Model) deleteWordLeft() bool { } // deleteWordRight deletes the word right to the cursor. Returns whether or not -// the cursor blink should be reset. +// the cursor blink should be reset. If input is masked delete everything after +// the cursor so as not to reveal word breaks in the masked input. func (m *Model) deleteWordRight() bool { if m.pos >= len(m.value) || len(m.value) == 0 { return false } + if m.EchoMode != EchoNormal { + return m.deleteAfterCursor() + } + i := m.pos m.setCursor(m.pos + 1) for unicode.IsSpace(m.value[m.pos]) { @@ -509,12 +534,9 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { case tea.KeyCtrlE, tea.KeyEnd: // ^E, go to end resetBlink = m.cursorEnd() case tea.KeyCtrlK: // ^K, kill text after cursor - m.value = m.value[:m.pos] - resetBlink = m.setCursor(len(m.value)) + resetBlink = m.deleteAfterCursor() case tea.KeyCtrlU: // ^U, kill text before cursor - m.value = m.value[m.pos:] - resetBlink = m.setCursor(0) - m.offset = 0 + resetBlink = m.deleteBeforeCursor() case tea.KeyCtrlV: // ^V paste return m, Paste case tea.KeyRunes: // input regular characters