| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "github.com/charmbracelet/bubbles/textarea" |
| 5 | tea "github.com/charmbracelet/bubbletea" |
| 6 | ) |
| 7 | |
| 8 | type interactiveInputModel struct { |
| 9 | sharedData *interactiveSharedData |
| 10 | inputModel textarea.Model |
| 11 | } |
| 12 | |
| 13 | func newInteractiveInputModel(sharedData *interactiveSharedData) *interactiveInputModel { |
| 14 | ti := textarea.New() |
| 15 | ti.Placeholder = "Enter a query..." |
| 16 | ti.SetValue(sharedData.selector) |
| 17 | ti.Focus() |
| 18 | ti.SetHeight(5) |
| 19 | ti.ShowLineNumbers = false |
| 20 | |
| 21 | return &interactiveInputModel{ |
| 22 | sharedData: sharedData, |
| 23 | inputModel: ti, |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func (m *interactiveInputModel) Init() tea.Cmd { |
| 28 | return textarea.Blink |
| 29 | } |
| 30 | |
| 31 | func (m *interactiveInputModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 32 | var cmds []tea.Cmd |
| 33 | var cmd tea.Cmd |
| 34 | |
| 35 | m.sharedData.selector = m.inputModel.Value() |
| 36 | |
| 37 | switch msg := msg.(type) { |
| 38 | case tea.WindowSizeMsg: |
| 39 | m.inputModel.SetWidth(msg.Width) |
| 40 | } |
| 41 | |
| 42 | m.inputModel, cmd = m.inputModel.Update(msg) |
| 43 | cmds = append(cmds, cmd) |
| 44 | |
| 45 | return m, tea.Batch(cmds...) |
| 46 | } |
| 47 | |
| 48 | func (m *interactiveInputModel) View() string { |
| 49 | return m.inputModel.View() |
| 50 | } |