-
Notifications
You must be signed in to change notification settings - Fork 0
/
gomodoro.go
133 lines (107 loc) Β· 2.41 KB
/
gomodoro.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
package main
import (
"fmt"
"github.com/gen2brain/beeep"
"github.com/getlantern/systray"
"math"
"time"
)
type GomodoroState int
const (
Initial GomodoroState = 0
Resting GomodoroState = 1
Working GomodoroState = 2
Toggle GomodoroState = 3
)
const WorkMinutes int64 = 23
const RestMinutes int64 = 7
type SystrayUpdate struct {
state GomodoroState
stateChangeTime time.Time
}
func main() {
systray.Run(onReady, nil)
}
func timeSince(state *SystrayUpdate) (int64, int64) {
elapsed := time.Since(state.stateChangeTime)
elapsedSeconds := int64(math.Round(elapsed.Seconds()))
elapsedMinutes := int64(elapsedSeconds / 60)
return elapsedMinutes, elapsedSeconds - elapsedMinutes*60
}
func updateSystray(state *SystrayUpdate, beep bool) (int64, int64) {
if state.stateChangeTime.IsZero() || state.state == Initial {
systray.SetTitle("π
")
return 0, 0
}
minutes, seconds := timeSince(state)
var stateString string
switch state.state {
case Resting:
stateString = "β"
case Working:
stateString = "π¨"
beep = false
}
if (beep) {
beeep.Notify("π
", stateString, "")
}
systray.SetTitle(fmt.Sprintf("%s%dm%ds", stateString, minutes, seconds))
return minutes, seconds
}
func onReady() {
systray.SetTitle("π
")
systray.SetTooltip("Gomodoro")
mStartStop := systray.AddMenuItem("Start/Stop", "Start/Stop")
mQuitOrig := systray.AddMenuItem("Quit", "Bye bye")
globalState := &SystrayUpdate{Initial, time.Unix(0, 0)}
go func() {
<-mQuitOrig.ClickedCh
systray.Quit()
fmt.Println("Quit")
}()
tick := make(chan int)
go func() {
j := 1
for {
time.Sleep(1025 * time.Millisecond)
tick <- j
j += 1
}
}()
stateChange := make(chan GomodoroState)
go func() {
for {
<-mStartStop.ClickedCh
stateChange <- Toggle
}
}()
go func() {
for {
state := <-stateChange
globalState.stateChangeTime = time.Now()
if state == Toggle {
if globalState.state == Initial || globalState.state == Resting {
globalState.state = Working
} else {
globalState.state = Resting
}
} else {
globalState.state = state
}
updateSystray(globalState, true)
}
}()
go func() {
for {
<-tick
minutes, _ := updateSystray(globalState, false)
if globalState.state == Resting && minutes >= RestMinutes {
stateChange <- Initial
continue
}
if globalState.state == Working && minutes >= WorkMinutes {
stateChange <- Resting
}
}
}()
}