-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
246 lines (203 loc) · 4.59 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"fmt"
"log"
"os"
"strconv"
"time"
"github.com/charmbracelet/bubbles/spinner"
input "github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/muesli/termenv"
"github.com/Beartime234/babble"
)
const usage = `kboard [number] [time]
number: the number of words to generate. Must be a non-zero positive integer.
defaults to 1 word.
time: the number of seconds that the game will last.
If none is passed, tha game finishes after the first word.
Examples:
- kboard 2
- kboard 1 30`
var term = termenv.ColorProfile()
func main() {
if len(os.Args) < 2 || len(os.Args) > 3 {
fmt.Println(usage)
os.Exit(1)
}
if os.Args[1] == "-h" || os.Args[1] == "--help" {
fmt.Println(usage)
os.Exit(0)
}
numOfWords, err := strconv.Atoi(os.Args[1])
if err != nil {
log.Fatal(err)
os.Exit(2)
}
if numOfWords < 1 {
fmt.Println(usage)
os.Exit(1)
}
var duration int
if len(os.Args) == 3 {
duration, err = strconv.Atoi(os.Args[2])
if err != nil {
log.Fatal(err)
os.Exit(2)
}
if duration < 1 {
fmt.Println(usage)
os.Exit(1)
}
}
p := tea.NewProgram(initialModel(numOfWords, duration))
if err := p.Start(); err != nil {
log.Fatal(err)
}
}
type model struct {
babbler babble.Babbler
textInput input.Model
spinner spinner.Model
currentWord string
status string
startTime time.Time
duration time.Duration
timeLeft time.Duration
points int
done bool
timeMode bool
}
func initialModel(numOfWords, duration int) model {
b := babble.NewBabbler()
b.Separator = " "
b.Count = numOfWords
i := input.NewModel()
if numOfWords == 1 {
i.Placeholder = "Type the word above and press Space or Enter 👆"
} else {
i.Placeholder = "Type the word above and press Enter 👆"
}
i.Focus()
s := spinner.NewModel()
s.Spinner = spinner.Dot
d := time.Duration(duration) * time.Second
return model{
babbler: b,
currentWord: b.Babble(),
textInput: i,
spinner: s,
startTime: time.Now(),
duration: d,
timeLeft: d,
timeMode: duration > 0,
}
}
type newWordMsg struct{}
type countdownMsg struct {
time time.Time
}
func newWord() tea.Cmd {
return func() tea.Msg {
return newWordMsg{}
}
}
func countDown() tea.Cmd {
return tea.Tick(time.Second, func(t time.Time) tea.Msg {
return countdownMsg{
time: t,
}
})
}
func (m model) Init() tea.Cmd {
cmds := []tea.Cmd{
input.Blink,
spinner.Tick,
}
if m.timeMode {
cmds = append(cmds, countDown())
}
return tea.Batch(cmds...)
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var spinnerCmd tea.Cmd
m.spinner, spinnerCmd = m.spinner.Update(msg)
cmds := []tea.Cmd{spinnerCmd}
switch msg := msg.(type) {
case countdownMsg:
if !m.timeMode {
return m, nil
}
currentTime := msg.time
timeConsumed := currentTime.Sub(m.startTime)
if timeConsumed < m.duration {
m.timeLeft = m.duration - timeConsumed
return m, countDown()
}
m.done = true
return m, tea.Quit
case newWordMsg:
m.currentWord = m.babbler.Babble()
m.textInput.Reset()
m.textInput.Focus()
return m, nil
case tea.KeyMsg:
switch msg.String() {
case "esc", "ctrl+c":
return m, tea.Quit
case "enter":
return m.handleSubmission()
case " ":
if m.babbler.Count == 1 {
return m.handleSubmission()
}
fallthrough
default:
var inputCmd tea.Cmd
m.textInput, inputCmd = m.textInput.Update(msg)
cmds = append(cmds, inputCmd)
}
}
return m, tea.Batch(cmds...)
}
func (m model) View() string {
quitMsg := "(esc or ctrl-c to quit)"
if m.done {
var pointMsg string
if m.points == 0 {
pointMsg = "no good answers"
} else if m.points == 1 {
pointMsg = "1 good answer"
} else {
pointMsg = fmt.Sprintf("%d good answers", m.points)
}
return fmt.Sprintf("⏰ time is up! you had %s\n%s", pointMsg, quitMsg)
}
s := termenv.
String(m.spinner.View()).
Foreground(term.Color("205")).
String()
timerText := fmt.Sprintf("%d seconds remaining\n%d points\n",
int(m.timeLeft.Seconds()),
m.points,
)
if m.timeMode {
timedOutput := fmt.Sprintf("%s%s\n %s\n%s\n%s\n%s\n", s, timerText, m.currentWord, m.textInput.View(), m.status, quitMsg)
return timedOutput
}
nonTimedOutput := fmt.Sprintf("%s%s\n%s\n%s\n%s\n", s, m.currentWord, m.textInput.View(), m.status, quitMsg)
return nonTimedOutput
}
func (m model) handleSubmission() (model, tea.Cmd) {
if m.textInput.Value() == m.currentWord {
m.status = "🎉 correct!"
m.points++
} else {
m.status = "😭 nope"
}
if !m.timeMode {
m.textInput.Blur()
return m, tea.Quit
}
return m, newWord()
}