-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
169 lines (146 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
package main
import (
"fmt"
"log"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/progress"
tea "github.com/charmbracelet/bubbletea"
"github.com/gen2brain/beeep"
"github.com/saaste/pomodoro/timer"
)
type TimerState int
const (
TimerStateWait TimerState = iota
TimerStateWork
TimerStateShortBreak
TimerStateLongBreak
)
type model struct {
timerState TimerState
shortBreakCount int
pomodoroCount int
notificationSent bool
keys keyMap
help help.Model
timer timer.Timer
progress progress.Model
config Config
}
func (m model) Init() tea.Cmd {
return timer.TickEverySecond()
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case timer.TickMsg:
m.timer.Update()
m.SendNotificationIfNeeded()
return m, timer.TickEverySecond()
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keys.Quit):
return m, tea.Quit
case key.Matches(msg, m.keys.StartWork):
if m.timerState != TimerStateWork {
m.timerState = TimerStateWork
m.pomodoroCount += 1
m.keys.StartWork.SetEnabled(false)
m.keys.StartBreak.SetEnabled(true)
m.timer.Reset(m.config.WorkDuration())
m.notificationSent = false
}
case key.Matches(msg, m.keys.StartBreak):
m.keys.StartWork.SetEnabled(true)
m.keys.StartBreak.SetEnabled(false)
if m.timerState == TimerStateWork {
if m.shortBreakCount < m.config.ShortBreaks {
m.timerState = TimerStateShortBreak
m.shortBreakCount += 1
m.timer.Reset(m.config.ShortBreakDuration())
} else {
m.timerState = TimerStateLongBreak
m.shortBreakCount = 0
m.timer.Reset(m.config.LongBreakDuration())
}
m.notificationSent = false
}
}
}
return m, nil
}
func (m model) View() string {
s := fmt.Sprintf("%s\n", titleStyle.Render("POMODORO TIMER"))
switch m.timerState {
case TimerStateShortBreak:
if m.timer.IsTimeout() {
s += alertStyle.Render("Time to go back to work!")
} else {
s += fmt.Sprintf("%s\n\n", normalStyle.Render("Time for a quick break!"))
s += fmt.Sprintf("%s: %s\n", hilightStyle.Render("Time left"), m.timer.TimeLeft())
s += fmt.Sprintf("%s: %s", hilightStyle.Render("Progress"), m.progress.ViewAs(m.timer.PercentageLeft()))
}
case TimerStateLongBreak:
if m.timer.IsTimeout() {
s += alertStyle.Render("Time to go back to work!")
} else {
s += fmt.Sprintf("%s\n\n", normalStyle.Render("Time for a longer break! You've earned it!"))
s += fmt.Sprintf("%s: %s\n", hilightStyle.Render("Time left"), m.timer.TimeLeft())
s += fmt.Sprintf("%s: %s", hilightStyle.Render("Progress"), m.progress.ViewAs(m.timer.PercentageLeft()))
}
case TimerStateWork:
if m.timer.IsTimeout() {
s += alertStyle.Render("Good job! Time to have a break!")
} else {
var nth = "1st"
if m.pomodoroCount == 2 {
nth = "2nd"
} else if m.pomodoroCount > 2 {
nth = fmt.Sprintf("%dth", m.pomodoroCount)
}
text := fmt.Sprintf("This is your %s pomodoro. Time to focus on the task!", nth)
s += fmt.Sprintf("%s\n\n", normalStyle.Render(text))
s += fmt.Sprintf("%s: %s\n", hilightStyle.Render("Time left"), m.timer.TimeLeft())
s += fmt.Sprintf("%s: %s", hilightStyle.Render("Progress"), m.progress.ViewAs(m.timer.PercentageLeft()))
}
case TimerStateWait:
s += fmt.Sprintf("%s\n\n", normalStyle.Render("When you are ready, press w to start your first pomodoro."))
}
s += fmt.Sprintf("\n\n%s", m.help.View(m.keys))
return s
}
func (m *model) SendNotificationIfNeeded() {
if !m.config.SendNotification {
return
}
if m.timerState != TimerStateWait && m.timer.IsTimeout() && !m.notificationSent {
switch m.timerState {
case TimerStateWork:
if m.shortBreakCount < 4 {
beeep.Notify("Pomodoro Timer", "Time to have a quick break!", "assets/pomodoro.png")
} else {
beeep.Notify("Pomodoro Timer", "Time to have a longer break!", "assets/pomodoro.png")
}
case TimerStateShortBreak, TimerStateLongBreak:
beeep.Notify("Pomodoro Timer", "Time to go back to work!", "assets/pomodoro.png")
}
m.notificationSent = true
}
}
func main() {
config, err := ReadConfig()
if err != nil {
log.Fatalf("unable to read the config file: %s", err)
}
model := model{
timer: timer.New(config.WorkDuration()),
timerState: TimerStateWait,
progress: progress.New(progress.WithScaledGradient(gradientColors[0], gradientColors[1])),
keys: keys,
help: help.New(),
config: config,
}
p := tea.NewProgram(model)
if _, err := p.Run(); err != nil {
log.Fatalf("failed to run the program: %s", err)
}
}