-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer.go
83 lines (72 loc) · 1.57 KB
/
timer.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
package main
import (
"fmt"
"os/exec"
"runtime"
"time"
"github.com/gen2brain/beeep"
"fyne.io/fyne/v2/widget"
)
type timer struct {
start time.Time
inWorkMode bool
workDuration int
restDuration int
}
func (t timer) getElapsedTimeInSeconds() int {
return int(time.Since(t.start).Seconds())
}
func (t *timer) switchMode() {
t.start = time.Now()
t.inWorkMode = !t.inWorkMode
}
func (t timer) alert() {
message := "Take a break"
if !t.inWorkMode {
message = "Back to work"
}
fmt.Println(message)
os := runtime.GOOS
if os == "darwin" && !isQuietTime() {
go exec.Command("say", message).Output()
}
beeep.Notify(message, message, "assets/information.png")
}
func (t timer) shouldSwitchMode(elapsed int) bool {
return elapsed == t.getDuration()
}
func (t timer) getDuration() int {
duration := t.workDuration
if !t.inWorkMode {
duration = t.restDuration
}
return duration
}
func (t timer) getMode() string {
mode := "Work"
if !t.inWorkMode {
mode = "Rest"
}
return mode
}
func (t timer) printTimeRemaining(elapsed int, clock *widget.Label) {
timeRemaining := t.getDuration() - elapsed
minutes := timeRemaining / 60
seconds := timeRemaining - minutes*60
updatedTime := fmt.Sprintf("\r%v: %02d:%02d", t.getMode(), minutes, seconds)
clock.SetText(updatedTime)
}
func (t timer) updateTime(clock *widget.Label) {
prevElapsed := 0
for {
elapsed := t.getElapsedTimeInSeconds()
if elapsed != prevElapsed {
t.printTimeRemaining(elapsed, clock)
prevElapsed = elapsed
if t.shouldSwitchMode(elapsed) {
t.alert()
t.switchMode()
}
}
}
}