-
Notifications
You must be signed in to change notification settings - Fork 9
/
pomodoro.go
214 lines (179 loc) · 3.9 KB
/
pomodoro.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/justincampbell/tmux-pomodoro/tmux"
"github.com/0xAX/notificator"
)
const timeFormat = time.RFC3339
var duration, _ = time.ParseDuration("25m")
var noTime time.Time
var notify *notificator.Notificator
const usage = `
github.com/justincampbell/tmux-pomodoro
pomodoro start Start a timer for 25 minutes
pomodoro status Show the remaining time, or an exclamation point if done
pomodoro clear Clear the timer
`
const version = "v1.2.1"
// State is the state of the world passed through the functions to determine
// side-effects.
type State struct {
endTime time.Time
now time.Time
}
// Output has fields for functions to set/append when they intend to output to
// the user.
type Output struct {
text string
returnCode int
}
func init() {
flag.Usage = func() {
fmt.Printf("tmux-pomodoro %s\n", version)
fmt.Printf("%s\n", strings.TrimSpace(usage))
}
flag.Parse()
}
func main() {
state := State{
endTime: readExistingTime(),
now: time.Now(),
}
args := flag.Args()
var command string
if len(args) == 0 {
command = ""
} else {
command = args[0]
}
notify = notificator.New(notificator.Options{
AppName: "tmux-pomodoro",
})
newState, output := parseCommand(state, command)
if newState.endTime != state.endTime {
writeTime(newState.endTime)
}
if output.text != "" {
fmt.Println(output.text)
}
if output.returnCode != 0 {
os.Exit(output.returnCode)
}
}
func refreshTmux() {
_ = tmux.RefreshClient("-S")
}
func parseCommand(state State, command string) (newState State, output Output) {
newState = state
switch command {
case "start":
newState.endTime = state.now.Add(duration)
output.text = "Timer started, 25 minutes remaining"
killRunningBeepers()
_ = startBeeper()
refreshTmux()
case "status":
if state.endTime == noTime {
return
}
output.text = formatRemainingTime(state.endTime, state.now) + " 🍅 "
case "clear":
newState.endTime = noTime
output.text = "Pomodoro cleared!"
killRunningBeepers()
refreshTmux()
case "beep":
<-time.NewTicker(duration).C
var message = "Pomodoro done, take a break!"
_ = tmux.DisplayMessage(message)
notify.Push("Pomodoro", message, "", notificator.UR_NORMAL)
refreshTmux()
case "":
flag.Usage()
default:
flag.Usage()
output.returnCode = 1
}
return
}
func startBeeper() (err error) {
ex, err := os.Executable()
if err != nil {
panic(err)
}
command := exec.Command(ex, "beep")
err = command.Start()
if err != nil {
log.Println(err)
return
}
bytes := []byte(strconv.Itoa(command.Process.Pid))
err = ioutil.WriteFile(pidFilePath(), bytes, 0644)
if err != nil {
log.Println(err)
}
return
}
func killRunningBeepers() {
bytes, err := ioutil.ReadFile(pidFilePath())
if err != nil {
return
}
pid, err := strconv.Atoi(string(bytes[:]))
if err != nil {
return
}
process, err := os.FindProcess(pid)
if err != nil {
return
}
_ = process.Kill()
}
func formatRemainingTime(existingTime time.Time, now time.Time) string {
remaining := existingTime.Sub(now)
remainingMinutes := remaining.Minutes()
if remainingMinutes >= 0 {
return strconv.FormatFloat(remainingMinutes, 'f', 0, 64)
}
return "❗️"
}
func writeTime(t time.Time) {
var bytes []byte
if t != noTime {
bytes = []byte(t.Format(timeFormat))
}
err := ioutil.WriteFile(filePath(), bytes, 0644)
if err != nil {
log.Fatal(err)
}
}
func readExistingTime() time.Time {
bytes, err := ioutil.ReadFile(filePath())
if err != nil {
return noTime
}
contents := string(bytes[:])
contents = strings.TrimSpace(contents)
result, err := time.Parse(timeFormat, contents)
if err != nil {
return noTime
}
return result
}
func filePath() string {
return homeDir() + "/.pomodoro"
}
func pidFilePath() string {
return homeDir() + "/.pomodoro.pid"
}
func homeDir() string {
return os.Getenv("HOME")
}