choice.go

 1package tui
 2
 3import (
 4	"fmt"
 5
 6	tea "github.com/charmbracelet/bubbletea"
 7	"github.com/charmbracelet/lipgloss"
 8)
 9
10var (
11	titleStyle = lipgloss.NewStyle().
12			Bold(true).
13			Foreground(lipgloss.Color("#FAFAFA")).
14			Background(lipgloss.Color("#7D56F4")).
15			PaddingLeft(2).
16			PaddingRight(2)
17
18	choiceStyle = lipgloss.NewStyle().
19			Foreground(lipgloss.Color("205")).
20			PaddingLeft(2)
21)
22
23type Choice struct {
24	cursor int
25}
26
27func NewChoice() Choice {
28	return Choice{cursor: 0}
29}
30
31func (m Choice) Init() tea.Cmd {
32	return nil
33}
34
35func (m Choice) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
36	switch msg := msg.(type) {
37	case tea.KeyMsg:
38		switch msg.String() {
39		case "up", "k":
40			if m.cursor > 0 {
41				m.cursor--
42			}
43		case "down", "j":
44			if m.cursor < 1 {
45				m.cursor++
46			}
47		case "enter":
48			if m.cursor == 0 {
49				return m, func() tea.Msg { return GoToInboxMsg{} }
50			} else if m.cursor == 1 {
51				return m, func() tea.Msg { return GoToSendMsg{} }
52			}
53		}
54	}
55	return m, nil
56}
57
58func (m Choice) View() string {
59	s := "What would you like to do?\n\n"
60
61	choices := []string{"View Inbox", "Compose Email"}
62
63	for i, choice := range choices {
64		cursor := " "
65		if m.cursor == i {
66			cursor = ">"
67			s += choiceStyle.Render(fmt.Sprintf("%s %s\n", cursor, choice))
68		} else {
69			s += fmt.Sprintf("%s %s\n", cursor, choice)
70		}
71	}
72
73	s += "\nPress q to quit.\n"
74
75	return s
76}