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		}
48	}
49	return m, nil
50}
51
52func (m Choice) View() string {
53	s := "What would you like to do?\n\n"
54
55	choices := []string{"View Inbox", "Compose Email"}
56
57	for i, choice := range choices {
58		cursor := " "
59		if m.cursor == i {
60			cursor = ">"
61			s += choiceStyle.Render(fmt.Sprintf("%s %s\n", cursor, choice))
62		} else {
63			s += fmt.Sprintf("%s %s\n", cursor, choice)
64		}
65	}
66
67	s += "\nPress q to quit.\n"
68
69	return s
70}