package main import ( "fmt" "strconv" "github.com/charmbracelet/bubbles/list" tea "github.com/charmbracelet/bubbletea" ) type selectingModel struct { list list.Model } func StartSelecting() selectingModel { var s string for i := 0; i < 100; i++ { s += "echo " + strconv.Itoa(i) + "\n" } items := []list.Item{ item{title: "Test command", contents: s}, item{title: "vim", contents: "vim"}, } list := list.New(items, list.NewDefaultDelegate(), 0, 0) list.Title = "Groceries" return selectingModel{ list: list, } } func (m selectingModel) Init() tea.Cmd { return tea.EnterAltScreen } func (m selectingModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmds []tea.Cmd switch msg := msg.(type) { case tea.WindowSizeMsg: h, v := appStyle.GetFrameSize() m.list.SetSize(msg.Width-h, msg.Height-v) case tea.KeyMsg: // Don't match any of the keys below if we're actively filtering. if m.list.FilterState() == list.Filtering { break } switch msg.String() { case "enter", " ": return switchToConfirm(m) case "+", "n": return m, newItem() } case VimFinishedMsg: if msg.err != nil { fmt.Println(msg.err) return m, tea.Quit } return switchToDetails(m, msg.snippet) } // This will also call our delegate's update function. newListModel, cmd := m.list.Update(msg) m.list = newListModel cmds = append(cmds, cmd) return m, tea.Batch(cmds...) } func (m selectingModel) View() string { return appStyle.Render(m.list.View()) }