-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
310 lines (255 loc) · 5.5 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"101/internal/player"
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
css "101/internal/styles"
)
//[ ] https://dev.to/pomdtr/how-to-debug-bubble-tea-applications-in-visual-studio-code-50jp
// Figure out how to connect to headless dlv
var (
players []*player.Player
)
type model struct {
s css.Styles
list []*player.Player
cursor int
selected int
chosen bool
command string
textInput textinput.Model
listHelp string
}
func main() {
f, err := LogToFile(".log", "debug")
if err != nil {
fmt.Println("fatal:", err)
os.Exit(1)
}
defer f.Close()
p := tea.NewProgram(initModel(), tea.WithAltScreen())
if _, err := p.Run(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
}
}
func initModel() model {
flag.Parse()
names := flag.Args()
{ // Create players from CLI args
switch len(names) {
case 0:
players = append(players, player.NewPlayer("Player 1"))
players = append(players, player.NewPlayer("Player 2"))
default:
for _, e := range names {
{
players = append(players, player.NewPlayer(e))
}
}
}
}
csss := *css.New()
return model{
list: players,
cursor: 0,
s: csss,
listHelp: initListHelp(&csss),
}
}
func initListHelp(s *css.Styles) string {
help := map[string]string{
"enter": "add new points",
"bckp": "substract int",
"0": "nullify player",
"esc": "quit",
}
var helpWriter strings.Builder
for i, j := range help {
helpWriter.WriteString(fmt.Sprintf(
"%s %s\n",
s.HelpKeyStyle.Render("• "+i),
s.HelpDescStyle.Render(j)))
}
return helpWriter.String()
}
func (m *model) initInputField() {
ti := textinput.New()
switch m.command {
case "add":
ti.Placeholder = "jqk67890t"
case "sub":
ti.Placeholder = "qQ / qqQQ"
}
ti.Focus()
ti.CharLimit = 156
ti.Width = 50
m.textInput = ti
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if !m.chosen {
return playersUpdate(msg, m)
} else {
return scoreUpdate(msg, m)
}
}
func scoreUpdate(msg tea.Msg, m model) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEnter:
m.convertStringToCommand(m.textInput.Value())
m.chosen = false
//m.cursor = 0
return m, cmd
case tea.KeyEsc:
m.chosen = false
//m.cursor = 0
return m, cmd
case tea.KeyBackspace, tea.KeyDelete, tea.KeyRight, tea.KeyLeft:
m.textInput, cmd = m.textInput.Update(msg)
return m, cmd
}
switch strings.ToLower(msg.String()) {
case "j", "k", "q", "t", "6", "7", "8", "9", "0":
m.textInput, cmd = m.textInput.Update(msg)
return m, cmd
}
}
return m, nil
}
func playersUpdate(msg tea.Msg, m model) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
// Is it a key press?
case tea.KeyMsg:
// Cool, what was the actual key pressed?
switch msg.String() {
case "up":
m.CursorUp()
case "down":
m.CursorDown()
case "enter":
m.SelectWithAddCmd()
case "backspace":
m.SelectWithSubstractCmd()
case "0":
m.NullScore()
// These keys should exit the program.
case "esc":
return m, tea.Quit
}
}
return m, nil
}
func (m *model) NullScore() {
m.selected = m.cursor
m.list[m.selected].Win()
}
func (m *model) CursorUp() {
if m.cursor > 0 {
m.cursor--
} else {
m.cursor = len(m.list) - 1
}
}
func (m *model) CursorDown() {
if m.cursor < len(m.list)-1 {
m.cursor++
} else {
m.cursor = 0
}
}
func (m *model) SelectWithAddCmd() {
m.chosen = true
m.selected = m.cursor
m.command = "add"
m.initInputField()
}
func (m *model) SelectWithSubstractCmd() {
m.chosen = true
m.selected = m.cursor
m.command = "sub"
m.initInputField()
}
func (m model) View() string {
var rndr string
if !m.chosen {
rndr = playersView(m)
} else {
rndr = scoreView(m)
}
return rndr
}
func scoreView(m model) string {
var prompt string
switch m.command {
case "add":
prompt = "Card combination"
case "sub":
prompt = "Q or QQ"
}
prompt = m.s.TitleSelected.Render(prompt)
help := m.s.HelpDescStyle.Render("esc -> quit")
return m.s.AppStyle.Render(fmt.Sprintf(
"%s\n\n%s\n\n%s",
prompt,
m.textInput.View(),
help,
) + "\n")
}
func playersView(m model) string {
var sb strings.Builder
for i, p := range m.list {
switch m.cursor {
case i: // selected
title := m.s.TitleSelected.Render(p.Title())
desc := m.s.DescSelected.Render(p.Description())
content := fmt.Sprintf(
"%s\n%s",
title,
desc)
sb.WriteString(m.s.BlockSelected.Render(content))
default:
title := m.s.TitleDefault.Render(p.Title())
desc := m.s.DescDefault.Render(p.Description())
content := fmt.Sprintf(
"%s\n%s",
title,
desc)
sb.WriteString(m.s.BlockDefault.Render(content))
}
}
sb.WriteString(m.s.BlockHelp.Render(m.listHelp))
return m.s.AppStyle.Render(sb.String())
}
func (m *model) convertStringToCommand(text string) {
switch m.command {
case "add":
p := players[m.selected]
p.SetPoints(text)
log.Printf("%s %s %s %s", p.Title(), m.command, text, p.Description())
case "sub":
p := players[m.selected]
p.SubPoints(text)
log.Printf("%s %s %s %s", p.Title(), m.command, text, p.Description())
default:
log.Println("wrong command")
}
}
func LogToFile(path string, prefix string) (*os.File, error) {
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
return nil, err
}
log.SetOutput(f)
return f, nil
}