choice.go

 1package tui
 2
 3import (
 4	"github.com/charmbracelet/bubbles/list"
 5	tea "github.com/charmbracelet/bubbletea"
 6)
 7
 8type Choice struct {
 9	list list.Model
10}
11
12func NewChoice() *Choice {
13	items := []list.Item{
14		choiceItem("View Inbox"),
15		choiceItem("Send Email"),
16	}
17
18	l := list.New(items, list.NewDefaultDelegate(), 0, 0)
19	l.Title = "What would you like to do?"
20	l.SetShowStatusBar(false)
21	l.SetFilteringEnabled(false)
22
23	return &Choice{list: l}
24}
25
26type choiceItem string
27
28func (i choiceItem) FilterValue() string { return "" }
29func (i choiceItem) Title() string       { return string(i) }
30func (i choiceItem) Description() string { return "" }
31
32func (m *Choice) Init() tea.Cmd {
33	return nil
34}
35
36func (m *Choice) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
37	var cmd tea.Cmd
38	switch msg := msg.(type) {
39	case tea.WindowSizeMsg:
40		m.list.SetSize(msg.Width, msg.Height)
41	case tea.KeyMsg:
42		if msg.String() == "enter" {
43			switch m.list.SelectedItem().(choiceItem) {
44			case "View Inbox":
45				return m, func() tea.Msg { return GoToInboxMsg{} }
46			case "Send Email":
47				return m, func() tea.Msg { return GoToSendMsg{} }
48			}
49		}
50	}
51	m.list, cmd = m.list.Update(msg)
52	return m, cmd
53}
54
55func (m *Choice) View() string {
56	return DocStyle.Render(m.list.View())
57}