Update is the Bubble Tea update loop.
(msg tea.Msg)
| 562 | |
| 563 | // Update is the Bubble Tea update loop. |
| 564 | func (m TextInputModel) Update(msg tea.Msg) (TextInputModel, tea.Cmd) { |
| 565 | if !m.Focus { |
| 566 | m.blink = true |
| 567 | return m, nil |
| 568 | } |
| 569 | |
| 570 | var resetBlink bool |
| 571 | |
| 572 | switch msg := msg.(type) { |
| 573 | case tea.KeyMsg: |
| 574 | switch msg.Type { |
| 575 | case tea.KeyBackspace: // delete character before cursor |
| 576 | if msg.Alt { |
| 577 | resetBlink = m.deleteWordLeft() |
| 578 | } else { |
| 579 | if len(m.value) > 0 { |
| 580 | m.value = append(m.value[:max(0, m.pos-1)], m.value[m.pos:]...) |
| 581 | if m.pos > 0 { |
| 582 | resetBlink = m.setCursor(m.pos - 1) |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | case tea.KeyLeft, tea.KeyCtrlB: |
| 587 | if msg.Alt { // alt+left arrow, back one word |
| 588 | resetBlink = m.wordLeft() |
| 589 | break |
| 590 | } |
| 591 | if m.pos > 0 { // left arrow, ^F, back one character |
| 592 | resetBlink = m.setCursor(m.pos - 1) |
| 593 | } |
| 594 | case tea.KeyRight, tea.KeyCtrlF: |
| 595 | if msg.Alt { // alt+right arrow, forward one word |
| 596 | resetBlink = m.wordRight() |
| 597 | break |
| 598 | } |
| 599 | if m.pos < len(m.value) { // right arrow, ^F, forward one character |
| 600 | resetBlink = m.setCursor(m.pos + 1) |
| 601 | } |
| 602 | case tea.KeyCtrlW: // ^W, delete word left of cursor |
| 603 | resetBlink = m.deleteWordLeft() |
| 604 | case tea.KeyHome, tea.KeyCtrlA: // ^A, go to beginning |
| 605 | resetBlink = m.cursorStart() |
| 606 | case tea.KeyDelete, tea.KeyCtrlD: // ^D, delete char under cursor |
| 607 | if len(m.value) > 0 && m.pos < len(m.value) { |
| 608 | m.value = append(m.value[:m.pos], m.value[m.pos+1:]...) |
| 609 | } |
| 610 | case tea.KeyCtrlE, tea.KeyEnd: // ^E, go to end |
| 611 | resetBlink = m.cursorEnd() |
| 612 | case tea.KeyCtrlK: // ^K, kill text after cursor |
| 613 | resetBlink = m.deleteAfterCursor() |
| 614 | case tea.KeyCtrlU: // ^U, kill text before cursor |
| 615 | resetBlink = m.deleteBeforeCursor() |
| 616 | case tea.KeyCtrlV: // ^V paste |
| 617 | return m, Paste |
| 618 | case tea.KeyRunes: // input regular characters |
| 619 | if msg.Alt && len(msg.Runes) == 1 { |
| 620 | if msg.Runes[0] == 'd' { // alt+d, delete word right of cursor |
| 621 | resetBlink = m.deleteWordRight() |
nothing calls this directly
no test coverage detected