-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
244 lines (200 loc) Β· 5.31 KB
/
ui.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
package pomo
import (
"fmt"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type model struct {
prefix string
quit bool
notified bool
width int
height int
session Session
}
var (
timerStyle = lipgloss.NewStyle().
Bold(true).
Padding(1, 4).
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("240")).
Align(lipgloss.Center)
prefixStyle = lipgloss.NewStyle().
Bold(true).
Padding(0, 1).
Align(lipgloss.Center)
containerStyle = lipgloss.NewStyle().
Align(lipgloss.Center).
Padding(2)
quitStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("240")).
Align(lipgloss.Center)
)
// Init implements tea.Model
func (m model) Init() tea.Cmd {
return tick()
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
return m, nil
case tea.KeyMsg:
switch msg.String() {
case "q", "ctrl+c":
// !! do not stop the session
// if m.session.isRunning() {
// if err := m.session.Stop(); err != nil {
// fmt.Printf("Failed to stop session: %v\n", err)
// }
// }
m.quit = true
return m, tea.Quit
case "w": // Switch to work
duration := conf.QueryString("duration")
if duration == "" {
duration = Break
}
dur, err := time.ParseDuration(duration)
if err != nil {
fmt.Printf("Failed to parse duration: %v\n", err)
}
// Stop current session
if m.session.isRunning() {
if err := m.session.Stop(); err != nil {
fmt.Printf("Failed to stop session: %v\n", err)
}
}
var session Session
if err := session.Start(conf, dur, WorkSession); err != nil {
fmt.Printf("Failed to start work session: %v\n", err)
}
if err := conf.Set("prefix", WorkPrefix); err != nil {
fmt.Printf("Failed to set the prefix config %v\n", err)
}
m.session = session
m.notified = false
return m, tick()
case "b": // Switch to break
duration := conf.QueryString("break")
if duration == "" {
duration = Break
}
dur, err := time.ParseDuration(duration)
if err != nil {
fmt.Printf("Failed to parse duration: %v\n", err)
}
// Stop current session
if m.session.isRunning() {
if err := m.session.Stop(); err != nil {
fmt.Printf("Failed to stop session: %v\n", err)
}
}
var session Session
if err := session.Start(conf, dur, BreakSession); err != nil {
fmt.Printf("Failed to start break session: %v\n", err)
}
if err := conf.Set("prefix", BreakPrefix); err != nil {
fmt.Printf("Failed to set the prefix config %v\n", err)
}
m.session = session
m.notified = false
return m, tick()
case "r": // Reset current timer
if err := m.session.Reset(); err != nil {
fmt.Printf("Failed to stop session: %v\n", err)
}
m.notified = false
}
return m, tick()
case time.Time:
remaining := m.session.Elapsed()
if !m.notified && remaining <= 0 && m.session.isRunning() {
title := "Pomo Timer"
message := "Time to take a break!"
if m.session.Type == BreakSession {
message = "Break is over! Time to focus!"
}
go func() {
if err := sendNotification(title, message); err != nil {
fmt.Printf("Failed to send notification: %v\n", err)
}
if err := playAlert(); err != nil {
fmt.Printf("Failed to play alert: %v\n", err)
}
}()
m.notified = true
}
return m, tick()
}
return m, nil
}
func (m model) View() string {
if m.quit {
return quitStyle.Render("Goodbye!") + "\n"
}
// Set colors based on session type and remaining time
var timeStyle = timerStyle
remaining := m.session.Elapsed()
if remaining < 0 {
timeStyle = timeStyle.Foreground(lipgloss.Color("196")) // Red
} else if m.session.Type == WorkSession {
timeStyle = timeStyle.Foreground(lipgloss.Color("35")) // Green
} else {
timeStyle = timeStyle.Foreground(lipgloss.Color("39")) // Blue
}
// Format the duration display
duration := remaining
sign := ""
if duration < 0 {
duration = -duration
sign = "-"
}
remainingStr := sign + StopWatchFormat(duration)
// ... (rest of the View formatting remains the same)
contentWidth := m.width / 6
if contentWidth < 30 {
contentWidth = 30 // minimum width
}
timeStyle = timeStyle.Width(contentWidth)
prefixStyle = prefixStyle.Width(contentWidth)
containerStyle = containerStyle.Width(m.width)
var sb strings.Builder
verticalPad := (m.height - 9) / 2
if verticalPad > 0 {
sb.WriteString(strings.Repeat("\n", verticalPad))
}
prefixText := prefixStyle.Render(m.prefix)
sb.WriteString(prefixText + "\n\n")
timerText := timeStyle.Render(remainingStr)
sb.WriteString(timerText + "\n\n")
helpStyle := quitStyle
sb.WriteString(helpStyle.Render("w: work β’ b: break β’ r: reset β’ q: quit") + "\n")
return containerStyle.Render(sb.String())
}
func StartUI() error {
var session Session
if err := session.Get(); err != nil {
return fmt.Errorf("failed to get current session: %v", err)
}
prefix := conf.QueryString("prefix")
if prefix == "" {
prefix = WorkPrefix
}
initialModel := model{
prefix: prefix,
session: session,
}
p := tea.NewProgram(
initialModel,
tea.WithAltScreen(),
tea.WithMouseCellMotion(),
)
if _, err := p.Run(); err != nil {
return fmt.Errorf("failed to start UI: %v", err)
}
return nil
}