-
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
// Copyright 2026 Shota FUJI <pockawoooh@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
"math"
"os"
"time"
tea "charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"
"github.com/charmbracelet/x/ansi"
"pocka.jp/x/system/programs/pomodoro/preferences"
"pocka.jp/x/system/programs/pomodoro/timer"
)
type model struct {
phase timer.Phase
remaining time.Duration
isRunning bool
preferences preferences.Preferences
timestamp time.Time
size tea.WindowSizeMsg
}
func (m model) Init() tea.Cmd {
return nil
}
type Tick time.Time
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyPressMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case "space", "enter", "p":
m.isRunning = !m.isRunning
if !m.isRunning {
return m, nil
}
m.timestamp = time.Now()
return m, tea.Tick(time.Second, func(t time.Time) tea.Msg {
return Tick(t)
})
case "s":
m.phase = m.phase.Next(m.preferences)
m.remaining = m.phase.Duration()
return m, nil
case "r":
m.isRunning = false
m.phase = timer.NewWorkPhase(m.preferences)
m.remaining = m.phase.Duration()
return m, nil
}
case tea.WindowSizeMsg:
m.size = msg
return m, nil
case Tick:
if !m.isRunning {
return m, nil
}
cmds := make([]tea.Cmd, 1, 2)
cmds[0] = tea.Tick(time.Second, func(t time.Time) tea.Msg {
return Tick(t)
})
m.remaining -= time.Time(msg).Sub(m.timestamp)
m.timestamp = time.Time(msg)
if m.remaining <= 0 {
m.phase = m.phase.Next(m.preferences)
m.remaining = m.phase.Duration()
cmds = append(cmds, tea.Raw(ansi.URxvtExt("notify", m.phase.DisplayName(), m.phase.Duration().String())))
}
return m, tea.Batch(cmds...)
}
return m, nil
}
func formatDuration(d time.Duration) string {
secs := math.Ceil(d.Seconds())
mins := uint(math.Trunc(secs / 60))
return fmt.Sprintf("%02d:%02d", mins, uint(secs)%60)
}
func (m model) View() tea.View {
usageStyle := lipgloss.NewStyle().Foreground(lipgloss.Black).Bold(true)
usageSection := lipgloss.Wrap(usageStyle.Render("space/p: Toggle play/pause, s: Skip to next, r: Reset, q: Quit"), m.size.Width, ", ")
titleStyle := lipgloss.NewStyle().Bold(true)
remainingStyle := lipgloss.NewStyle()
if m.isRunning {
titleStyle = titleStyle.Foreground(lipgloss.Blue)
if m.remaining <= time.Second*5 {
remainingStyle = remainingStyle.Foreground(lipgloss.Red)
}
}
title := titleStyle.Render(m.phase.DisplayName())
durations := remainingStyle.Render(formatDuration(m.remaining)) + " / " + formatDuration(m.phase.Duration())
mainSection := lipgloss.Place(
m.size.Width, m.size.Height-lipgloss.Height(usageSection),
lipgloss.Center, lipgloss.Center,
lipgloss.JoinVertical(lipgloss.Top, title, durations),
)
v := tea.NewView(lipgloss.JoinVertical(lipgloss.Center, mainSection, usageSection))
v.AltScreen = true
return v
}
func newModel(pref preferences.Preferences) model {
phase := timer.NewWorkPhase(pref)
return model{
phase: phase,
remaining: phase.Duration(),
isRunning: false,
preferences: pref,
timestamp: time.Now(),
}
}
func main() {
p := tea.NewProgram(newModel(preferences.Preferences{
SessionCount: 4,
WorkDuration: time.Minute * 25,
ShortBreakDuration: time.Minute * 5,
LongBreakDuration: time.Minute * 30,
}))
if _, err := p.Run(); err != nil {
fmt.Print(err)
os.Exit(1)
}
}