-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
342 lines (309 loc) · 9.56 KB
/
Copy pathmain.go
File metadata and controls
342 lines (309 loc) · 9.56 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package main
import (
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"time"
"github.com/anschnapp/pomodorofactory/pkg/audio"
"github.com/anschnapp/pomodorofactory/pkg/celebration"
"github.com/anschnapp/pomodorofactory/pkg/commandinput"
"github.com/anschnapp/pomodorofactory/pkg/factoryscene"
"github.com/anschnapp/pomodorofactory/pkg/motivationcloud"
"github.com/anschnapp/pomodorofactory/pkg/product"
"github.com/anschnapp/pomodorofactory/pkg/status"
"github.com/anschnapp/pomodorofactory/pkg/timer"
"github.com/anschnapp/pomodorofactory/pkg/view"
"golang.org/x/term"
)
var (
congratsWords = []string{
"Congratulations", "Well done", "Fantastic", "Bravo",
"Impressive", "Outstanding", "Remarkable", "Excellent",
"Stupendous", "Wonderful", "Sensational", "Phenomenal",
"Incredible", "Marvelous", "Brilliant", "Spectacular",
"Superb", "Terrific", "Magnificent", "Splendid",
}
adverbWords = []string{
"successfully", "masterfully", "skillfully", "proudly",
"brilliantly", "flawlessly", "expertly", "elegantly",
"perfectly", "superbly", "gracefully", "precisely",
"diligently", "gloriously", "beautifully", "boldly",
"heroically", "effortlessly", "passionately", "triumphantly",
}
verbWords = []string{
"built", "assembled", "crafted", "manufactured",
"constructed", "forged", "produced", "engineered",
"fabricated", "created", "welded", "shaped",
"molded", "formed", "designed", "composed",
"erected", "fashioned", "devised", "completed",
}
adjectiveWords = []string{
"beautiful", "magnificent", "splendid", "glorious",
"stunning", "exquisite", "pristine", "majestic",
"radiant", "dazzling", "fabulous", "grand",
"supreme", "flawless", "legendary", "epic",
"divine", "stellar", "remarkable", "perfect",
}
)
func randomCongrats(productName string) string {
return fmt.Sprintf("%s we %s %s a %s %s",
congratsWords[rand.Intn(len(congratsWords))],
adverbWords[rand.Intn(len(adverbWords))],
verbWords[rand.Intn(len(verbWords))],
adjectiveWords[rand.Intn(len(adjectiveWords))],
strings.ToLower(productName),
)
}
func selectorLine(products []*product.Product, idx int) string {
return fmt.Sprintf("build next: \u2190 [%s] \u2192", products[idx].Name)
}
type appState int
const (
stateIdle appState = iota // waiting for 's' to start a pomodoro
stateWorking // pomodoro timer running
stateWaitingForCelebration // timer done, waiting for user to press 'c'
stateCelebrating // celebration animation playing
stateOnBreak // break timer running (auto-started)
)
const (
shortBreak = 5 * time.Minute
longBreak = 15 * time.Minute
pomodorosPerSet = 4
)
// Sentinel rune values for arrow keys (not valid Unicode)
const (
keyLeft = rune(-1)
keyRight = rune(-2)
)
func breakCmdText(isLong bool) string {
if isLong {
return "[s]mall cooldown | [q]uit"
}
return "[l]ong cooldown | [q]uit"
}
func main() {
// Parse optional duration argument (in minutes, decimal allowed)
workDuration := 25 * time.Minute
if len(os.Args) > 1 {
minutes, err := strconv.ParseFloat(os.Args[1], 64)
if err != nil {
fmt.Fprintf(os.Stderr, "invalid duration: %s (expected minutes, e.g. 25 or 0.2)\n", os.Args[1])
os.Exit(1)
}
workDuration = time.Duration(minutes * float64(time.Minute))
}
// Put terminal in raw mode
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
fmt.Fprintf(os.Stderr, "failed to set raw mode: %v\n", err)
os.Exit(1)
}
defer term.Restore(int(os.Stdin.Fd()), oldState)
// Enter alternate screen buffer
fmt.Print("\033[?1049h")
defer fmt.Print("\033[?1049l")
// Hide cursor
fmt.Print("\033[?25l")
defer fmt.Print("\033[?25h")
// Initialize audio (optional — celebration works visually without it)
audioEngine, _ := audio.NewEngine()
// Product selection state
products := product.All
selectedProductIdx := 0
achievedEmojis := []string{}
// Build components
factory := factoryscene.MakeFactoryScene(products)
motivationcloudComp := motivationcloud.MakeMotivationcloud()
statusComp := status.MakeStatus()
cmdInput := commandinput.MakeCommandinput()
cmdInput.SetTexts("[s]tart | [q]uit", selectorLine(products, selectedProductIdx))
v := view.MakeView(factory, motivationcloudComp, statusComp, cmdInput)
t := timer.NewTimer(workDuration)
celeb := celebration.New(audioEngine)
lastShuffle := time.Now()
state := stateIdle
congratsMsg := ""
isLongBreak := false
// Read input in a goroutine; arrow keys are decoded as sentinel rune values
inputCh := make(chan rune)
go func() {
buf := make([]byte, 1)
for {
n, err := os.Stdin.Read(buf)
if n > 0 {
b := buf[0]
if b == 0x1b {
// Possible escape sequence — read up to two more bytes
seq := make([]byte, 2)
n2, _ := os.Stdin.Read(seq[:1])
if n2 > 0 && seq[0] == '[' {
n3, _ := os.Stdin.Read(seq[1:2])
if n3 > 0 {
switch seq[1] {
case 'C':
inputCh <- keyRight
case 'D':
inputCh <- keyLeft
}
}
}
// lone ESC or unrecognised sequence: ignored
} else {
inputCh <- rune(b)
}
}
if err != nil {
close(inputCh)
return
}
}
}()
// Initial render
v.Render()
v.Print()
ticker := time.NewTicker(50 * time.Millisecond)
defer ticker.Stop()
// Event loop
for {
dirty := false
select {
case b, ok := <-inputCh:
if !ok {
return
}
dirty = true
switch b {
case 'q', 0x03: // 'q' or Ctrl+C
return
case 'h', keyLeft:
if state == stateIdle {
selectedProductIdx = (selectedProductIdx - 1 + len(products)) % len(products)
cmdInput.SetTexts("[s]tart | [q]uit", selectorLine(products, selectedProductIdx))
}
case 'l', keyRight:
if state == stateIdle {
selectedProductIdx = (selectedProductIdx + 1) % len(products)
cmdInput.SetTexts("[s]tart | [q]uit", selectorLine(products, selectedProductIdx))
} else if b == 'l' && state == stateOnBreak && !isLongBreak {
isLongBreak = true
t.Reset(longBreak)
t.Start()
cmdInput.SetTexts(breakCmdText(isLongBreak), "")
}
case 's':
if state == stateIdle {
state = stateWorking
factory.LoadArt(products[selectedProductIdx].Art)
t.Reset(workDuration)
t.Start()
factory.Reset()
cmdInput.SetTexts("e[x]it current pomodoro | [q]uit", "")
} else if state == stateOnBreak && isLongBreak {
isLongBreak = false
t.Reset(shortBreak)
t.Start()
cmdInput.SetTexts(breakCmdText(isLongBreak), "")
}
case 'x':
if state == stateWorking {
state = stateIdle
t.Reset(workDuration)
factory.Reset()
statusComp.SetAchievements("Factory ready press [s] to start", achievedEmojis)
cmdInput.SetTexts("[s]tart | [q]uit", selectorLine(products, selectedProductIdx))
}
case 'c':
if state == stateWaitingForCelebration {
state = stateCelebrating
congratsMsg = randomCongrats(products[selectedProductIdx].Name)
celeb.Start(congratsMsg)
}
}
case <-ticker.C:
// tick proceeds — let state and cloud determine if a redraw is needed
}
switch state {
case stateWorking:
dirty = true
factory.SetProgress(t.Progress())
remaining := t.Remaining()
mins := int(remaining.Minutes())
secs := int(remaining.Seconds()) % 60
statusComp.SetAchievements(
fmt.Sprintf("Factory running %02d:%02d", mins, secs),
achievedEmojis,
)
if t.IsFinished() {
state = stateWaitingForCelebration
factory.SetProgress(1.0)
statusComp.SetAchievements("Pomodoro done! Press [c] to celebrate", achievedEmojis)
cmdInput.SetTexts("[c]elebrate", "")
if audioEngine != nil {
audioEngine.Play(audio.MakeNotificationSound())
}
}
case stateWaitingForCelebration:
// nothing to update each tick — dirty stays false unless cloud animates
case stateCelebrating:
dirty = true
if celeb.IsActive() {
phase := celeb.Tick()
switch phase {
case celebration.PhaseParty:
factory.SetCelebrating(celeb.PartyTick())
statusComp.SetCelebrationText("POMODORO COMPLETE!", celeb.PartyTick())
case celebration.PhaseSpeech:
factory.SetProgress(1.0)
statusComp.SetSpeechText(congratsMsg, celeb.CurrentCharIndex())
}
} else {
// Celebration finished — record achievement and auto-start break
achievedEmojis = append(achievedEmojis, products[selectedProductIdx].Emoji)
isLongBreak = len(achievedEmojis)%pomodorosPerSet == 0
breakDuration := shortBreak
if isLongBreak {
breakDuration = longBreak
}
state = stateOnBreak
t.Reset(breakDuration)
t.Start()
factory.SetProgress(1.0)
cmdInput.SetTexts(breakCmdText(isLongBreak), "")
}
case stateOnBreak:
dirty = true
t.Progress() // drive the finished flag
remaining := t.Remaining()
mins := int(remaining.Minutes())
secs := int(remaining.Seconds()) % 60
label := "Factory needs a short cooldown"
if isLongBreak {
label = "Factory needs a longer cooldown"
}
statusComp.SetAchievements(
fmt.Sprintf("%s %02d:%02d", label, mins, secs),
achievedEmojis,
)
if t.IsFinished() {
state = stateIdle
factory.Reset()
statusComp.SetAchievements("Factory ready press [s] to start", achievedEmojis)
cmdInput.SetTexts("[s]tart | [q]uit", selectorLine(products, selectedProductIdx))
}
}
// Replace one phrase every 15 seconds (with animated transition)
if time.Since(lastShuffle) >= 15*time.Second {
motivationcloudComp.ReplaceOne()
lastShuffle = time.Now()
}
if motivationcloudComp.Tick() {
dirty = true
}
if dirty {
v.Render()
v.Print()
}
}
}