diff options
Diffstat (limited to 'selecting.go')
-rw-r--r-- | selecting.go | 53 |
1 files changed, 39 insertions, 14 deletions
diff --git a/selecting.go b/selecting.go index ddd821d..7eeae72 100644 --- a/selecting.go +++ b/selecting.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "strconv" "github.com/charmbracelet/bubbles/list" tea "github.com/charmbracelet/bubbletea" @@ -13,21 +12,19 @@ type selectingModel struct { } 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"}, + items, err := getItems() + if err != nil { + panic(err) } list := list.New(items, list.NewDefaultDelegate(), 0, 0) - list.Title = "Groceries" + list.Title = "Commands" - return selectingModel{ + m := selectingModel{ list: list, } + + return m } func (m selectingModel) Init() tea.Cmd { @@ -50,19 +47,43 @@ func (m selectingModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg.String() { - case "enter", " ": - return switchToConfirm(m) case "+", "n": - return m, newItem() + cmds = append(cmds, newItemCmd()) + case "enter", " ": + if m.list.SelectedItem() != nil { + return switchToConfirm(m) + } + case "e": + if m.list.SelectedItem() != nil { + cmds = append(cmds, editItemCmd(m.list.SelectedItem().(item))) + } + case "d": + if m.list.SelectedItem() != nil { + cmds = append(cmds, deleteItemCmd(m.list.SelectedItem().(item))) + } } - case VimFinishedMsg: + case vimFinishedMsg: if msg.err != nil { fmt.Println(msg.err) return m, tea.Quit } return switchToDetails(m, msg.snippet) + + case setListItemsMsg: + cmds = append(cmds, m.list.SetItems(msg.newItems)) + + case updateListMsg: + cmds = append(cmds, func() tea.Msg { + items, err := getItems() + if err != nil { + panic(err) + } + + return setListItemsMsg{items} + }) + } // This will also call our delegate's update function. @@ -76,3 +97,7 @@ func (m selectingModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m selectingModel) View() string { return appStyle.Render(m.list.View()) } + +type setListItemsMsg struct { + newItems []list.Item +} |