-
Notifications
You must be signed in to change notification settings - Fork 0
/
mm.go
111 lines (89 loc) · 2.43 KB
/
mm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
// A simple example that shows how to retrieve a value from a Bubble Tea
// program after the Bubble Tea has exited.
import (
"fmt"
"os"
"strings"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var choices = []string{"A ... An actress or actor appeared in all of these movies", "B ... Same director", "C ... Released in the same year"}
type model struct {
cursor int
choice string
list list.Model
}
func (m model) Init() tea.Cmd {
return nil
}
var docStyle = lipgloss.NewStyle().Margin(1, 2)
type item struct {
title, desc string
}
func (i item) Title() string { return i.title }
func (i item) Description() string { return i.desc }
func (i item) FilterValue() string { return i.title }
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q", "esc":
return m, tea.Quit
case "enter":
// Send the choice on the channel and exit.
m.choice = choices[m.cursor]
return m, tea.Quit
case "down", "j":
m.cursor++
if m.cursor >= len(choices) {
m.cursor = 0
}
case "up", "k":
m.cursor--
if m.cursor < 0 {
m.cursor = len(choices) - 1
}
}
}
return m, nil
}
func (m model) View() string {
s := strings.Builder{}
s.WriteString("Let me show you three movies that have a thing in common!\n\n")
s.WriteString(docStyle.Render(m.list.View())) // TODO show movies as list
s.WriteString("What do these movies have in common?\n\n")
for i := 0; i < len(choices); i++ {
if m.cursor == i {
s.WriteString("(•) ")
} else {
s.WriteString("( ) ")
}
s.WriteString(choices[i])
s.WriteString("\n")
}
s.WriteString("\n(press q to quit)\n")
return s.String()
}
func main() {
items := []list.Item{
item{title: "Alien 1", desc: "scary"},
item{title: "My best friends wedding", desc: "what the"},
item{title: "Pretty Woman", desc: "oh Julia"},
}
p := tea.NewProgram(model{list: list.New(items, list.NewDefaultDelegate(), 0, 0)})
// Run returns the model as a tea.Model.
m, err := p.Run()
if err != nil {
fmt.Println("Oh no:", err)
os.Exit(1)
}
// Assert the final tea.Model to our local model and print the choice.
if m, ok := m.(model); ok && m.choice != "" {
fmt.Printf("\n---\nYou chose %s!\n", m.choice)
}
}