From 58a177394ee46d2a1a55b4f58369c51cefcb0c9d Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Mon, 26 Apr 2021 11:14:25 -0400 Subject: [PATCH] Don't allow word-to-word movement when input is masked in textinput When EchoMode is not set to EchoNormal, alt+left/alt+b and alt+right/alt+b jumps to the beginning and the end, respectively, so as not to reveal word breaks in the masked text. --- textinput/textinput.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/textinput/textinput.go b/textinput/textinput.go index 85c5ecf..12d7957 100644 --- a/textinput/textinput.go +++ b/textinput/textinput.go @@ -378,12 +378,17 @@ func (m *Model) deleteWordRight() bool { } // wordLeft moves the cursor one word to the left. Returns whether or not the -// cursor blink should be reset. +// cursor blink should be reset. If input is masked, move input to the start +// so as not to reveal word breaks in the masked input. func (m *Model) wordLeft() bool { if m.pos == 0 || len(m.value) == 0 { return false } + if m.EchoMode != EchoNormal { + return m.cursorStart() + } + blink := false i := m.pos - 1 for i >= 0 { @@ -408,12 +413,17 @@ func (m *Model) wordLeft() bool { } // wordRight moves the cursor one word to the right. Returns whether or not the -// cursor blink should be reset. +// cursor blink should be reset. If the input is masked, move input to the end +// so as not to reveal word breaks in the masked input. func (m *Model) wordRight() bool { if m.pos >= len(m.value) || len(m.value) == 0 { return false } + if m.EchoMode != EchoNormal { + return m.cursorEnd() + } + blink := false i := m.pos for i < len(m.value) { @@ -485,7 +495,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { resetBlink = m.wordRight() break } - if m.pos < len(m.value) { // right arrow, ^F, forward one word + if m.pos < len(m.value) { // right arrow, ^F, forward one character resetBlink = m.setCursor(m.pos + 1) } case tea.KeyCtrlW: // ^W, delete word left of cursor