-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.go
203 lines (175 loc) · 4.12 KB
/
update.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
package main
import (
"time"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
)
func (m model) updateTypingWindow(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "esc":
return m, tea.Quit
case "tab":
var err error
m.placeholder, err = chooseRandomText(m.texts)
if err != nil {
panic(err)
}
m.pruneForNewText()
case "enter":
m.typedEndTime = time.Now()
m.currendWindow = results
case "backspace":
if m.cursor != 0 {
m.cursor--
if m.mistakes[m.cursor] {
delete(m.mistakes, m.cursor)
}
m.text = m.text[:len(m.text)-1]
}
default:
if len(msg.Runes) != 1 {
break
}
m.typedSybmolsCount++
// start timer when typed first symbol
if m.typedSybmolsCount == 1 {
m.typedStartTime = time.Now()
}
// detect mistake if typed sybmol != expected symbol
if m.placeholder[m.cursor] != msg.Runes[0] {
m.mistakeCount++
m.mistakes[m.cursor] = true
}
m.cursor++
m.text = append(m.text, msg.Runes[0])
// if typed last symbol then end the test
if m.cursor == len(m.placeholder) {
m.typedEndTime = time.Now()
m.currendWindow = results
}
}
}
return m, nil
}
func (m model) updateResultsWindow(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "esc":
return m, tea.Quit
case "tab", "enter":
m.cursor = 0
m.currendWindow = mode
}
}
return m, nil
}
func (m model) updateModeWindow(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
// If we set a width on the help menu it can it can gracefully truncate
// its view as needed.
m.help.Width = msg.Width
case tea.KeyMsg:
if key.Matches(msg, m.keys.Help) {
m.help.ShowAll = !m.help.ShowAll
}
switch msg.String() {
case "esc":
return m, tea.Quit
case "up":
if m.cursor == 1 {
m.cursor = 0
}
case "down":
if m.cursor == 0 {
m.cursor = 1
}
case "enter":
if m.cursor == 0 {
m.placeholder = generateRandomWords()
} else {
var err error
m.placeholder, err = chooseRandomText(m.texts)
if err != nil {
panic(err)
}
}
m.pruneForNewText()
m.currendWindow = typing
case "s":
m.currendWindow = settings
}
}
return m, nil
}
func (m model) updateSettings(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "esc":
return m, tea.Quit
// Set focus to next input
case "up", "down":
s := msg.String()
// Did the user press enter while the submit button was focused?
// If so, exit.
if s == "enter" && m.focusIndex == len(m.inputs) {
return m, tea.Quit
}
// Cycle indexes
if s == "up" || s == "shift+tab" {
m.focusIndex--
} else {
m.focusIndex++
}
if m.focusIndex > len(m.inputs) {
m.focusIndex = 0
} else if m.focusIndex < 0 {
m.focusIndex = len(m.inputs)
}
cmds := make([]tea.Cmd, len(m.inputs))
for i := 0; i <= len(m.inputs)-1; i++ {
if i == m.focusIndex {
// Set focused state
cmds[i] = m.inputs[i].Focus()
m.inputs[i].PromptStyle = focusedStyle
m.inputs[i].TextStyle = focusedStyle
continue
}
// Remove focused state
m.inputs[i].Blur()
m.inputs[i].PromptStyle = noStyle
m.inputs[i].TextStyle = noStyle
}
return m, tea.Batch(cmds...)
}
}
// Handle character input and blinking
cmd := m.updateInputs(msg)
return m, cmd
}
func (m *model) updateInputs(msg tea.Msg) tea.Cmd {
cmds := make([]tea.Cmd, len(m.inputs))
// Only text inputs with Focus() set will respond, so it's safe to simply
// update all of them here without any further logic.
for i := range m.inputs {
m.inputs[i], cmds[i] = m.inputs[i].Update(msg)
}
return tea.Batch(cmds...)
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch m.currendWindow {
case typing:
return m.updateTypingWindow(msg)
case results:
return m.updateResultsWindow(msg)
case mode:
return m.updateModeWindow(msg)
case settings:
return m.updateSettings(msg)
}
return m, nil
}