-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
365 lines (316 loc) · 8.33 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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package main
import (
"fmt"
"os"
"time"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/timer"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/isaacdonaldson/pomgo/fonts"
)
const (
WorkPeriod = time.Second * 1500 // 25 mins
RestPeriod = time.Second * 300 // 5 mins
LongRestPeriod = time.Second * 1500 // 900 mins
)
const (
ColorGreen = "#a9dc76"
ColorRed = "#ff6188"
ColorYellow = "#ffd866"
ColorGrey = "#2d2a2e"
ColorLightGrey = "#939293"
ColorPurple = "#ab9df2"
ColorOrange = "#fc9867"
)
type TextStyle int
const (
SimpleLineSmall TextStyle = iota
ShadowBlockLarge
PlainBlockLarge
)
type viewport struct {
Style lipgloss.Style
Width int
Height int
TextColor string
TextStyle TextStyle
}
type timings struct {
workPeriod int
restPeriod int
}
type model struct {
timer timer.Model
keymap keymap
help help.Model
quitting bool
timings timings
viewport viewport
}
type keymap struct {
start key.Binding
stop key.Binding
reset key.Binding
quit key.Binding
}
func (m model) Init() tea.Cmd {
return m.timer.Init()
}
func (m model) Timings() (int, int) {
return m.timings.workPeriod, m.timings.restPeriod
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case timer.TickMsg:
var cmd tea.Cmd
m.timer, cmd = m.timer.Update(msg)
return m, cmd
case timer.StartStopMsg:
var cmd tea.Cmd
m.timer, cmd = m.timer.Update(msg)
m.keymap.stop.SetEnabled(m.timer.Running())
m.keymap.start.SetEnabled(!m.timer.Running())
return m, cmd
case timer.TimeoutMsg:
// Stop the timer and reset the values for the next period
var cmd tea.Cmd
stopMsg := timer.StartStopMsg{ID: m.timer.ID()}
m.timer, cmd = m.timer.Update(stopMsg)
m.keymap.stop.SetEnabled(m.timer.Running())
m.keymap.start.SetEnabled(!m.timer.Running())
// TODO: have this display 00:00 and on start display the next time?
work, rest := m.Timings()
if work == 1 && rest == 0 {
m.timings.restPeriod++
m.timer.Timeout = RestPeriod
m.viewport.TextColor = ColorGreen
return m, cmd
} else if work == 1 && rest == 1 {
m.timings.workPeriod++
m.timer.Timeout = WorkPeriod
m.viewport.TextColor = ColorYellow
return m, cmd
} else if work == 2 && rest == 1 {
m.timings.restPeriod++
m.timer.Timeout = RestPeriod
m.viewport.TextColor = ColorGreen
return m, cmd
} else if work == 2 && rest == 2 {
m.timings.workPeriod++
m.timer.Timeout = WorkPeriod
m.viewport.TextColor = ColorYellow
return m, cmd
} else if work == 3 && rest == 2 {
m.timings.restPeriod++
m.timer.Timeout = LongRestPeriod
m.viewport.TextColor = ColorGreen
return m, cmd
} else {
m.timings.workPeriod = 1
m.timings.restPeriod = 0
m.timer.Timeout = WorkPeriod
m.viewport.TextColor = ColorYellow
return m, cmd
}
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keymap.quit):
m.quitting = true
return m, tea.Quit
case key.Matches(msg, m.keymap.reset):
m.timings.workPeriod = 1
m.timings.restPeriod = 0
m.timer.Timeout = WorkPeriod
m.viewport.TextColor = ColorYellow
case key.Matches(msg, m.keymap.start, m.keymap.stop):
return m, m.timer.Toggle()
}
case tea.WindowSizeMsg:
m.viewport.Width = msg.Width
m.viewport.Height = msg.Height
}
return m, nil
}
func (m model) helpView() string {
return m.help.ShortHelpView([]key.Binding{
m.keymap.start,
m.keymap.stop,
m.keymap.reset,
m.keymap.quit,
})
}
func (m model) View() string {
// For a more detailed timer view you could read m.timer.Timeout to get
// the remaining time as a time.Duration and skip calling m.timer.View()
// entirely.
t := m.timer.Timeout
totalSeconds := int64(t.Seconds())
mins := totalSeconds / 60
secs := totalSeconds - (mins * 60)
millis := t.Milliseconds()
// TODO: also render the milliseconds
minsString := numToString(mins, m.viewport.TextStyle)
secsString := numToString(secs, m.viewport.TextStyle)
// TODO: base this off the config/selected font
var numRows int
switch m.viewport.TextStyle {
case SimpleLineSmall:
numRows = fonts.SimpleLineSmallHeight
case ShadowBlockLarge:
numRows = fonts.ShadowBlockLargeHeight
case PlainBlockLarge:
numRows = fonts.PlainBlockLargeHeight
default:
numRows = fonts.ShadowBlockLargeHeight
}
s := "Pomogoro\n"
for row := 0; row < numRows+1; row++ {
s += minsString[row] + fonts.ColonForSize(numRows)[row] + " " + secsString[row]
if row != numRows {
s += "\n"
}
}
s += lipgloss.NewStyle().
Foreground(lipgloss.Color(ColorLightGrey)).
Render(fmt.Sprintf("%03d", int(millis%1000)))
s += "\n"
// Adding the styling to the numbers
timerStyle := lipgloss.NewStyle().
MarginRight(4).
MarginLeft(4).
Foreground(lipgloss.Color(m.viewport.TextColor))
if !m.quitting {
// TODO: add in extra things like title and clock time here
s += m.helpView()
s = timerStyle.Render(s)
} else {
return timerStyle.
UnsetBorderBottom().
UnsetBorderTop().
UnsetBorderLeft().
UnsetBorderRight().
Render(s)
}
w := m.viewport.Width
h := m.viewport.Height
clockWidth := lipgloss.Width(s)
clockHeight := lipgloss.Height(s)
// s += "\n-> " + strconv.Itoa(clockWidth) + " " + strconv.Itoa(clockHeight) + " | " + strconv.Itoa(w) + " " + strconv.Itoa(h) + " <-"
halfW := (w - clockWidth) / 2
halfH := (h - clockHeight) / 2
// TODO: Move all styling to a separate function
// Creating the styling for the terminal window
terminalStyle := lipgloss.NewStyle().
PaddingLeft(halfW).
PaddingRight(halfW - 2).
PaddingTop(halfH - 1).
PaddingBottom(halfH - 1).
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color(ColorGreen)) // TODO: make a light green or yellow like zellij
block := lipgloss.Place(w, h, lipgloss.Center, lipgloss.Center, terminalStyle.Render(s))
return block
}
func main() {
m := model{
timer: timer.NewWithInterval(WorkPeriod, time.Millisecond),
keymap: keymap{
start: key.NewBinding(
key.WithKeys("s"),
key.WithHelp("s", "start"),
),
stop: key.NewBinding(
key.WithKeys("s"),
key.WithHelp("s", "stop"),
),
reset: key.NewBinding(
key.WithKeys("r"),
key.WithHelp("r", "reset"),
),
quit: key.NewBinding(
key.WithKeys("q", "ctrl+c"),
key.WithHelp("q", "quit"),
),
},
help: help.New(),
timings: timings{
workPeriod: 1,
restPeriod: 0,
},
viewport: viewport{
Style: lipgloss.NewStyle(),
Width: 80,
Height: 24,
TextColor: ColorYellow,
TextStyle: ShadowBlockLarge, // TODO: have this configurable
},
}
m.keymap.start.SetEnabled(false)
if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Uh oh, we encountered an error:", err)
os.Exit(1)
}
}
func numToString(num int64, style TextStyle) []string {
if num < 0 {
panic("number needs to be positive")
}
hundreds := (num / 100) % 10
tens := (num / 10) % 10
ones := num % 10
ltrs := [][]string{}
if hundreds > 0 {
ltrs = append(ltrs, blockNums(hundreds, style))
}
ltrs = append(ltrs, blockNums(tens, style))
ltrs = append(ltrs, blockNums(ones, style))
var numRows int
switch style {
case SimpleLineSmall:
numRows = fonts.SimpleLineSmallHeight
case ShadowBlockLarge:
numRows = fonts.ShadowBlockLargeHeight
case PlainBlockLarge:
numRows = fonts.PlainBlockLargeHeight
default:
numRows = fonts.ShadowBlockLargeHeight
}
block := []string{}
for row := 0; row < numRows+1; row++ {
s := ""
for idx := 0; idx < len(ltrs); idx++ {
s += ltrs[idx][row]
s += " "
}
block = append(block, s)
}
return block
}
func blockNums(n int64, style TextStyle) []string {
if n > 9 {
panic("provided number was greater than 9. Provide numbers in the range 0-9")
}
var font [][]string
switch style {
case SimpleLineSmall:
font = fonts.SimpleLineSmallNumbers
case ShadowBlockLarge:
font = fonts.ShadowBlockLargeNumbers
case PlainBlockLarge:
font = fonts.PlainBlockLargeNumbers
default:
font = fonts.ShadowBlockLargeNumbers
}
return font[n]
}
// TODO:
// [x] Block letter
// [x] Format to the center
// [x] add color to the numbers
// [x] add milliseconds in grey normal letter
// [ ] have the real clock time displayed as well
// [ ] configurable font, colours
// [ ] ability to invert colors so yellow background and dark grey text
// [ ] have other fonts
// [ ] have a small tomato?