Skip to content

Commit 9449cc7

Browse files
committed
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.
1 parent 58a1773 commit 9449cc7

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

textinput/textinput.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,33 @@ func (m *Model) handleOverflow() {
311311
}
312312
}
313313

314+
// deleteBeforeCursor deletes all text before the cursor. Returns whether or
315+
// not the cursor blink should be reset.
316+
func (m *Model) deleteBeforeCursor() bool {
317+
m.value = m.value[m.pos:]
318+
m.offset = 0
319+
return m.setCursor(0)
320+
}
321+
322+
// deleteAfterCursor deletes all text after the cursor. Returns whether or not
323+
// the cursor blink should be reset. If input is masked delete everything after
324+
// the cursor so as not to reveal word breaks in the masked input.
325+
func (m *Model) deleteAfterCursor() bool {
326+
m.value = m.value[:m.pos]
327+
return m.setCursor(len(m.value))
328+
}
329+
314330
// deleteWordLeft deletes the word left to the cursor. Returns whether or not
315331
// the cursor blink should be reset.
316332
func (m *Model) deleteWordLeft() bool {
317333
if m.pos == 0 || len(m.value) == 0 {
318334
return false
319335
}
320336

337+
if m.EchoMode != EchoNormal {
338+
return m.deleteBeforeCursor()
339+
}
340+
321341
i := m.pos
322342
blink := m.setCursor(m.pos - 1)
323343
for unicode.IsSpace(m.value[m.pos]) {
@@ -347,12 +367,17 @@ func (m *Model) deleteWordLeft() bool {
347367
}
348368

349369
// deleteWordRight deletes the word right to the cursor. Returns whether or not
350-
// the cursor blink should be reset.
370+
// the cursor blink should be reset. If input is masked delete everything after
371+
// the cursor so as not to reveal word breaks in the masked input.
351372
func (m *Model) deleteWordRight() bool {
352373
if m.pos >= len(m.value) || len(m.value) == 0 {
353374
return false
354375
}
355376

377+
if m.EchoMode != EchoNormal {
378+
return m.deleteAfterCursor()
379+
}
380+
356381
i := m.pos
357382
m.setCursor(m.pos + 1)
358383
for unicode.IsSpace(m.value[m.pos]) {
@@ -509,12 +534,9 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
509534
case tea.KeyCtrlE, tea.KeyEnd: // ^E, go to end
510535
resetBlink = m.cursorEnd()
511536
case tea.KeyCtrlK: // ^K, kill text after cursor
512-
m.value = m.value[:m.pos]
513-
resetBlink = m.setCursor(len(m.value))
537+
resetBlink = m.deleteAfterCursor()
514538
case tea.KeyCtrlU: // ^U, kill text before cursor
515-
m.value = m.value[m.pos:]
516-
resetBlink = m.setCursor(0)
517-
m.offset = 0
539+
resetBlink = m.deleteBeforeCursor()
518540
case tea.KeyCtrlV: // ^V paste
519541
return m, Paste
520542
case tea.KeyRunes: // input regular characters

0 commit comments

Comments
 (0)