-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
150 lines (127 loc) · 2.89 KB
/
main.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"fmt"
"log"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type Styles struct {
BorderColor lipgloss.Color
InputField lipgloss.Style
}
func DefaultStyles() *Styles {
s := new(Styles)
s.BorderColor = lipgloss.Color("36")
s.InputField = lipgloss.NewStyle().BorderForeground(s.BorderColor).BorderStyle(lipgloss.NormalBorder()).Padding(1).Width(80)
return s
}
type model struct {
index int
questions []Question
width int
height int
styles *Styles
done bool
}
type Question struct {
question string
answer string
input Input
}
func NewQuestion(question string) Question {
return Question{question: question}
}
func newShortQuestion(question string) Question {
q := NewQuestion(question)
field := NewShortAnswerField()
q.input = field
return q
}
func newLongQuestion(question string) Question {
q := NewQuestion(question)
field := NewLongAnswerField()
q.input = field
return q
}
func New(questions []Question) *model {
styles := DefaultStyles()
return &model{
questions: questions,
styles: styles,
}
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
current := &m.questions[m.index]
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
return m, tea.Quit
case "enter":
if m.index == len(m.questions)-1 {
m.done = true
}
current.answer = current.input.Value()
log.Printf("question: %s, answer: %s", current.question, current.answer)
m.Next()
return m, current.input.Blur
}
}
current.input, cmd = current.input.Update(msg)
return m, cmd
}
func (m model) View() string {
current := m.questions[m.index]
if m.done {
var output string
for _, q := range m.questions {
output += fmt.Sprintf("%s: %s\n", q.question, q.answer)
}
return output
}
if m.width == 0 {
return "Waiting for size..."
}
return lipgloss.Place(
m.width,
m.height,
lipgloss.Center,
lipgloss.Center,
lipgloss.JoinVertical(
lipgloss.Center,
m.questions[m.index].question,
m.styles.InputField.Render(current.input.View()),
),
)
}
func (m *model) Next() {
if m.index < len(m.questions)-1 {
m.index++
} else {
m.index = 0
}
}
func main() {
questions := []Question{
newShortQuestion("What is your name?"),
newShortQuestion("How old are you?"),
newLongQuestion("What is your favorite color?"),
}
m := New(questions)
f, err := tea.LogToFile("debug.log", "debug")
if err != nil {
log.Fatal("err: %w", err)
}
defer f.Close()
p := tea.NewProgram(m, tea.WithAltScreen())
if _, err := p.Run(); err != nil {
log.Fatal("err: %w", err)
}
}