| 53 | func (m chatInputModel) Init() tea.Cmd { return textinput.Blink } |
| 54 | |
| 55 | func (m chatInputModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 56 | switch msg := msg.(type) { |
| 57 | case tea.WindowSizeMsg: |
| 58 | m.width = msg.Width |
| 59 | m.ti.Width = max(msg.Width-6, 20) |
| 60 | case tea.KeyMsg: |
| 61 | switch msg.Type { |
| 62 | case tea.KeyEnter: |
| 63 | m.submitted = m.ti.Value() |
| 64 | m.done = true |
| 65 | return m, tea.Quit |
| 66 | case tea.KeyCtrlD: |
| 67 | if m.ti.Value() == "" { |
| 68 | return m, tea.Quit |
| 69 | } |
| 70 | case tea.KeyCtrlC: |
| 71 | m.ti.SetValue("") |
| 72 | m.histIdx = -1 |
| 73 | return m, nil |
| 74 | case tea.KeyUp: |
| 75 | if len(m.history) == 0 { |
| 76 | return m, nil |
| 77 | } |
| 78 | if m.histIdx == -1 { |
| 79 | m.bufferText = m.ti.Value() |
| 80 | m.histIdx = 0 |
| 81 | } else if m.histIdx < len(m.history)-1 { |
| 82 | m.histIdx++ |
| 83 | } |
| 84 | m.ti.SetValue(m.history[len(m.history)-1-m.histIdx]) |
| 85 | m.ti.CursorEnd() |
| 86 | return m, nil |
| 87 | case tea.KeyDown: |
| 88 | if m.histIdx == -1 { |
| 89 | return m, nil |
| 90 | } |
| 91 | if m.histIdx == 0 { |
| 92 | m.histIdx = -1 |
| 93 | m.ti.SetValue(m.bufferText) |
| 94 | } else { |
| 95 | m.histIdx-- |
| 96 | m.ti.SetValue(m.history[len(m.history)-1-m.histIdx]) |
| 97 | } |
| 98 | m.ti.CursorEnd() |
| 99 | return m, nil |
| 100 | } |
| 101 | } |
| 102 | var cmd tea.Cmd |
| 103 | m.ti, cmd = m.ti.Update(msg) |
| 104 | return m, cmd |
| 105 | } |
| 106 | |
| 107 | func (m chatInputModel) View() string { |
| 108 | w := m.width - 2 |