Skip to content

Commit

Permalink
Don't allow word-based deletion when input is masked in textinput
Browse files Browse the repository at this point in the history
alt+d and ctrl+w will now delete all the way to the beginning and end,
respectively, if EchoMode is not EchoNormal.
  • Loading branch information
meowgorithm committed Apr 26, 2021
1 parent 58a1773 commit 9449cc7
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions textinput/textinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,33 @@ 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 {
if m.pos == 0 || len(m.value) == 0 {
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]) {
Expand Down Expand Up @@ -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]) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9449cc7

Please sign in to comment.