-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
69 lines (58 loc) · 1.43 KB
/
model.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
package main
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
)
type Model struct {
state state
lg *lipgloss.Renderer
styles *Styles
form *huh.Form
width int
color string
formType string
flux string
inOrbit string
isExo string
randomID int
generatedURL string
}
func (m *Model) Init() tea.Cmd {
m.generatedURL = m.GenerateUrl()
return m.form.Init()
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = min(msg.Width, maxWidth) - m.styles.Base.GetHorizontalFrameSize()
case tea.KeyMsg:
switch msg.String() {
case "esc", "ctrl+c", "q":
return &m, tea.Quit
}
}
var cmds []tea.Cmd
// Process the main form
if m.state == stateFormPage {
form, cmd := m.form.Update(msg)
if f, ok := form.(*huh.Form); ok {
m.form = f
cmds = append(cmds, cmd)
}
m.color = m.form.GetString("color")
m.formType = m.form.GetString("formType")
m.flux = m.form.GetString("flux")
m.inOrbit = m.form.GetString("inOrbit")
m.isExo = m.form.GetString("isExoplanet")
}
// If the form is completed, validate answers
if m.form.State == huh.StateCompleted && m.state == stateFormPage {
if m.ValidateAnswers() {
m.state = stateCorrectAnswer
} else {
m.state = stateWrongAnswer
}
}
return &m, tea.Batch(cmds...)
}